]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliTagAnalysis.cxx
Getters and setter of vtx type modified.
[u/mrichter/AliRoot.git] / STEER / 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 "AliRunTagCuts.h"
40 #include "AliXMLCollection.h"
41
42 class TTree;
43
44 ClassImp(AliTagAnalysis)
45
46 //___________________________________________________________________________
47 AliTagAnalysis::AliTagAnalysis(): 
48   TObject(),
49   ftagresult(0x0),
50   fTagDirName(),
51   fChain(0x0)
52 {
53   //Default constructor for a AliTagAnalysis
54 }
55
56 //___________________________________________________________________________
57 AliTagAnalysis::~AliTagAnalysis() {
58 //Default destructor for a AliTagAnalysis
59 }
60
61 //___________________________________________________________________________
62 Bool_t  AliTagAnalysis::AddTagsFile(const char *alienUrl) {
63
64   // Add a single tags file to the chain
65
66   Bool_t rv = kTRUE ;
67
68   if (! fChain) fChain = new TChain("T");
69
70   TFile *f = TFile::Open(alienUrl,"READ");
71   fChain->Add(alienUrl);
72   AliInfo(Form("Chained tag files: %d ",fChain->GetEntries()));
73   delete f;
74
75   if (fChain->GetEntries() == 0 )
76     rv = kFALSE ;
77
78   return rv ;
79 }
80
81 //___________________________________________________________________________
82 void AliTagAnalysis::ChainLocalTags(const char *dirname) {
83   //Searches the entries of the provided direcory
84   //Chains the tags that are stored locally
85   fTagDirName = dirname;
86   TString fTagFilename;
87   
88   if (! fChain)  fChain = new TChain("T");
89   
90   const char * tagPattern = "tag.root";
91   // Open the working directory
92   void * dirp = gSystem->OpenDirectory(fTagDirName);
93   const char * name = 0x0;
94   // Add all files matching *pattern* to the chain
95   while((name = gSystem->GetDirEntry(dirp))) {
96     if (strstr(name,tagPattern)) { 
97       fTagFilename = fTagDirName;
98       fTagFilename += "/";
99       fTagFilename += name;
100                 
101       fChain->Add(fTagFilename);  
102     }//pattern check
103   }//directory loop
104   AliInfo(Form("Chained tag files: %d ",fChain->GetEntries()));
105 }
106
107
108 //___________________________________________________________________________
109 void AliTagAnalysis::ChainGridTags(TGridResult *res) {
110   //Loops overs the entries of the TGridResult
111   //Chains the tags that are stored in the GRID
112   ftagresult = res;
113   Int_t nEntries = ftagresult->GetEntries();
114  
115   if (! fChain)  fChain = new TChain("T");
116
117   TString gridname = "alien://";
118   TString alienUrl;
119  
120   for(Int_t i = 0; i < nEntries; i++) {
121     alienUrl = ftagresult->GetKey(i,"turl");
122     fChain->Add(alienUrl);
123   }//grid result loop  
124 }
125
126
127 //___________________________________________________________________________
128 TChain *AliTagAnalysis::QueryTags(AliRunTagCuts *RunTagCuts, AliEventTagCuts *EvTagCuts) {
129   //Queries the tag chain using the defined 
130   //event tag cuts from the AliEventTagCuts object
131   //and returns a TChain along with the associated TEventList
132   AliInfo(Form("Querying the tags........"));
133   
134   //ESD file chain
135   TChain *fESDchain = new TChain("esdTree");
136   //Event list
137   TEventList *fEventList = new TEventList();
138   
139   //Defining tag objects
140   AliRunTag *tag = new AliRunTag;
141   AliEventTag *evTag = new AliEventTag;
142   fChain->SetBranchAddress("AliTAG",&tag);
143
144   TString guid = 0;
145   TString turl = 0;
146   TString path = 0;
147
148   Int_t iAccepted = 0;
149   for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
150     fChain->GetEntry(iTagFiles);
151     if(RunTagCuts->IsAccepted(tag)) {
152       Int_t iEvents = tag->GetNEvents();
153       const TClonesArray *tagList = tag->GetEventTags();
154       for(Int_t i = 0; i < iEvents; i++) {
155         evTag = (AliEventTag *) tagList->At(i);
156         guid = evTag->GetGUID(); 
157         turl = evTag->GetTURL(); 
158         path = evTag->GetPath();
159         if(EvTagCuts->IsAccepted(evTag)) fEventList->Enter(iAccepted+i);
160       }//event loop
161       iAccepted += iEvents;
162     
163       if(path != "") fESDchain->AddFile(path);
164       else if(turl != "") fESDchain->AddFile(turl);
165     }//run tags cut
166   }//tag file loop
167   AliInfo(Form("Accepted events: %d",fEventList->GetN()));
168   fESDchain->SetEventList(fEventList);
169    
170   return fESDchain;
171 }
172
173 //___________________________________________________________________________
174 TChain *AliTagAnalysis::QueryTags(const char *fRunCut, const char *fEventCut) {          
175   //Queries the tag chain using the defined      
176   //event tag cuts from the AliEventTagCuts object       
177   //and returns a TChain along with the associated TEventList    
178   AliInfo(Form("Querying the tags........"));    
179   
180   //ESD file chain       
181   TChain *fESDchain = new TChain("esdTree");     
182   //Event list   
183   TEventList *fEventList = new TEventList();     
184   
185   //Defining tag objects         
186   AliRunTag *tag = new AliRunTag;        
187   AliEventTag *evTag = new AliEventTag;          
188   fChain->SetBranchAddress("AliTAG",&tag);       
189   
190   TString guid = 0;      
191   TString turl = 0;      
192   TString path = 0;      
193   
194   TTreeFormula *fRunFormula = new TTreeFormula("fRun",fRunCut,fChain);   
195   TTreeFormula *fEventFormula = new TTreeFormula("fEvent",fEventCut,fChain);     
196   
197   Int_t current = -1;    
198   Int_t iAccepted = 0;   
199   for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {      
200     fChain->GetEntry(iTagFiles);         
201     if (current != fChain->GetTreeNumber()) {    
202       fRunFormula->UpdateFormulaLeaves();        
203       fEventFormula->UpdateFormulaLeaves();      
204       current = fChain->GetTreeNumber();         
205     }    
206     if(fRunFormula->EvalInstance(iTagFiles) == 1) {      
207       Int_t iEvents = fEventFormula->GetNdata();         
208       const TClonesArray *tagList = tag->GetEventTags();         
209       for(Int_t i = 0; i < iEvents; i++) {       
210         evTag = (AliEventTag *) tagList->At(i);          
211         guid = evTag->GetGUID();         
212         turl = evTag->GetTURL();         
213         path = evTag->GetPath();         
214         if(fEventFormula->EvalInstance(i) == 1) fEventList->Enter(iAccepted+i);          
215       }//event loop      
216       iAccepted += iEvents;      
217       
218       if(path != "") fESDchain->AddFile(path);   
219       else if(turl != "") fESDchain->AddFile(turl);      
220     }//run tag cut       
221   }//tag file loop       
222   AliInfo(Form("Accepted events: %d",fEventList->GetN()));       
223   fESDchain->SetEventList(fEventList);   
224   
225   return fESDchain;      
226 }
227
228 //___________________________________________________________________________
229 Bool_t AliTagAnalysis::CreateXMLCollection(const char* name, AliRunTagCuts *RunTagCuts, AliEventTagCuts *EvTagCuts) {
230   //Queries the tag chain using the defined 
231   //event tag cuts from the AliEventTagCuts object
232   //and returns a XML collection
233   AliInfo(Form("Creating the collection........"));
234
235   AliXMLCollection *collection = new AliXMLCollection();
236   collection->SetCollectionName(name);
237   collection->WriteHeader();
238
239   //Event list
240   //TEntryList *fEventList = new TEntryList();
241   TString guid = 0x0;
242   TString turl = 0x0;
243   TString lfn = 0x0;
244   
245   //Defining tag objects
246   AliRunTag *tag = new AliRunTag;
247   AliEventTag *evTag = new AliEventTag;
248   fChain->SetBranchAddress("AliTAG",&tag);
249
250   for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
251     //Event list
252     TEntryList *fList = new TEntryList();
253     fChain->GetEntry(iTagFiles);
254     if(RunTagCuts->IsAccepted(tag)) {
255       Int_t iEvents = tag->GetNEvents();
256       const TClonesArray *tagList = tag->GetEventTags();
257       for(Int_t i = 0; i < iEvents; i++) {
258         evTag = (AliEventTag *) tagList->At(i);
259         guid = evTag->GetGUID(); 
260         turl = evTag->GetTURL(); 
261         lfn = turl(8,turl.Length());
262         if(EvTagCuts->IsAccepted(evTag)) fList->Enter(i);
263       }//event loop
264       collection->WriteBody(iTagFiles+1,guid,lfn,turl,fList);
265     }//run tag cuts
266   }//tag file loop
267   collection->Export();
268
269   return kTRUE;
270 }
271
272 //___________________________________________________________________________
273 Bool_t AliTagAnalysis::CreateXMLCollection(const char* name, const char *fRunCut, const char *fEventCut) {
274   //Queries the tag chain using the defined 
275   //event tag cuts from the AliEventTagCuts object
276   //and returns a XML collection
277   AliInfo(Form("Creating the collection........"));
278
279   AliXMLCollection *collection = new AliXMLCollection();
280   collection->SetCollectionName(name);
281   collection->WriteHeader();
282
283   TString guid = 0x0;
284   TString turl = 0x0;
285   TString lfn = 0x0;
286   
287   //Defining tag objects
288   AliRunTag *tag = new AliRunTag;
289   AliEventTag *evTag = new AliEventTag;
290   fChain->SetBranchAddress("AliTAG",&tag);
291
292   TTreeFormula *fRunFormula = new TTreeFormula("fRun",fRunCut,fChain);
293   TTreeFormula *fEventFormula = new TTreeFormula("fEvent",fEventCut,fChain);
294
295   Int_t current = -1;
296   for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
297     //Event list
298     TEntryList *fList = new TEntryList();
299     fChain->GetEntry(iTagFiles);
300     if (current != fChain->GetTreeNumber()) {
301       fRunFormula->UpdateFormulaLeaves();
302       fEventFormula->UpdateFormulaLeaves();
303       current = fChain->GetTreeNumber();
304     }
305     if(fRunFormula->EvalInstance(iTagFiles) == 1) {
306       Int_t iEvents = fEventFormula->GetNdata();
307       const TClonesArray *tagList = tag->GetEventTags();
308       for(Int_t i = 0; i < iEvents; i++) {
309         evTag = (AliEventTag *) tagList->At(i);
310         guid = evTag->GetGUID(); 
311         turl = evTag->GetTURL(); 
312         lfn = turl(8,turl.Length());
313         if(fEventFormula->EvalInstance(i) == 1) fList->Enter(i);
314       }//event loop
315       collection->WriteBody(iTagFiles+1,guid,lfn,turl,fList);
316     }//run tag cuts
317   }//tag file loop
318   collection->Export();
319
320   return kTRUE;
321 }
322
323 //___________________________________________________________________________
324 TChain *AliTagAnalysis::GetInputChain(const char* system, const char*global, const char *wn) {
325   //returns the chain+event list - used in batch sessions
326   TString fsystem = system;
327   Int_t iAccepted = 0;
328   //ESD file chain
329   TChain *fESDchain = new TChain("esdTree");
330   //Event list
331   TEventList *fEventList = new TEventList();
332   AliXMLCollection *collection1 = AliXMLCollection::Open(global);
333   AliXMLCollection *collection2 = AliXMLCollection::Open(wn);
334   collection1->OverlapCollection(collection2);
335   collection1->Reset();
336   while (collection1->Next()) {
337     AliInfo(Form("Adding: %s",collection1->GetTURL("")));
338     fESDchain->Add(collection1->GetTURL(""));
339     TEntryList *list = (TEntryList *)collection1->GetEventList("");
340     for(Int_t i = 0; i < list->GetN(); i++) fEventList->Enter(iAccepted+list->GetEntry(i));
341
342     if(fsystem == "pp") iAccepted += 100;
343     else if(fsystem == "PbPb") iAccepted += 1;
344   }
345
346   fESDchain->SetEventList(fEventList);
347   
348   AliInfo(Form("Number of selected events: %d",fEventList->GetN()));
349
350   return fESDchain;
351 }