]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ANALYSIS/AliTagAnalysis.cxx
Fix length of water inside cooling pipes
[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 <TSystem.h>
24 #include <TChain.h>
25 #include <TFile.h>
26 #include <TEventList.h>
27 #include <TEntryList.h>
28 #include <TTreeFormula.h>
29
30 //ROOT-AliEn
31 #include <TGridResult.h>
32
33 #include "AliLog.h"
34
35 #include "AliRunTag.h"
36 #include "AliEventTag.h"
37 #include "AliTagAnalysis.h"
38 #include "AliEventTagCuts.h"
39 #include "AliDetectorTagCuts.h"
40 #include "AliLHCTagCuts.h"
41 #include "AliRunTagCuts.h"
42 #include "AliXMLCollection.h"
43
44 class TTree;
45
46 ClassImp(AliTagAnalysis)
47
48 //___________________________________________________________________________
49 AliTagAnalysis::AliTagAnalysis(): 
50   TObject(),
51   ftagresult(0x0),
52   fTagDirName(),
53   fChain(0x0)
54 {
55   //Default constructor for a AliTagAnalysis
56 }
57
58 //___________________________________________________________________________
59 AliTagAnalysis::~AliTagAnalysis() {
60 //Default destructor for a AliTagAnalysis
61 }
62
63 //___________________________________________________________________________
64 Bool_t  AliTagAnalysis::AddTagsFile(const char *alienUrl) {
65
66   // Add a single tags file to the chain
67
68   Bool_t rv = kTRUE ;
69
70   if (! fChain) fChain = new TChain("T");
71
72   TFile *f = TFile::Open(alienUrl,"READ");
73   fChain->Add(alienUrl);
74   AliInfo(Form("Chained tag files: %d ",fChain->GetEntries()));
75   delete f;
76
77   if (fChain->GetEntries() == 0 )
78     rv = kFALSE ;
79
80   return rv ;
81 }
82
83 //___________________________________________________________________________
84 void AliTagAnalysis::ChainLocalTags(const char *dirname) {
85   //Searches the entries of the provided direcory
86   //Chains the tags that are stored locally
87   fTagDirName = dirname;
88   TString fTagFilename;
89   
90   if (! fChain)  fChain = new TChain("T");
91   
92   const char * tagPattern = "tag.root";
93   // Open the working directory
94   void * dirp = gSystem->OpenDirectory(fTagDirName);
95   const char * name = 0x0;
96   // Add all files matching *pattern* to the chain
97   while((name = gSystem->GetDirEntry(dirp))) {
98     if (strstr(name,tagPattern)) { 
99       fTagFilename = fTagDirName;
100       fTagFilename += "/";
101       fTagFilename += name;
102                 
103       fChain->Add(fTagFilename);  
104     }//pattern check
105   }//directory loop
106   AliInfo(Form("Chained tag files: %d ",fChain->GetEntries()));
107 }
108
109
110 //___________________________________________________________________________
111 void AliTagAnalysis::ChainGridTags(TGridResult *res) {
112   //Loops overs the entries of the TGridResult
113   //Chains the tags that are stored in the GRID
114   ftagresult = res;
115   Int_t nEntries = ftagresult->GetEntries();
116  
117   if (! fChain)  fChain = new TChain("T");
118
119   TString gridname = "alien://";
120   TString alienUrl;
121  
122   for(Int_t i = 0; i < nEntries; i++) {
123     alienUrl = ftagresult->GetKey(i,"turl");
124     fChain->Add(alienUrl);
125   }//grid result loop  
126 }
127
128
129 //___________________________________________________________________________
130 TChain *AliTagAnalysis::QueryTags(AliRunTagCuts *runTagCuts, AliLHCTagCuts *lhcTagCuts, AliDetectorTagCuts *detTagCuts, AliEventTagCuts *evTagCuts) {
131   //Queries the tag chain using the defined 
132   //event tag cuts from the AliEventTagCuts object
133   //and returns a TChain along with the associated TEventList
134   AliInfo(Form("Querying the tags........"));
135
136   TString fAliceFile = "esdTree";
137
138   //ESD file chain
139   TChain *fESDchain = new TChain(fAliceFile.Data());
140   //global entry list
141   TEntryList *fGlobalList = new TEntryList();
142   
143   //Defining tag objects
144   AliRunTag *tag = new AliRunTag;
145   AliEventTag *evTag = new AliEventTag;
146   fChain->SetBranchAddress("AliTAG",&tag);
147
148   TString guid = 0;
149   TString turl = 0;
150   TString path = 0;
151
152   Int_t iAccepted = 0;
153   for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
154     fChain->GetEntry(iTagFiles);
155     if(runTagCuts->IsAccepted(tag)) {
156       if(lhcTagCuts->IsAccepted(tag->GetLHCTag())) {
157         if(detTagCuts->IsAccepted(tag->GetDetectorTags())) {
158           TEntryList *fLocalList = new TEntryList();
159           Int_t iEvents = tag->GetNEvents();
160           const TClonesArray *tagList = tag->GetEventTags();
161           for(Int_t i = 0; i < iEvents; i++) {
162             evTag = (AliEventTag *) tagList->At(i);
163             guid = evTag->GetGUID(); 
164             turl = evTag->GetTURL(); 
165             path = evTag->GetPath();
166             fLocalList->SetTreeName(fAliceFile.Data());
167             if(turl!="") fLocalList->SetFileName(turl.Data());
168             else fLocalList->SetFileName(path.Data());
169             if(evTagCuts->IsAccepted(evTag)) fLocalList->Enter(i);
170           }//event loop
171           
172           if(path != "") fESDchain->AddFile(path);
173           else if(turl != "") fESDchain->AddFile(turl);
174           fGlobalList->Add(fLocalList);
175           iAccepted += fLocalList->GetN();
176         }//detector tag cuts
177       }//lhc tag cuts
178     }//run tags cut
179   }//tag file loop
180   AliInfo(Form("Accepted events: %d",iAccepted));
181   fESDchain->SetEntryList(fGlobalList,"ne");
182    
183   return fESDchain;
184 }
185
186 //___________________________________________________________________________
187 TChain *AliTagAnalysis::QueryTags(const char *fRunCut, const char *fLHCCut, const char *fDetectorCut, const char *fEventCut) {   
188   //Queries the tag chain using the defined      
189   //event tag cuts from the AliEventTagCuts object       
190   //and returns a TChain along with the associated TEventList    
191   AliInfo(Form("Querying the tags........"));    
192   
193   TString fAliceFile = "esdTree";
194
195   //ESD file chain
196   TChain *fESDchain = new TChain(fAliceFile.Data());
197   //global entry list
198   TEntryList *fGlobalList = new TEntryList();
199   
200   //Defining tag objects         
201   AliRunTag *tag = new AliRunTag;        
202   AliEventTag *evTag = new AliEventTag;          
203   fChain->SetBranchAddress("AliTAG",&tag);       
204   
205   TString guid = 0;      
206   TString turl = 0;      
207   TString path = 0;      
208   
209   TTreeFormula *fRunFormula = new TTreeFormula("fRun",fRunCut,fChain);   
210   TTreeFormula *fLHCFormula = new TTreeFormula("fLHC",fLHCCut,fChain);   
211   TTreeFormula *fDetectorFormula = new TTreeFormula("fDetector",fDetectorCut,fChain);
212   TTreeFormula *fEventFormula = new TTreeFormula("fEvent",fEventCut,fChain);
213   
214   Int_t current = -1;    
215   Int_t iAccepted = 0;   
216   for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {      
217     fChain->GetEntry(iTagFiles);         
218     if (current != fChain->GetTreeNumber()) {    
219       fRunFormula->UpdateFormulaLeaves();        
220       fLHCFormula->UpdateFormulaLeaves();        
221       fDetectorFormula->UpdateFormulaLeaves();   
222       fEventFormula->UpdateFormulaLeaves();      
223       current = fChain->GetTreeNumber();         
224     }    
225     if(fRunFormula->EvalInstance(iTagFiles) == 1) {      
226       if(fLHCFormula->EvalInstance(iTagFiles) == 1) {    
227         if(fDetectorFormula->EvalInstance(iTagFiles) == 1) {     
228           TEntryList *fLocalList = new TEntryList();
229           Int_t iEvents = fEventFormula->GetNdata();     
230           const TClonesArray *tagList = tag->GetEventTags();     
231           for(Int_t i = 0; i < iEvents; i++) {   
232             evTag = (AliEventTag *) tagList->At(i);      
233             guid = evTag->GetGUID();     
234             turl = evTag->GetTURL();     
235             path = evTag->GetPath();     
236             fLocalList->SetTreeName(fAliceFile.Data());
237             fLocalList->SetFileName(turl.Data());
238             if(fEventFormula->EvalInstance(i) == 1) fLocalList->Enter(i);
239           }//event loop          
240           iAccepted += fLocalList->GetN();       
241           
242           if(path != "") fESDchain->AddFile(path);       
243           else if(turl != "") fESDchain->AddFile(turl);          
244           fGlobalList->Add(fLocalList);
245           iAccepted += fLocalList->GetN();
246         }//detector tag cuts
247       }//lhc tag cuts
248     }//run tag cut       
249   }//tag file loop       
250   AliInfo(Form("Accepted events: %d",iAccepted));        
251   fESDchain->SetEntryList(fGlobalList,"ne");     
252   
253   return fESDchain;      
254 }
255
256 //___________________________________________________________________________
257 Bool_t AliTagAnalysis::CreateXMLCollection(const char* name, AliRunTagCuts *runTagCuts, AliLHCTagCuts *lhcTagCuts, AliDetectorTagCuts *detTagCuts, AliEventTagCuts *evTagCuts) {
258   //Queries the tag chain using the defined 
259   //event tag cuts from the AliEventTagCuts object
260   //and returns a XML collection
261   AliInfo(Form("Creating the collection........"));
262
263   AliXMLCollection *collection = new AliXMLCollection();
264   collection->SetCollectionName(name);
265   collection->WriteHeader();
266
267   TString guid = 0x0;
268   TString turl = 0x0;
269   TString lfn = 0x0;
270   
271   //Defining tag objects
272   AliRunTag *tag = new AliRunTag;
273   AliEventTag *evTag = new AliEventTag;
274   fChain->SetBranchAddress("AliTAG",&tag);
275
276   for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
277     //Event list
278     TEntryList *fList = new TEntryList();
279     fChain->GetEntry(iTagFiles);
280     if(runTagCuts->IsAccepted(tag)) {
281       if(lhcTagCuts->IsAccepted(tag->GetLHCTag())) {
282         if(detTagCuts->IsAccepted(tag->GetDetectorTags())) {
283           Int_t iEvents = tag->GetNEvents();
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             lfn = turl(8,turl.Length());
290             if(evTagCuts->IsAccepted(evTag)) fList->Enter(i);
291           }//event loop
292           collection->WriteBody(iTagFiles+1,guid,lfn,turl,fList);
293         }//detector tag cuts
294       }//lhc tag cuts 
295     }//run tag cuts
296   }//tag file loop
297   collection->Export();
298
299   return kTRUE;
300 }
301
302 //___________________________________________________________________________
303 Bool_t AliTagAnalysis::CreateXMLCollection(const char* name, const char *fRunCut, const char *fLHCCut, const char *fDetectorCut, const char *fEventCut) {
304   //Queries the tag chain using the defined 
305   //event tag cuts from the AliEventTagCuts object
306   //and returns a XML collection
307   AliInfo(Form("Creating the collection........"));
308
309   AliXMLCollection *collection = new AliXMLCollection();
310   collection->SetCollectionName(name);
311   collection->WriteHeader();
312
313   TString guid = 0x0;
314   TString turl = 0x0;
315   TString lfn = 0x0;
316   
317   //Defining tag objects
318   AliRunTag *tag = new AliRunTag;
319   AliEventTag *evTag = new AliEventTag;
320   fChain->SetBranchAddress("AliTAG",&tag);
321
322   TTreeFormula *fRunFormula = new TTreeFormula("fRun",fRunCut,fChain);
323   TTreeFormula *fLHCFormula = new TTreeFormula("fLHC",fLHCCut,fChain);   
324   TTreeFormula *fDetectorFormula = new TTreeFormula("fDetector",fDetectorCut,fChain);
325   TTreeFormula *fEventFormula = new TTreeFormula("fEvent",fEventCut,fChain);
326
327   Int_t current = -1;
328   for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
329     //Event list
330     TEntryList *fList = new TEntryList();
331     fChain->GetEntry(iTagFiles);
332     if (current != fChain->GetTreeNumber()) {
333       fRunFormula->UpdateFormulaLeaves();
334       fLHCFormula->UpdateFormulaLeaves();
335       fDetectorFormula->UpdateFormulaLeaves();
336       fEventFormula->UpdateFormulaLeaves();
337       current = fChain->GetTreeNumber();
338     }
339     if(fRunFormula->EvalInstance(iTagFiles) == 1) {
340       if(fLHCFormula->EvalInstance(iTagFiles) == 1) {    
341         if(fDetectorFormula->EvalInstance(iTagFiles) == 1) {     
342           Int_t iEvents = fEventFormula->GetNdata();
343           const TClonesArray *tagList = tag->GetEventTags();
344           for(Int_t i = 0; i < iEvents; i++) {
345             evTag = (AliEventTag *) tagList->At(i);
346             guid = evTag->GetGUID(); 
347             turl = evTag->GetTURL(); 
348             lfn = turl(8,turl.Length());
349             if(fEventFormula->EvalInstance(i) == 1) fList->Enter(i);
350           }//event loop
351           collection->WriteBody(iTagFiles+1,guid,lfn,turl,fList);
352         }//detector tag cuts
353       }//lhc tag cuts 
354     }//run tag cuts
355   }//tag file loop
356   collection->Export();
357
358   return kTRUE;
359 }
360
361 //___________________________________________________________________________
362 TChain *AliTagAnalysis::GetInputChain(const char* system, const char *wn) {
363   //returns the chain+event list - used in batch sessions
364   // this function will be removed once the new root 
365   // improvements are committed
366   TString fsystem = system;
367   Int_t iAccepted = 0;
368   //ESD file chain
369   TChain *fESDchain = new TChain("esdTree");
370   //Event list
371   TEventList *fEventList = new TEventList();
372   AliXMLCollection *collection = AliXMLCollection::Open(wn);
373
374   collection->Reset();
375   while (collection->Next()) {
376     AliInfo(Form("Adding: %s",collection->GetTURL("")));
377     fESDchain->Add(collection->GetTURL(""));
378     TEntryList *list = (TEntryList *)collection->GetEventList("");
379     for(Int_t i = 0; i < list->GetN(); i++) fEventList->Enter(iAccepted+list->GetEntry(i));
380
381     if(fsystem == "pp") iAccepted += 100;
382     else if(fsystem == "PbPb") iAccepted += 1;
383   }
384
385   fESDchain->SetEventList(fEventList);
386   
387   AliInfo(Form("Number of selected events: %d",fEventList->GetN()));
388
389   return fESDchain;
390 }
391
392 //___________________________________________________________________________
393 TChain *AliTagAnalysis::GetChainFromCollection(const char* collectionname, const char* treename) {
394   //returns the TChain+TEntryList object- used in batch sessions
395   TString fAliceFile = treename;
396   Int_t iAccepted = 0;
397   TChain *fAnalysisChain = new TChain();
398   if(fAliceFile == "esdTree") {
399     //ESD file chain
400     fAnalysisChain->SetName("esdTree");
401   } else if(fAliceFile == "aodTree") {
402     //AOD file chain
403     fAnalysisChain->SetName("aodTree");
404     AliFatal("AOD case not yet implemented!!!");
405   }
406   else AliFatal("Inconsistent tree name - use esdTree or aodTree!");
407   //Event list
408   TEntryList *fGlobalList = new TEntryList();
409   AliXMLCollection *collection = AliXMLCollection::Open(collectionname);
410
411   collection->Reset();
412   while (collection->Next()) {
413     AliInfo(Form("Adding: %s",collection->GetTURL("")));
414     fAnalysisChain->Add(collection->GetTURL(""));
415     TEntryList *list = (TEntryList *)collection->GetEventList("");
416     list->SetTreeName(fAliceFile.Data());
417     list->SetFileName(collection->GetTURL(""));
418     fGlobalList->Add(list);
419     iAccepted += list->GetN();
420   }
421
422   fAnalysisChain->SetEntryList(fGlobalList,"ne");
423   
424   AliInfo(Form("Number of selected events: %d",iAccepted));
425
426   return fAnalysisChain;
427 }