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