Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ProofAux.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_ProofAux
3///
4/// Selector used for auxiliary actions in the PROOF tutorials
5///
6/// \macro_code
7///
8/// \author Gerardo Ganis (gerardo.ganis@cern.ch)
9
10#define ProofAux_cxx
11
12#include "ProofAux.h"
13#include "TDSet.h"
14#include "TProofServ.h"
15#include "TMap.h"
16#include "TString.h"
17#include "TSystem.h"
18#include "TParameter.h"
19#include "TFile.h"
20#include "TUrl.h"
21#include "TTree.h"
22#include "TRandom.h"
23#include "TMath.h"
24
25//_____________________________________________________________________________
26ProofAux::ProofAux()
27{
28 // Constructor
29
30 fAction = -1;
31 fNEvents= -1;
32 fMainList = 0;
33 fFriendList = 0;
34 fDir = "";
35}
36
37//_____________________________________________________________________________
38ProofAux::~ProofAux()
39{
40 // Destructor
41
42}
43
44//_____________________________________________________________________________
45Int_t ProofAux::GetAction(TList *input)
46{
47 // Get the required action.
48 // Returns -1 if unknown.
49
50 Int_t action = -1;
51 // Determine the test type
52 TNamed *ntype = dynamic_cast<TNamed*>(input->FindObject("ProofAux_Action"));
53 if (ntype) {
54 TString act(ntype->GetTitle());
55 if (act == "GenerateTreesSameFile") {
56 action = 1;
57 } else if (act.BeginsWith("GenerateTrees")) {
58 action = 0;
59 // Extract directory, if any
60 Ssiz_t icol = act.Index(":");
61 if (icol != kNPOS) {
62 action = 2;
63 act.Remove(0, icol+1);
64 if (!act.IsNull()) fDir = act;
65 }
66 } else {
67 Warning("GetAction", "unknown action: '%s'", ntype->GetTitle());
68 }
69 }
70 // Done
71 return action;
72}
73
74
75//_____________________________________________________________________________
76void ProofAux::Begin(TTree * /*tree*/)
77{
78 // The Begin() function is called at the start of the query.
79 // When running with PROOF Begin() is only called on the client.
80 // The tree argument is deprecated (on PROOF 0 is passed).
81}
82
83//_____________________________________________________________________________
84void ProofAux::SlaveBegin(TTree * /*tree*/)
85{
86 // The SlaveBegin() function is called after the Begin() function.
87 // When running with PROOF SlaveBegin() is called on each slave server.
88 // The tree argument is deprecated (on PROOF 0 is passed).
89
90 TString option = GetOption();
91
92 // Determine the action type
93 fAction = GetAction(fInput);
94
95 // Get the number of events
96 TParameter<Long64_t> *a = (TParameter<Long64_t> *) fInput->FindObject("ProofAux_NEvents");
97 if (a) fNEvents = a->GetVal();
98
99 // Create lists
100 fMainList = new TList;
101 if (gProofServ) fMainList->SetName(TString::Format("MainList-%s", gProofServ->GetOrdinal()));
102 if (fAction < 2) {
103 fFriendList = new TList;
104 if (gProofServ) fFriendList->SetName(TString::Format("FriendList-%s", gProofServ->GetOrdinal()));
105 }
106}
107
108//_____________________________________________________________________________
109Bool_t ProofAux::Process(Long64_t entry)
110{
111 // The Process() function is called for each entry in the tree (or possibly
112 // keyed object in the case of PROOF) to be processed. The entry argument
113 // specifies which entry in the currently loaded tree is to be processed.
114 // It can be passed to either ProofAux::GetEntry() or TBranch::GetEntry()
115 // to read either all or the required parts of the data. When processing
116 // keyed objects with PROOF, the object is already loaded and is available
117 // via the fObject pointer.
118 //
119 // This function should contain the "body" of the analysis. It can contain
120 // simple or elaborate selection criteria, run algorithms on the data
121 // of the event and typically fill histograms.
122 //
123 // The processing can be stopped by calling Abort().
124 //
125 // Use fStatus to set the return value of TTree::Process().
126 //
127 // The return value is currently not used.
128
129 // Nothing to do if the action if not defined
130 if (fAction < 0) {
131 Error("Process", "action not specified!");
132 return kFALSE;
133 }
134
135 // Link to current element, if any
136 TDSetElement *fCurrent = 0;
137 TPair *elemPair = 0;
138 if (fInput && (elemPair = dynamic_cast<TPair *>(fInput->FindObject("PROOF_CurrentElement")))) {
139 if ((fCurrent = dynamic_cast<TDSetElement *>(elemPair->Value()))) {
140 Info("Process", "entry %lld: file: '%s'", entry, fCurrent->GetName());
141 } else {
142 Error("Process", "entry %lld: no file specified!", entry);
143 return kFALSE;
144 }
145 }
146
147 // Act now
148 if (fAction == 0) {
149 TString fnt;
150 // Generate the TTree and save it in the specified file
151 if (GenerateTree(fCurrent->GetName(), fNEvents, fnt) != 0) {
152 Error("Process", "problems generating tree (%lld, %s, %lld)",
153 entry, fCurrent->GetName(), fNEvents);
154 return kFALSE;
155 }
156 // The output filename
157 TString fnf(fnt);
158 TString xf = gSystem->BaseName(fnf);
159 fnf = gSystem->GetDirName(fnf);
160 if (xf.Contains("tree")) {
161 xf.ReplaceAll("tree", "friend");
162 } else {
163 if (xf.EndsWith(".root")) {
164 xf.ReplaceAll(".root", "_friend.root");
165 } else {
166 xf += "_friend";
167 }
168 }
169 fnf += TString::Format("/%s", xf.Data());
170 // Generate the TTree friend and save it in the specified file
171 if (GenerateFriend(fnt, fnf) != 0) {
172 Error("Process", "problems generating friend tree for %s (%s)",
173 fCurrent->GetName(), fnt.Data());
174 return kFALSE;
175 }
176 } else if (fAction == 1) {
177 TString fnt;
178 // Generate the TTree and save it in the specified file
179 if (GenerateTree(fCurrent->GetName(), fNEvents, fnt) != 0) {
180 Error("Process", "problems generating tree (%lld, %s, %lld)",
181 entry, fCurrent->GetName(), fNEvents);
182 return kFALSE;
183 }
184 // Generate the TTree friend and save it in the specified file
185 if (GenerateFriend(fnt) != 0) {
186 Error("Process", "problems generating friend tree for %s (%s)",
187 fCurrent->GetName(), fnt.Data());
188 return kFALSE;
189 }
190 } else if (fAction == 2) {
191 TString fnt;
192 // Generate the TTree and save it in the specified file
193 if (GenerateTree(fCurrent->GetName(), fNEvents, fnt) != 0) {
194 Error("Process", "problems generating tree (%lld, %s, %lld)",
195 entry, fCurrent->GetName(), fNEvents);
196 return kFALSE;
197 }
198 } else {
199 // Unknown action
200 Warning("Process", "do not know how to process action %d - do nothing", fAction);
201 return kFALSE;
202 }
203
204 return kTRUE;
205}
206
207//_____________________________________________________________________________
208void ProofAux::SlaveTerminate()
209{
210 // The SlaveTerminate() function is called after all entries or objects
211 // have been processed. When running with PROOF SlaveTerminate() is called
212 // on each slave server.
213
214 if (fMainList && fMainList->GetSize() > 0) fOutput->Add(fMainList);
215 if (fFriendList && fFriendList->GetSize() > 0) fOutput->Add(fFriendList);
216}
217
218//_____________________________________________________________________________
219void ProofAux::Terminate()
220{
221 // The Terminate() function is the last function to be called during
222 // a query. It always runs on the client, it can be used to present
223 // the results graphically or save the results to file.
224
225}
226
227//_____________________________________________________________________________
228Int_t ProofAux::GenerateTree(const char *fnt, Long64_t ent, TString &fn)
229{
230 // Generate the main tree for the 'friends' tutorial; the tree is called
231 // 'Tmain', has 'ent' entries and is saved to file 'fnt'.
232 // The full file path is returned in 'fn'.
233 // Return 0 on success, -1 on error.
234
235 Int_t rc = -1;
236
237 // Check the filename
238 fn = fnt;
239 if (fn.IsNull()) {
240 Error("GenerateTree", "file name undefined!");
241 return rc;
242 }
243 TUrl uu(fn, kTRUE);
244 if (!strcmp(uu.GetProtocol(), "file") && !fn.BeginsWith("/")) {
245 // Local file with relative path: create under the data directory
246 if (!gProofServ ||
247 !(gProofServ->GetDataDir()) || strlen(gProofServ->GetDataDir()) <= 0) {
248 Error("GenerateTree", "data directory undefined!");
249 return rc;
250 }
251 // Insert data directory
253 // Make sure the directory exists
254 TString dir = gSystem->GetDirName(fn);
256 if (gSystem->mkdir(dir, kTRUE) != 0) {
257 Error("GenerateTree", "problems creating directory %s to store the file", dir.Data());
258 return rc;
259 }
260 }
261 }
262
263 // Create the file
264 TDirectory *savedir = gDirectory;
265 TFile *f = new TFile(fn, "RECREATE");
266 if (!f || f->IsZombie()) {
267 Error("GenerateTree", "problems opening file %s", fn.Data());
268 return rc;
269 }
270 savedir->cd();
271
272 // Create sub-drirectory, if required
273 TDirectory *destdir = f;
274 if (!fDir.IsNull()) {
275 if (f->mkdir(fDir.Data())) {
276 Info("GenerateTree", "sub-directory '%s' successfully created", fDir.Data());
277 f->cd(fDir.Data());
278 destdir = gDirectory;
279 } else {
280 Error("GenerateTree", "creating sub-directory '%s'", fDir.Data());
281 f->Close();
282 delete f;
283 return rc;
284 }
285 }
286
287 rc = 0;
288
289 // Create the tree
290 TTree *T = new TTree("Tmain","Main tree for tutorial friends");
291 T->SetDirectory(destdir);
292 Int_t Run = 1;
293 T->Branch("Run",&Run,"Run/I");
294 Long64_t Event = 0;
295 T->Branch("Event",&Event,"Event/L");
296 Float_t x = 0., y = 0., z = 0.;
297 T->Branch("x",&x,"x/F");
298 T->Branch("y",&y,"y/F");
299 T->Branch("z",&z,"z/F");
300 TRandom r;
301 for (Long64_t i = 0; i < ent; i++) {
302 if (i > 0 && i%1000 == 0) Run++;
303 Event = i;
304 x = r.Gaus(10,1);
305 y = r.Gaus(20,2);
306 z = r.Landau(2,1);
307 T->Fill();
308 }
309 T->Print();
310 destdir->cd();
311 T->Write();
312 T->SetDirectory(0);
313 f->Close();
314 delete f;
315 delete T;
316
317 // Notify success
318 Info("GenerateTree", "file '%s' successfully created", fn.Data());
319
320 // Add to the list
321 TString fds(fn);
322 if (!strcmp(uu.GetProtocol(), "file")) {
323 if (gSystem->Getenv("LOCALDATASERVER")) {
324 if (strcmp(TUrl(gSystem->Getenv("LOCALDATASERVER"), kTRUE).GetProtocol(), "file"))
325 fds.Insert(0, TString::Format("%s/", gSystem->Getenv("LOCALDATASERVER")));
326 } else {
327 fds.Insert(0, TString::Format("root://%s/", gSystem->HostName()));
328 }
329 }
330 fMainList->Add(new TObjString(fds));
331
332 // Done
333 return rc;
334}
335
336//_____________________________________________________________________________
337Int_t ProofAux::GenerateFriend(const char *fnt, const char *fnf)
338{
339 // Generate the friend tree for the main tree in the 'friends' tutorial fetched
340 // from 'fnt'.
341 // the tree is called 'Tfriend', has the same number of entries as the main
342 // tree and is saved to file 'fnf'. If 'fnf' is not defined the filename is
343 // derived from 'fnt' either replacing 'tree' with 'friend', or adding '_friend'
344 // before the '.root' extension.
345 // Return 0 on success, -1 on error.
346
347 Int_t rc = -1;
348 // Check the input filename
349 TString fin(fnt);
350 if (fin.IsNull()) {
351 Error("GenerateFriend", "file name for the main tree undefined!");
352 return rc;
353 }
354 // Make sure that the file can be read
356 Error("GenerateFriend", "input file does not exist or cannot be read: %s", fin.Data());
357 return rc;
358 }
359
360 // File handlers
361 Bool_t sameFile = kTRUE;
362 const char *openMain = "UPDATE";
363
364 // The output filename
365 TString fout(fnf);
366 if (!fout.IsNull()) {
367 sameFile = kFALSE;
368 openMain = "READ";
369 // Make sure the directory exists
370 TString dir = gSystem->GetDirName(fout);
372 if (gSystem->mkdir(dir, kTRUE) != 0) {
373 Error("GenerateFriend", "problems creating directory %s to store the file", dir.Data());
374 return rc;
375 }
376 }
377 } else {
378 // We set the same name
379 fout = fin;
380 }
381
382 // Get main tree
383 TFile *fi = TFile::Open(fin, openMain);
384 if (!fi || fi->IsZombie()) {
385 Error("GenerateFriend", "problems opening input file %s", fin.Data());
386 return rc;
387 }
388 TTree *Tin = (TTree *) fi->Get("Tmain");
389 if (!Tin) {
390 Error("GenerateFriend", "problems getting tree 'Tmain' from file %s", fin.Data());
391 delete fi;
392 return rc;
393 }
394 // Set branches
395 Float_t x, y, z;
396 Tin->SetBranchAddress("x", &x);
397 Tin->SetBranchAddress("y", &y);
398 Tin->SetBranchAddress("z", &z);
399 TBranch *b_x = Tin->GetBranch("x");
400 TBranch *b_y = Tin->GetBranch("y");
401 TBranch *b_z = Tin->GetBranch("z");
402
403 TDirectory* savedir = gDirectory;
404 // Create output file
405 TFile *fo = 0;
406 if (!sameFile) {
407 fo = new TFile(fout, "RECREATE");
408 if (!fo || fo->IsZombie()) {
409 Error("GenerateFriend", "problems opening file %s", fout.Data());
410 delete fi;
411 return rc;
412 }
413 savedir->cd();
414 } else {
415 // Same file
416 fo = fi;
417 }
418 rc = 0;
419
420 // Create the tree
421 TTree *Tfrnd = new TTree("Tfrnd", "Friend tree for tutorial 'friends'");
422 Tfrnd->SetDirectory(fo);
423 Float_t r = 0;
424 Tfrnd->Branch("r",&r,"r/F");
425 Long64_t ent = Tin->GetEntries();
426 for (Long64_t i = 0; i < ent; i++) {
427 b_x->GetEntry(i);
428 b_y->GetEntry(i);
429 b_z->GetEntry(i);
430 r = TMath::Sqrt(x*x + y*y + z*z);
431 Tfrnd->Fill();
432 }
433 if (!sameFile) {
434 fi->Close();
435 delete fi;
436 }
437 Tfrnd->Print();
438 fo->cd();
439 Tfrnd->Write();
440 Tfrnd->SetDirectory(0);
441 fo->Close();
442 delete fo;
443 delete Tfrnd;
444
445 // Notify success
446 Info("GenerateFriend", "friend file '%s' successfully created", fout.Data());
447
448 // Add to the list
449 TUrl uu(fout);
450 if (!strcmp(uu.GetProtocol(), "file")) {
451 if (gSystem->Getenv("LOCALDATASERVER")) {
452 if (strcmp(TUrl(gSystem->Getenv("LOCALDATASERVER"), kTRUE).GetProtocol(), "file"))
453 fout.Insert(0, TString::Format("%s/", gSystem->Getenv("LOCALDATASERVER")));
454 } else {
455 fout.Insert(0, TString::Format("root://%s/", gSystem->HostName()));
456 }
457 }
458 fFriendList->Add(new TObjString(fout));
459
460 // Done
461 return rc;
462}
ROOT::R::TRInterface & r
Definition Object.C:4
Selector used for auxiliary actions in the PROOF tutorials.
#define f(i)
Definition RSha256.hxx:104
#define a(i)
Definition RSha256.hxx:99
const Ssiz_t kNPOS
Definition RtypesCore.h:124
int Int_t
Definition RtypesCore.h:45
int Ssiz_t
Definition RtypesCore.h:67
const Bool_t kFALSE
Definition RtypesCore.h:101
bool Bool_t
Definition RtypesCore.h:63
long long Long64_t
Definition RtypesCore.h:80
float Float_t
Definition RtypesCore.h:57
const Bool_t kTRUE
Definition RtypesCore.h:100
#define gDirectory
Definition TDirectory.h:385
void Info(const char *location, const char *msgfmt,...)
Use this function for informational messages.
Definition TError.cxx:220
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:187
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:231
R__EXTERN TProofServ * gProofServ
Definition TProofServ.h:347
@ kReadPermission
Definition TSystem.h:47
@ kWritePermission
Definition TSystem.h:46
R__EXTERN TSystem * gSystem
Definition TSystem.h:559
A TTree is a list of TBranches.
Definition TBranch.h:89
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all leaves of entry and return total number of bytes read.
Definition TBranch.cxx:1652
void SetName(const char *name)
Manages an element of a TDSet.
Definition TDSet.h:66
Bool_t cd() override
Change current directory to "this" directory.
TObject * Get(const char *namecycle) override
Return pointer to object identified by namecycle.
Describe directory structure in memory.
Definition TDirectory.h:45
virtual Bool_t cd()
Change current directory to "this" directory.
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition TFile.h:54
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition TFile.cxx:4025
void Close(Option_t *option="") override
Close a file.
Definition TFile.cxx:899
A doubly linked list.
Definition TList.h:38
virtual TObject * FindObject(const char *name) const
Find an object in this list using its name.
Definition TList.cxx:578
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
virtual const char * GetTitle() const
Returns title of object.
Definition TNamed.h:48
virtual const char * GetName() const
Returns name of object.
Definition TNamed.h:47
Collectable string class.
Definition TObjString.h:28
virtual TObject * FindObject(const char *name) const
Must be redefined in derived classes.
Definition TObject.cxx:393
R__ALWAYS_INLINE Bool_t IsZombie() const
Definition TObject.h:153
Class used by TMap to store (key,value) pairs.
Definition TMap.h:102
TObject * Value() const
Definition TMap.h:121
Named parameter, streamable and storable.
Definition TParameter.h:35
const char * GetOrdinal() const
Definition TProofServ.h:253
const char * GetDataDir() const
Definition TProofServ.h:250
This is the base class for the ROOT Random number generators.
Definition TRandom.h:27
Basic string class.
Definition TString.h:136
TString & Insert(Ssiz_t pos, const char *s)
Definition TString.h:649
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
Definition TString.cxx:2202
const char * Data() const
Definition TString.h:369
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition TString.h:692
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition TString.h:615
Bool_t IsNull() const
Definition TString.h:407
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2336
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:624
virtual const char * Getenv(const char *env)
Get environment variable.
Definition TSystem.cxx:1663
virtual int mkdir(const char *name, Bool_t recursive=kFALSE)
Make a file system directory.
Definition TSystem.cxx:907
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition TSystem.cxx:1296
virtual const char * BaseName(const char *pathname)
Base name of a file name. Base name of /user/root is root.
Definition TSystem.cxx:935
virtual const char * HostName()
Return the system's host name.
Definition TSystem.cxx:306
virtual TString GetDirName(const char *pathname)
Return the directory name in pathname.
Definition TSystem.cxx:1032
A TTree represents a columnar dataset.
Definition TTree.h:79
virtual Int_t Fill()
Fill all branches.
Definition TTree.cxx:4594
virtual TBranch * GetBranch(const char *name)
Return pointer to the branch with the given name in this tree or its friends.
Definition TTree.cxx:5279
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
Definition TTree.cxx:8356
virtual void SetDirectory(TDirectory *dir)
Change the tree's directory.
Definition TTree.cxx:8932
virtual Long64_t GetEntries() const
Definition TTree.h:460
virtual void Print(Option_t *option="") const
Print a summary of the tree contents.
Definition TTree.cxx:7203
TBranch * Branch(const char *name, T *obj, Int_t bufsize=32000, Int_t splitlevel=99)
Add a new branch, and infer the data type from the type of obj being passed.
Definition TTree.h:350
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition TTree.cxx:9693
This class represents a WWW compatible URL.
Definition TUrl.h:33
const char * GetProtocol() const
Definition TUrl.h:64
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
double T(double x)
Double_t Sqrt(Double_t x)
Definition TMath.h:641