]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ANALYSIS/AliTagAnalysis.cxx
Leaks darned.
[u/mrichter/AliRoot.git] / ANALYSIS / AliTagAnalysis.cxx
1 /**************************************************************************
2  * Author: Panos Christakoglou.                                           *
3  * Contributors are mentioned in the code where appropriate.              *
4  *                                                                        *
5  * Permission to use, copy, modify and distribute this software and its   *
6  * documentation strictly for non-commercial purposes is hereby granted   *
7  * without fee, provided that the above copyright notice appears in all   *
8  * copies and that both the copyright notice and this permission notice   *
9  * appear in the supporting documentation. The authors make no claims     *
10  * about the suitability of this software for any purpose. It is          *
11  * provided "as is" without express or implied warranty.                  *
12  **************************************************************************/
13
14 /* $Id$ */
15
16 //-----------------------------------------------------------------
17 //           AliTagAnalysis class
18 //   This is the class to deal with the tag analysis
19 //   Origin: Panos Christakoglou, UOA-CERN, Panos.Christakoglou@cern.ch
20 //-----------------------------------------------------------------
21
22 //ROOT
23 #include <Riostream.h>
24 #include <TSystem.h>
25 #include <TChain.h>
26 #include <TFile.h>
27 #include <TEventList.h>
28 #include <TEntryList.h>
29 #include <TTreeFormula.h>
30
31 //ROOT-AliEn
32 #include <TGridResult.h>
33
34 #include "AliLog.h"
35
36 #include "AliRunTag.h"
37 #include "AliEventTag.h"
38 #include "AliTagAnalysis.h"
39 #include "AliEventTagCuts.h"
40 #include "AliDetectorTagCuts.h"
41 #include "AliLHCTagCuts.h"
42 #include "AliRunTagCuts.h"
43 #include "AliXMLCollection.h"
44
45 class TTree;
46
47 ClassImp(AliTagAnalysis)
48
49 //___________________________________________________________________________
50 AliTagAnalysis::AliTagAnalysis(): 
51   TObject(),
52   ftagresult(0x0),
53   fTagDirName(),
54   fChain(0x0),
55   fAnalysisType(),
56   fGlobalList(0) {
57   //Default constructor for a AliTagAnalysis
58 }
59
60 //___________________________________________________________________________
61 AliTagAnalysis::AliTagAnalysis(const char* type): 
62   TObject(),
63   ftagresult(0x0),
64   fTagDirName(),
65   fChain(0x0),
66   fAnalysisType(type),
67   fGlobalList(0) {
68   //constructor for a AliTagAnalysis
69 }
70
71 //___________________________________________________________________________
72 AliTagAnalysis::~AliTagAnalysis() {
73   //Default destructor for a AliTagAnalysis
74   if(ftagresult) delete ftagresult;
75   if(fChain) delete fChain;
76   if(fGlobalList) delete fGlobalList;
77 }
78
79 //___________________________________________________________________________
80 Bool_t  AliTagAnalysis::AddTagsFile(const char *alienUrl) {
81   // Add a single tags file to the chain
82
83   Bool_t rv = kTRUE ;
84
85   if (! fChain) fChain = new TChain("T");
86
87   TFile *f = TFile::Open(alienUrl,"READ");
88   fChain->Add(alienUrl);
89   AliInfo(Form("Chained tag files: %d ",fChain->GetEntries()));
90   delete f;
91
92   if (fChain->GetEntries() == 0 )
93     rv = kFALSE ;
94
95   return rv ;
96 }
97
98 //___________________________________________________________________________
99 void AliTagAnalysis::ChainLocalTags(const char *dirname) {
100   //Searches the entries of the provided direcory
101   //Chains the tags that are stored locally
102   fTagDirName = dirname;
103   TString fTagFilename;
104   
105   if (! fChain)  fChain = new TChain("T");
106   const char * tagPattern = 0x0;
107   if(fAnalysisType == "ESD") tagPattern = "ESD.tag.root";
108   else if(fAnalysisType == "AOD") tagPattern = "AOD.tag.root";
109   else AliFatal("Only ESD and AOD type is implemented!!!");
110
111   // Open the working directory
112   void * dirp = gSystem->OpenDirectory(fTagDirName);
113   const char * name = 0x0;
114   // Add all files matching *pattern* to the chain
115   while((name = gSystem->GetDirEntry(dirp))) {
116     if (strstr(name,tagPattern)) { 
117       fTagFilename = fTagDirName;
118       fTagFilename += "/";
119       fTagFilename += name;
120                 
121       fChain->Add(fTagFilename);  
122       printf("Tag file %s\n", fTagFilename.Data());
123       
124     }//pattern check
125   }//directory loop
126   AliInfo(Form("Chained tag files: %d ",fChain->GetEntries()));
127   fChain->ls();
128   
129 }
130
131
132 //___________________________________________________________________________
133 TChain * AliTagAnalysis::ChainGridTags(TGridResult *res) {
134   //Loops overs the entries of the TGridResult
135   //Chains the tags that are stored in the GRID
136   ftagresult = res;
137   Int_t nEntries = ftagresult->GetEntries();
138  
139   if (! fChain)  fChain = new TChain("T");
140
141   TString gridname = "alien://";
142   TString alienUrl;
143  
144   for(Int_t i = 0; i < nEntries; i++) {
145     alienUrl = ftagresult->GetKey(i,"turl");
146     fChain->Add(alienUrl);
147   }//grid result loop
148   return fChain;
149 }
150
151
152 //___________________________________________________________________________
153 TChain *AliTagAnalysis::QueryTags(AliRunTagCuts *runTagCuts, 
154                                   AliLHCTagCuts *lhcTagCuts, 
155                                   AliDetectorTagCuts *detTagCuts, 
156                                   AliEventTagCuts *evTagCuts) {
157   //Queries the tag chain using the defined 
158   //event tag cuts from the AliEventTagCuts object
159   //and returns a TChain along with the associated TEventList
160   AliInfo(Form("Querying the tags........"));
161
162   Bool_t aod = kFALSE;
163   TString aliceFile;
164   if(fAnalysisType == "ESD") aliceFile = "esdTree";
165   else if(fAnalysisType == "AOD") {
166       aliceFile = "aodTree";
167       aod = kTRUE;
168   }
169   else AliFatal("Only ESD and AOD type is implemented!!!");
170
171   //ESD file chain
172   TChain *esdChain = new TChain(aliceFile.Data());
173   //global entry list
174   fGlobalList = new TEntryList();
175   
176   //Defining tag objects
177   AliRunTag   *tag     = new AliRunTag;
178   AliEventTag *evTag   = new AliEventTag;
179   fChain->SetBranchAddress("AliTAG",&tag);
180
181   TString guid;
182   TString turl;
183   TString path;
184
185   TTree*      cTree     = 0; 
186   TEntryList* localList = new TEntryList();
187
188   Int_t iAccepted = 0;
189   Int_t iev       = 0;
190   Int_t ientry    = 0;
191   Int_t cEntries  = 0;
192   
193   for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
194     fChain->GetEntry(iTagFiles);
195     TTree* tree = fChain->GetTree();
196     if (cTree != tree) {
197         // Fix for aod tags: for each tree in the chain, merge the entries
198         cTree    = tree;
199         cEntries = tree->GetEntries();
200         iev      = 0;
201         ientry   = 0;
202     }
203
204     if(runTagCuts->IsAccepted(tag)) {
205       if(lhcTagCuts->IsAccepted(tag->GetLHCTag())) {
206         if(detTagCuts->IsAccepted(tag->GetDetectorTags())) {
207           if ((iev == 0) || !aod) localList->Reset();
208           Int_t iEvents = tag->GetNEvents();
209           const TClonesArray *tagList = tag->GetEventTags();
210           for(Int_t i = 0; i < iEvents; i++) {
211             evTag = (AliEventTag *) tagList->At(i);
212             guid = evTag->GetGUID(); 
213             turl = evTag->GetTURL(); 
214             path = evTag->GetPath();
215             localList->SetTreeName(aliceFile.Data());
216             if(turl!="") localList->SetFileName(turl.Data());
217             else localList->SetFileName(path.Data());
218
219             if(evTagCuts->IsAccepted(evTag)) {
220                 if(aod) localList->Enter(iev);
221                 else localList->Enter(i);
222             }
223             iev++;
224           }//event loop
225           if ((ientry == cEntries-1) || !aod) {
226               iAccepted += localList->GetN();
227               if(path != "") esdChain->AddFile(path);
228               else if(turl != "") esdChain->AddFile(turl);
229               fGlobalList->Add(localList);
230           }
231         }//detector tag cuts
232       }//lhc tag cuts
233     }//run tags cut
234     tag->Clear();
235     ientry++;
236   }//tag file loop
237   AliInfo(Form("Accepted events: %d",iAccepted));
238   esdChain->ls();
239   esdChain->SetEntryList(fGlobalList,"ne");
240   delete tag;
241   
242   return esdChain;
243 }
244
245 //___________________________________________________________________________
246 TChain *AliTagAnalysis::QueryTags(const char *fRunCut, 
247                                   const char *fLHCCut, 
248                                   const char *fDetectorCut, 
249                                   const char *fEventCut) {       
250   //Queries the tag chain using the defined      
251   //event tag cuts from the AliEventTagCuts object       
252   //and returns a TChain along with the associated TEventList    
253   AliInfo(Form("Querying the tags........"));    
254   
255   Bool_t aod = kFALSE;
256   TString aliceFile;
257   if(fAnalysisType == "ESD") aliceFile = "esdTree";
258   else if(fAnalysisType == "AOD") {
259       aliceFile = "aodTree";
260       aod = kTRUE;
261   }
262   else AliFatal("Only ESD and AOD type is implemented!!!");
263
264   //ESD file chain
265   TChain *esdChain = new TChain(aliceFile.Data());
266   //global entry list
267   fGlobalList = new TEntryList();
268   
269   //Defining tag objects         
270   AliRunTag   *tag   = new AliRunTag;    
271   AliEventTag *evTag = new AliEventTag;          
272   fChain->SetBranchAddress("AliTAG",&tag);       
273   
274   TString guid;          
275   TString turl;          
276   TString path;          
277   
278   TTreeFormula *fRunFormula = new TTreeFormula("fRun",fRunCut,fChain);   
279   TTreeFormula *fLHCFormula = new TTreeFormula("fLHC",fLHCCut,fChain);   
280   TTreeFormula *fDetectorFormula = new TTreeFormula("fDetector",fDetectorCut,fChain);
281   TTreeFormula *fEventFormula = new TTreeFormula("fEvent",fEventCut,fChain);
282   
283   TEntryList* localList = new TEntryList();
284
285   Int_t iev       = 0;
286   Int_t ientry    = 0;
287   Int_t cEntries  = 0;
288   Int_t current   = -1;          
289   Int_t iAccepted = 0;   
290
291   for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
292     fChain->GetEntry(iTagFiles);         
293     if (current != fChain->GetTreeNumber()) {    
294       fRunFormula->UpdateFormulaLeaves();        
295       fLHCFormula->UpdateFormulaLeaves();        
296       fDetectorFormula->UpdateFormulaLeaves();   
297       fEventFormula->UpdateFormulaLeaves();      
298       // Fix for aod tags: for each tree in the chain, merge the entries
299       cEntries = (fChain->GetTree())->GetEntries();
300       iev      = 0;
301       ientry   = 0;
302       //
303       current = fChain->GetTreeNumber();         
304     }    
305     if(fRunFormula->EvalInstance(iTagFiles) == 1) {      
306       if(fLHCFormula->EvalInstance(iTagFiles) == 1) {    
307         if(fDetectorFormula->EvalInstance(iTagFiles) == 1) {
308           if ((iev == 0) || !aod) localList->Reset();    
309           Int_t iEvents = fEventFormula->GetNdata();     
310           const TClonesArray *tagList = tag->GetEventTags();     
311           for(Int_t i = 0; i < iEvents; i++) {   
312             evTag = (AliEventTag *) tagList->At(i);      
313             guid = evTag->GetGUID();     
314             turl = evTag->GetTURL();     
315             path = evTag->GetPath();     
316             localList->SetTreeName(aliceFile.Data());
317             localList->SetFileName(turl.Data());
318             if(fEventFormula->EvalInstance(i) == 1) {
319                 if(aod) localList->Enter(iev);
320                 else localList->Enter(i);
321             }
322             iev++;
323           }//event loop          
324
325           if ((ientry == cEntries-1) || !aod) {  
326               if(path != "") esdChain->AddFile(path);    
327               else if(turl != "") esdChain->AddFile(turl);       
328               fGlobalList->Add(localList);
329               iAccepted += localList->GetN();
330           }
331         }//detector tag cuts
332       }//lhc tag cuts
333     }//run tag cut       
334     tag->Clear();
335     ientry++;
336   }//tag file loop       
337   AliInfo(Form("Accepted events: %d",iAccepted));        
338   esdChain->SetEntryList(fGlobalList,"ne");      
339
340   delete tag;
341   return esdChain;       
342 }
343
344 //___________________________________________________________________________
345 Bool_t AliTagAnalysis::CreateXMLCollection(const char* name, 
346                                            AliRunTagCuts *runTagCuts, 
347                                            AliLHCTagCuts *lhcTagCuts, 
348                                            AliDetectorTagCuts *detTagCuts, 
349                                            AliEventTagCuts *evTagCuts) {
350   //Queries the tag chain using the defined 
351   //event tag cuts from the AliEventTagCuts object
352   //and returns a XML collection
353   AliInfo(Form("Creating the collection........"));
354
355   Bool_t aod = kFALSE;
356   if(fAnalysisType == "AOD") aod = kTRUE;
357
358
359   AliXMLCollection *collection = new AliXMLCollection();
360   collection->SetCollectionName(name);
361   collection->WriteHeader();
362
363   TString guid;
364   TString turl;
365   TString lfn;
366   
367   TTree*      cTree = 0; 
368   TEntryList* localList = new TEntryList();
369   Int_t iAccepted = 0;
370   Int_t iev       = 0;
371   Int_t ientry    = 0;
372   Int_t cEntries  = 0;
373
374   //Defining tag objects
375   AliRunTag   *tag   = new AliRunTag;
376   AliEventTag *evTag = new AliEventTag;
377   fChain->SetBranchAddress("AliTAG",&tag);
378
379   for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
380
381     fChain->GetEntry(iTagFiles);
382     TTree* tree = fChain->GetTree();
383     if (cTree != tree) {
384         // Fix for aod tags: for each tree in the chain, merge the entries
385         cTree    = tree;
386         cEntries = tree->GetEntries();
387         iev      = 0;
388         ientry   = 0;
389     }
390     //Event list
391     if ((iev == 0) || !aod) localList->Reset();
392     if(runTagCuts->IsAccepted(tag)) {
393       if(lhcTagCuts->IsAccepted(tag->GetLHCTag())) {
394         if(detTagCuts->IsAccepted(tag->GetDetectorTags())) {
395           Int_t iEvents = tag->GetNEvents();
396           const TClonesArray *tagList = tag->GetEventTags();
397           for(Int_t i = 0; i < iEvents; i++) {
398             evTag = (AliEventTag *) tagList->At(i);
399             guid = evTag->GetGUID(); 
400             turl = evTag->GetTURL(); 
401             lfn = turl(8,turl.Length());
402             if(evTagCuts->IsAccepted(evTag)) {
403                 if(aod) localList->Enter(iev);
404                 else localList->Enter(i);
405             }
406             iev++;
407           }//event loop
408           if ((ientry == cEntries-1) || !aod) {
409               collection->WriteBody(iTagFiles+1,guid,lfn,turl,localList);
410               iAccepted += localList->GetN();
411           }
412         }//detector tag cuts
413       }//lhc tag cuts 
414     }//run tag cuts
415     tag->Clear();
416     ientry++;
417   }//tag file loop
418   collection->Export();
419   
420   delete tag;
421   return kTRUE;
422 }
423
424 //___________________________________________________________________________
425 Bool_t AliTagAnalysis::CreateXMLCollection(const char* name, 
426                                            const char *fRunCut, 
427                                            const char *fLHCCut, 
428                                            const char *fDetectorCut, 
429                                            const char *fEventCut) {
430   //Queries the tag chain using the defined 
431   //event tag cuts from the AliEventTagCuts object
432   //and returns a XML collection
433   AliInfo(Form("Creating the collection........"));
434
435   Bool_t aod = kFALSE;
436   if(fAnalysisType == "AOD") aod = kTRUE;
437
438   AliXMLCollection *collection = new AliXMLCollection();
439   collection->SetCollectionName(name);
440   collection->WriteHeader();
441
442   TString guid;
443   TString turl;
444   TString lfn;
445   TEntryList* localList = new TEntryList();
446   
447
448   Int_t iAccepted = 0;
449   Int_t iev       = 0;
450   Int_t ientry    = 0;
451   Int_t cEntries  = 0;
452
453   //Defining tag objects
454   AliRunTag *tag     = new AliRunTag;
455   AliEventTag *evTag = new AliEventTag;
456   fChain->SetBranchAddress("AliTAG",&tag);
457
458   TTreeFormula *fRunFormula = new TTreeFormula("fRun",fRunCut,fChain);
459   TTreeFormula *fLHCFormula = new TTreeFormula("fLHC",fLHCCut,fChain);   
460   TTreeFormula *fDetectorFormula = new TTreeFormula("fDetector",fDetectorCut,fChain);
461   TTreeFormula *fEventFormula = new TTreeFormula("fEvent",fEventCut,fChain);
462
463   Int_t current = -1;
464   for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
465
466     fChain->GetEntry(iTagFiles);
467     if (current != fChain->GetTreeNumber()) {
468       fRunFormula->UpdateFormulaLeaves();
469       fLHCFormula->UpdateFormulaLeaves();
470       fDetectorFormula->UpdateFormulaLeaves();
471       fEventFormula->UpdateFormulaLeaves();
472       // Fix for aod tags: for each tree in the chain, merge the entries
473       cEntries = (fChain->GetTree())->GetEntries();
474       iev      = 0;
475       ientry   = 0;
476       //
477       current = fChain->GetTreeNumber();
478     }
479     //Event list
480     if ((iev == 0) || !aod) localList->Reset();
481     if(fRunFormula->EvalInstance(iTagFiles) == 1) {
482       if(fLHCFormula->EvalInstance(iTagFiles) == 1) {    
483         if(fDetectorFormula->EvalInstance(iTagFiles) == 1) {     
484           Int_t iEvents = fEventFormula->GetNdata();
485           const TClonesArray *tagList = tag->GetEventTags();
486           for(Int_t i = 0; i < iEvents; i++) {
487             evTag = (AliEventTag *) tagList->At(i);
488             guid = evTag->GetGUID(); 
489             turl = evTag->GetTURL(); 
490             lfn = turl(8,turl.Length());
491             if(fEventFormula->EvalInstance(i) == 1) {
492                 if(aod) localList->Enter(iev);
493                 else localList->Enter(i);
494             }
495             iev++;
496           }//event loop
497           if ((ientry == cEntries-1) || !aod) {
498               collection->WriteBody(iTagFiles+1,guid,lfn,turl,localList);
499               iAccepted += localList->GetN();
500           }
501         }//detector tag cuts
502       }//lhc tag cuts 
503     }//run tag cuts
504     ientry++;
505   }//tag file loop
506   collection->Export();
507
508   delete tag;
509   return kTRUE;
510 }
511
512 //___________________________________________________________________________
513 TChain *AliTagAnalysis::GetInputChain(const char* system, const char *wn) {
514   //returns the chain+event list - used in batch sessions
515   // this function will be removed once the new root 
516   // improvements are committed
517   TString fsystem = system;
518   Int_t iAccepted = 0;
519
520   TChain *fAnalysisChain = 0;
521   if(fAnalysisType == "ESD") fAnalysisChain = new TChain("esdTree");
522   else if(fAnalysisType == "AOD") fAnalysisChain = new TChain("aodTree");
523   else AliFatal("Only ESD and AOD type is implemented!!!");
524   
525   //Event list
526   TEventList *fEventList = new TEventList();
527   AliXMLCollection *collection = AliXMLCollection::Open(wn);
528
529   collection->Reset();
530   while (collection->Next()) {
531     AliInfo(Form("Adding: %s",collection->GetTURL("")));
532     fAnalysisChain->Add(collection->GetTURL(""));
533     TEntryList *list = (TEntryList *)collection->GetEventList("");
534     for(Int_t i = 0; i < list->GetN(); i++) fEventList->Enter(iAccepted+list->GetEntry(i));
535
536     if(fsystem == "pp") iAccepted += 100;
537     else if(fsystem == "PbPb") iAccepted += 1;
538   }
539
540   fAnalysisChain->SetEventList(fEventList);
541   
542   AliInfo(Form("Number of selected events: %d",fEventList->GetN()));
543
544   return fAnalysisChain;
545 }
546
547 //___________________________________________________________________________
548 TChain *AliTagAnalysis::GetChainFromCollection(const char* collectionname, 
549                                                const char* treename) {
550   //returns the TChain+TEntryList object- used in batch sessions
551   TString aliceFile = treename;
552   Int_t iAccepted = 0;
553   TChain *fAnalysisChain = 0;
554   if(aliceFile == "esdTree") fAnalysisChain = new TChain("esdTree");
555   else if(aliceFile == "aodTree") fAnalysisChain = new TChain("aodTree");
556   else AliFatal("Inconsistent tree name - use esdTree or aodTree!");
557
558   //Event list
559   fGlobalList = new TEntryList();
560   AliXMLCollection *collection = AliXMLCollection::Open(collectionname);
561
562   collection->Reset();
563   while (collection->Next()) {
564     AliInfo(Form("Adding: %s",collection->GetTURL("")));
565     fAnalysisChain->Add(collection->GetTURL(""));
566     TEntryList *list = (TEntryList *)collection->GetEventList("");
567     list->SetTreeName(aliceFile.Data());
568     list->SetFileName(collection->GetTURL(""));
569     fGlobalList->Add(list);
570     iAccepted += list->GetN();
571   }
572
573   fAnalysisChain->SetEntryList(fGlobalList,"ne");
574   
575   AliInfo(Form("Number of selected events: %d",iAccepted));
576
577   return fAnalysisChain;
578 }