]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ANALYSIS/AliTagAnalysis.cxx
AODtags treared like ESDtags again.
[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 #include <TMap.h>
31
32 //ROOT-AliEn
33 #include <TGridResult.h>
34
35 #include "AliLog.h"
36
37 #include "AliRunTag.h"
38 #include "AliEventTag.h"
39 #include "AliTagAnalysis.h"
40 #include "AliEventTagCuts.h"
41 #include "AliDetectorTagCuts.h"
42 #include "AliLHCTagCuts.h"
43 #include "AliRunTagCuts.h"
44 #include "AliXMLCollection.h"
45
46 class TTree;
47
48 ClassImp(AliTagAnalysis)
49
50 //___________________________________________________________________________
51 AliTagAnalysis::AliTagAnalysis(): 
52   TObject(),
53   ftagresult(0x0),
54   fTagDirName(),
55   fChain(0x0),
56   fAnalysisType(),
57   fGlobalList(0) {
58   //Default constructor for a AliTagAnalysis
59 }
60
61 //___________________________________________________________________________
62 AliTagAnalysis::AliTagAnalysis(const char* type): 
63   TObject(),
64   ftagresult(0x0),
65   fTagDirName(),
66   fChain(0x0),
67   fAnalysisType(type),
68   fGlobalList(0) {
69   //constructor for a AliTagAnalysis
70 }
71
72 //___________________________________________________________________________
73 AliTagAnalysis::~AliTagAnalysis() {
74   //Default destructor for a AliTagAnalysis
75   if(ftagresult) delete ftagresult;
76   if(fChain) delete fChain;
77   if(fGlobalList) delete fGlobalList;
78 }
79
80 //___________________________________________________________________________
81 Bool_t  
82 AliTagAnalysis::AddTagsFile(const char* alienUrl, Bool_t checkFile) 
83 {
84   /// Add a single tags file to the chain
85   ///
86   /// If checkFile=kTRUE (default) the file is opened to check
87   /// it can be and that it contains data.
88   /// It's safer but a lot longer...
89   
90   if (!fChain) fChain = new TChain("T");
91
92   if ( checkFile )
93   {
94     return ( fChain->AddFile(alienUrl,-1) > 0 );
95   }
96   else
97   {
98     return ( fChain->AddFile(alienUrl) > 0 );
99   }
100
101 }
102
103 //___________________________________________________________________________
104 void AliTagAnalysis::ChainLocalTags(const char *dirname) {
105   //Searches the entries of the provided direcory
106   //Chains the tags that are stored locally
107   fTagDirName = dirname;
108   TString fTagFilename;
109   
110   if (! fChain)  fChain = new TChain("T");
111   const char * tagPattern = 0x0;
112   if(fAnalysisType == "ESD") tagPattern = "ESD.tag.root";
113   else if(fAnalysisType == "AOD") tagPattern = "AOD.tag.root";
114   else AliFatal("Only ESD and AOD type is implemented!!!");
115
116   // Open the working directory
117   void * dirp = gSystem->OpenDirectory(fTagDirName);
118   const char * name = 0x0;
119   // Add all files matching *pattern* to the chain
120   while((name = gSystem->GetDirEntry(dirp))) {
121     if (strstr(name,tagPattern)) { 
122       fTagFilename = fTagDirName;
123       fTagFilename += "/";
124       fTagFilename += name;
125                 
126       fChain->Add(fTagFilename);  
127       printf("Tag file %s\n", fTagFilename.Data());
128       
129     }//pattern check
130   }//directory loop
131   //AliInfo(Form("Chained tag files: %d ",fChain->GetEntries()));
132  // AliDebug(Form("Chained tag files: %d ",fChain->GetEntries()));
133   fChain->ls();
134   
135 }
136
137
138 //___________________________________________________________________________
139 TChain * AliTagAnalysis::ChainGridTags(TGridResult *res) {
140   //Loops overs the entries of the TGridResult
141    //Chains the tags that are stored in the GRID
142   ftagresult = res;
143   Int_t nEntries = ftagresult->GetEntries();
144  
145   if (! fChain)  fChain = new TChain("T");
146
147   TString gridname = "alien://";
148   TString alienUrl;
149  
150   for(Int_t i = 0; i < nEntries; i++) {
151     alienUrl = ftagresult->GetKey(i,"turl");
152     fChain->Add(alienUrl);
153   }//grid result loop
154   return fChain;
155 }
156
157
158 //___________________________________________________________________________
159 TChain *AliTagAnalysis::QueryTags(AliRunTagCuts *runTagCuts, 
160                                   AliLHCTagCuts *lhcTagCuts, 
161                                   AliDetectorTagCuts *detTagCuts, 
162                                   AliEventTagCuts *evTagCuts) {
163   //Queries the tag chain using the defined 
164   //event tag cuts from the AliEventTagCuts object
165   //and returns a TChain along with the associated TEventList
166   AliInfo(Form("Querying the tags........"));
167
168   TString aliceFile;
169   if(fAnalysisType == "ESD") aliceFile = "esdTree";
170   else if(fAnalysisType == "AOD") aliceFile = "aodTree";
171   else AliFatal("Only ESD and AOD type is implemented!!!");
172
173   //ESD file chain
174   TChain *esdChain = new TChain(aliceFile.Data());
175   //global entry list
176   fGlobalList = new TEntryList();
177   
178   //Defining tag objects
179   AliRunTag   *tag     = new AliRunTag;
180   AliEventTag *evTag   = new AliEventTag;
181   fChain->SetBranchAddress("AliTAG",&tag);
182
183   TString guid;
184   TString turl;
185   TString path;
186
187   TEntryList* localList = new TEntryList();
188
189   Int_t iAccepted = 0;
190   
191   for(Int_t iEntry = 0; iEntry < fChain->GetEntries(); iEntry++) {
192     fChain->GetEntry(iEntry);
193
194     if(runTagCuts->IsAccepted(tag)) {
195       if(lhcTagCuts->IsAccepted(tag->GetLHCTag())) {
196         if(detTagCuts->IsAccepted(tag->GetDetectorTags())) {
197           localList->Reset();
198           Int_t iEvents = tag->GetNEvents();
199           const TClonesArray *tagList = tag->GetEventTags();
200           for(Int_t i = 0; i < iEvents; i++) {
201             evTag = (AliEventTag *) tagList->At(i);
202             guid = evTag->GetGUID(); 
203             turl = evTag->GetTURL(); 
204             path = evTag->GetPath();
205             localList->SetTreeName(aliceFile.Data());
206             if(turl!="") localList->SetFileName(turl.Data());
207             else localList->SetFileName(path.Data());
208             
209             if(evTagCuts->IsAccepted(evTag)) localList->Enter(i);
210           }//event loop
211           iAccepted += localList->GetN();
212           if(turl != "")      esdChain->AddFile(turl);
213           else if(path != "") esdChain->AddFile(path);
214           fGlobalList->Add(localList);
215         }//detector tag cuts
216       }//lhc tag cuts
217     }//run tags cut
218     tag->Clear();
219   }//tag file loop
220   AliInfo(Form("Accepted events: %d", iAccepted));
221   esdChain->ls();
222   esdChain->SetEntryList(fGlobalList,"ne");
223   delete tag;
224   delete localList;
225   
226   return esdChain;
227 }
228
229 //___________________________________________________________________________
230 TChain *AliTagAnalysis::QueryTags(const char *fRunCut, 
231                                   const char *fLHCCut, 
232                                   const char *fDetectorCut, 
233                                   const char *fEventCut) {       
234   //Queries the tag chain using the defined      
235   //event tag cuts from the AliEventTagCuts object       
236   //and returns a TChain along with the associated TEventList    
237   AliInfo(Form("Querying the tags........"));    
238
239   TString aliceFile;
240   if(fAnalysisType == "ESD") aliceFile = "esdTree";
241   else if(fAnalysisType == "AOD") aliceFile = "aodTree";
242   else AliFatal("Only ESD and AOD type is implemented!!!");
243
244
245   //ESD file chain
246   TChain *esdChain = new TChain(aliceFile.Data());
247   //global entry list
248   fGlobalList = new TEntryList();
249   
250   //Defining tag objects         
251   AliRunTag   *tag   = new AliRunTag;    
252   AliEventTag *evTag = new AliEventTag;          
253   fChain->SetBranchAddress("AliTAG",&tag);       
254   
255   TString guid;          
256   TString turl;          
257   TString path;          
258   
259   TTreeFormula *fRunFormula = new TTreeFormula("fRun",fRunCut,fChain);   
260   TTreeFormula *fLHCFormula = new TTreeFormula("fLHC",fLHCCut,fChain);   
261   TTreeFormula *fDetectorFormula = new TTreeFormula("fDetector",fDetectorCut,fChain);
262   TTreeFormula *fEventFormula = new TTreeFormula("fEvent",fEventCut,fChain);
263   
264   TEntryList* localList = new TEntryList();
265
266   Int_t current = -1; 
267   Int_t iAccepted = 0;   
268   
269   for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
270     fChain->GetEntry(iTagFiles);         
271     if (current != fChain->GetTreeNumber()) {    
272       fRunFormula->UpdateFormulaLeaves();        
273       fLHCFormula->UpdateFormulaLeaves();        
274       fDetectorFormula->UpdateFormulaLeaves();   
275       fEventFormula->UpdateFormulaLeaves();      
276       current = fChain->GetTreeNumber();         
277     }    
278     
279     if(fRunFormula->EvalInstance(iTagFiles) == 1) {      
280       if(fLHCFormula->EvalInstance(iTagFiles) == 1) {    
281         if(fDetectorFormula->EvalInstance(iTagFiles) == 1) {
282           localList->Reset();    
283           Int_t iEvents = fEventFormula->GetNdata();     
284           const TClonesArray *tagList = tag->GetEventTags();     
285           for(Int_t i = 0; i < iEvents; i++) {   
286             evTag = (AliEventTag *) tagList->At(i);      
287             guid = evTag->GetGUID();     
288             turl = evTag->GetTURL();     
289             path = evTag->GetPath();     
290             localList->SetTreeName(aliceFile.Data());
291             localList->SetFileName(turl.Data());
292             if(fEventFormula->EvalInstance(i) == 1) localList->Enter(i);
293           }//event loop          
294
295           if(path != "")      esdChain->AddFile(path);   
296           else if(turl != "") esdChain->AddFile(turl);   
297           fGlobalList->Add(localList);
298           iAccepted += localList->GetN();
299         }//detector tag cuts
300       }//lhc tag cuts
301     }//run tag cut       
302     tag->Clear();
303   }//tag file loop       
304   AliInfo(Form("Accepted events: %d", iAccepted));       
305   esdChain->SetEntryList(fGlobalList,"ne");      
306
307   delete tag;
308   delete localList;
309   return esdChain;       
310 }
311
312 //___________________________________________________________________________
313 Bool_t 
314 AliTagAnalysis::CreateXMLCollection(const char* name, 
315                                     AliRunTagCuts *runTagCuts, 
316                                     AliLHCTagCuts *lhcTagCuts, 
317                                     AliDetectorTagCuts *detTagCuts, 
318                                     AliEventTagCuts *evTagCuts) 
319 {
320   /// Queries the tag chain using the defined run, lhc, detector and event tag objects
321   /// and create a XML collection named "name.xml"
322   /// if any of the runTagCuts, lhcTagCuts, detTagCuts or evTagCuts is NULL
323   /// check on that object will be skipped.
324   
325   AliInfo(Form("Creating the collection........"));
326   
327   if (!fChain) 
328   {
329     AliError("fChain is NULL. Cannot make a collection from that !");
330     return kFALSE;
331   }
332   
333   AliXMLCollection collection;
334   collection.SetCollectionName(name);
335   collection.WriteHeader();
336   
337   TString guid;
338   TString turl;
339   TString lfn;
340   
341   TEntryList localList;
342   Int_t iAccepted = 0;
343     
344   Int_t iRejectedRun = 0;
345   Int_t iRejectedLHC = 0;
346   Int_t iRejectedDet = 0;
347   Int_t iRejectedEvt = 0;
348   
349   Int_t iTotalEvents = 0;
350   
351   Int_t iAcceptedEvtInFile = 0;
352   Int_t iRejectedEvtInFile = 0;
353   
354   //Defining tag objects
355   AliRunTag* tag = new AliRunTag;
356   fChain->SetBranchAddress("AliTAG",&tag);
357   
358   for(Int_t iTagFiles = 0; iTagFiles < fChain->GetListOfFiles()->GetEntries(); ++iTagFiles) 
359   {
360     fChain->GetEntry(iTagFiles);
361     //Event list
362     iTotalEvents += tag->GetNEvents();
363     localList.Reset();
364     
365     if ( !runTagCuts || ( runTagCuts && runTagCuts->IsAccepted(tag) ) ) 
366       {
367         if ( !lhcTagCuts || ( lhcTagCuts && lhcTagCuts->IsAccepted(tag->GetLHCTag())) ) 
368           {
369             if ( !detTagCuts || ( detTagCuts && detTagCuts->IsAccepted(tag->GetDetectorTags())) )
370               {
371                 Int_t i(0);
372                 TIter next(tag->GetEventTags());
373                 AliEventTag* evTag(0x0);
374                 iRejectedEvtInFile = 0;
375                 iAcceptedEvtInFile = 0;
376                 while ( ( evTag = static_cast<AliEventTag*>(next()) ) )
377                   {
378                     guid = evTag->GetGUID(); 
379                     turl = evTag->GetTURL(); 
380                     lfn = turl(8,turl.Length());
381                     if( !evTagCuts || ( evTagCuts && evTagCuts->IsAccepted(evTag)) )
382             {
383               localList.Enter(i);
384               iAcceptedEvtInFile++;
385             }
386                     else 
387                       {
388                         ++iRejectedEvt;
389                         ++iRejectedEvtInFile;
390                       }
391                     ++i;
392                   }//event loop
393                 iAccepted += localList.GetN();
394                 collection.WriteBody(iTagFiles+1,guid,lfn,turl,&localList,iAcceptedEvtInFile,iRejectedEvtInFile);
395               }//detector tag cuts
396             else {
397               iRejectedDet += tag->GetNEvents();
398             }
399           }//lhc tag cuts 
400         else {
401           iRejectedLHC += tag->GetNEvents();
402         }
403       }//run tag cuts
404     else {
405       iRejectedRun += tag->GetNEvents();
406     }
407     tag->Clear();
408   } //tag file loop
409   
410   collection.WriteSummary(iTotalEvents, iAccepted, iRejectedRun, iRejectedLHC, iRejectedDet, iRejectedEvt);
411   collection.Export();
412   
413   return kTRUE;
414 }
415
416 //___________________________________________________________________________
417 Bool_t AliTagAnalysis::CreateXMLCollection(const char* name, 
418                                            const char *fRunCut, 
419                                            const char *fLHCCut, 
420                                            const char *fDetectorCut, 
421                                            const char *fEventCut) {
422   //Queries the tag chain using the defined 
423   //event tag cuts from the AliEventTagCuts object
424   //and returns a XML collection
425   AliInfo(Form("Creating the collection........"));
426
427
428   AliXMLCollection *collection = new AliXMLCollection();
429   collection->SetCollectionName(name);
430   collection->WriteHeader();
431
432   TString guid;
433   TString turl;
434   TString lfn;
435   TEntryList* localList = new TEntryList();
436   
437   Int_t iAccepted = 0;
438
439   Int_t iRejectedRun = 0;
440   Int_t iRejectedLHC = 0;
441   Int_t iRejectedDet = 0;
442   Int_t iRejectedEvt = 0;
443
444   Int_t iTotalEvents = 0;
445
446   Int_t iAcceptedEvtInFile = 0;
447   Int_t iRejectedEvtInFile = 0;
448
449   //Defining tag objects
450   AliRunTag *tag     = new AliRunTag;
451   AliEventTag *evTag = new AliEventTag;
452   fChain->SetBranchAddress("AliTAG",&tag);
453
454   TTreeFormula *fRunFormula = new TTreeFormula("fRun",fRunCut,fChain);
455   TTreeFormula *fLHCFormula = new TTreeFormula("fLHC",fLHCCut,fChain);   
456   TTreeFormula *fDetectorFormula = new TTreeFormula("fDetector",fDetectorCut,fChain);
457   TTreeFormula *fEventFormula = new TTreeFormula("fEvent",fEventCut,fChain);
458   
459   Int_t current = -1;
460
461   for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
462
463     fChain->GetEntry(iTagFiles);
464     if (current != fChain->GetTreeNumber()) {
465       fRunFormula->UpdateFormulaLeaves();
466       fLHCFormula->UpdateFormulaLeaves();
467       fDetectorFormula->UpdateFormulaLeaves();
468       fEventFormula->UpdateFormulaLeaves();
469       current = fChain->GetTreeNumber();
470      }
471  
472    //Event list
473     iTotalEvents += tag->GetNEvents();
474     localList->Reset();
475     if(fRunFormula->EvalInstance(iTagFiles) == 1) {
476       if(fLHCFormula->EvalInstance(iTagFiles) == 1) {    
477         if(fDetectorFormula->EvalInstance(iTagFiles) == 1) {     
478           Int_t iEvents = fEventFormula->GetNdata();
479           const TClonesArray *tagList = tag->GetEventTags();
480           iRejectedEvtInFile = 0;
481           iAcceptedEvtInFile = 0;
482           for(Int_t i = 0; i < iEvents; i++) {
483             evTag = (AliEventTag *) tagList->At(i);
484             guid = evTag->GetGUID(); 
485             turl = evTag->GetTURL(); 
486             lfn = turl(8,turl.Length());
487             if(fEventFormula->EvalInstance(i) == 1) {
488               localList->Enter(i);
489               iAcceptedEvtInFile++;
490             }
491             else {
492               iRejectedEvt++;
493               iRejectedEvtInFile++;
494             }
495           }//event loop
496           collection->WriteBody(iTagFiles+1,guid,lfn,turl,localList,iAcceptedEvtInFile, iRejectedEvtInFile);
497           iAccepted += localList->GetN();
498         }//detector tag cuts
499         else {
500           iRejectedDet += tag->GetNEvents();
501         }
502       }//lhc tag cuts 
503       else {
504         iRejectedLHC += tag->GetNEvents();
505       }
506     }//run tag cuts
507     else {
508       iRejectedRun += tag->GetNEvents();
509     }
510   }//tag file loop
511   collection->WriteSummary(iTotalEvents, iAccepted, iRejectedRun, iRejectedLHC, iRejectedDet, iRejectedEvt);
512   collection->Export();
513   
514   delete tag;
515   return kTRUE;
516 }
517
518 //___________________________________________________________________________
519 TChain *AliTagAnalysis::GetInputChain(const char* system, const char *wn) {
520   //returns the chain+event list - used in batch sessions
521   // this function will be removed once the new root 
522   // improvements are committed
523   TString fsystem = system;
524   Int_t iAccepted = 0;
525
526   TChain *fAnalysisChain = 0;
527   if(fAnalysisType == "ESD") fAnalysisChain = new TChain("esdTree");
528   else if(fAnalysisType == "AOD") fAnalysisChain = new TChain("aodTree");
529   else AliFatal("Only ESD and AOD type is implemented!!!");
530   
531   //Event list
532   TEventList *fEventList = new TEventList();
533   AliXMLCollection *collection = AliXMLCollection::Open(wn);
534
535   collection->Reset();
536   while (collection->Next()) {
537     AliInfo(Form("Adding: %s",collection->GetTURL("")));
538     fAnalysisChain->Add(collection->GetTURL(""));
539     TEntryList *list = (TEntryList *)collection->GetEventList("");
540     for(Int_t i = 0; i < list->GetN(); i++) fEventList->Enter(iAccepted+list->GetEntry(i));
541
542     if(fsystem == "pp") iAccepted += 100;
543     else if(fsystem == "PbPb") iAccepted += 1;
544   }
545
546   fAnalysisChain->SetEventList(fEventList);
547   
548   AliInfo(Form("Number of selected events: %d",fEventList->GetN()));
549
550   return fAnalysisChain;
551 }
552
553 //___________________________________________________________________________
554 TChain*
555 AliTagAnalysis::CreateChainFromCollection(const char* collectionname, const char* treename)
556 {
557   /// Build a TChain (with its TEntryList object attached) from an XML collection.
558   /// Returned chain must be deleted by the client.
559
560   TString streename(treename);
561   if ( streename != "esdTree" && streename != "aodTree" )
562   {
563     AliErrorClass("Only esdTree and aodTree implemented so far...");
564     return 0x0;
565   }
566   
567   TChain* chain = new TChain(streename.Data());
568
569   // create the event list for the chain. Will be attached to the chain
570   // which thus becomes the owner of it.
571   TEntryList* elist = new TEntryList; 
572   
573   AliXMLCollection* collection = AliXMLCollection::Open(collectionname);
574
575   // Tag selection summary per file
576   TMap* tagCutSummary = new TMap();
577   tagCutSummary->SetName("TagCutSumm");
578
579   Int_t iAccepted = 0;
580   
581   collection->Reset();
582   
583   while (collection->Next()) 
584   {
585     AliDebugClass(1,Form("Adding: %s",collection->GetTURL("")));
586     chain->Add(collection->GetTURL(""));
587     TEntryList *list = collection->GetEventList("");
588     list->SetTreeName(streename.Data());
589     list->SetFileName(collection->GetTURL(""));
590     elist->Add(list);
591     iAccepted += list->GetN();
592     if (collection->GetCutSumm())
593     {
594       tagCutSummary->Add(new TObjString(collection->GetTURL("")), new TObjString(collection->GetCutSumm()));
595     }
596   }
597
598   chain->SetEntryList(elist,"ne"); // ne => do not expand tree name and/or file names
599   
600   AliDebugClass(1,Form("Number of selected events: %d",iAccepted));
601
602   TList *aUserInfo = chain->GetUserInfo();
603   aUserInfo->Add(tagCutSummary);
604
605   Int_t iAccEv;
606   Int_t iTotalEvents;
607   Int_t iRejRun;
608   Int_t iRejLHC;
609   Int_t iRejDet;
610   Int_t iRejEvt;
611
612   collection->GetCollectionSummary(&iTotalEvents, &iAccEv, &iRejRun, &iRejLHC, &iRejDet, &iRejEvt);
613  
614   char nstr[2000];
615
616   sprintf(nstr, "TotalEvents=%i", iTotalEvents);
617   TObjString *iTotStr = new TObjString(nstr);
618   aUserInfo->Add(iTotStr);
619
620   sprintf(nstr, "AcceptedEvents=%i", iAccepted);
621   TObjString *iAccStr = new TObjString(nstr);
622   aUserInfo->Add(iAccStr);
623
624   sprintf(nstr, "RejectedRun=%i", iRejRun);
625   TObjString *iRejRunStr = new TObjString(nstr);
626   aUserInfo->Add(iRejRunStr);
627
628   sprintf(nstr, "RejectedLHC=%i", iRejLHC);
629   TObjString *iRejLHCStr = new TObjString(nstr);
630   aUserInfo->Add(iRejLHCStr);
631
632   sprintf(nstr, "RejectedDet=%i", iRejDet);
633   TObjString *iRejDetStr = new TObjString(nstr);
634   aUserInfo->Add(iRejDetStr);
635
636   sprintf(nstr, "RejectedEvt=%i", iRejEvt);
637   TObjString *iRejEvtStr = new TObjString(nstr);
638   aUserInfo->Add(iRejEvtStr);
639
640   return chain;
641 }