]> git.uio.no Git - u/mrichter/AliRoot.git/blame - ANALYSIS/AliTagAnalysis.cxx
DDL left and right definition swapped
[u/mrichter/AliRoot.git] / ANALYSIS / AliTagAnalysis.cxx
CommitLineData
d08133e6 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"
2ec6a1d3 39#include "AliDetectorTagCuts.h"
40#include "AliLHCTagCuts.h"
d08133e6 41#include "AliRunTagCuts.h"
42#include "AliXMLCollection.h"
43
44class TTree;
45
46ClassImp(AliTagAnalysis)
47
48//___________________________________________________________________________
49AliTagAnalysis::AliTagAnalysis():
50 TObject(),
51 ftagresult(0x0),
52 fTagDirName(),
53 fChain(0x0)
54{
55 //Default constructor for a AliTagAnalysis
56}
57
58//___________________________________________________________________________
59AliTagAnalysis::~AliTagAnalysis() {
60//Default destructor for a AliTagAnalysis
61}
62
63//___________________________________________________________________________
64Bool_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//___________________________________________________________________________
84void 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//___________________________________________________________________________
111void 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//___________________________________________________________________________
2ec6a1d3 130TChain *AliTagAnalysis::QueryTags(AliRunTagCuts *runTagCuts, AliLHCTagCuts *lhcTagCuts, AliDetectorTagCuts *detTagCuts, AliEventTagCuts *evTagCuts) {
d08133e6 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........"));
b97856f9 135
136 TString fAliceFile = "esdTree";
137
d08133e6 138 //ESD file chain
b97856f9 139 TChain *fESDchain = new TChain(fAliceFile.Data());
140 //global entry list
141 TEntryList *fGlobalList = new TEntryList();
d08133e6 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);
2ec6a1d3 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 fLocalList->SetFileName(turl.Data());
168 if(evTagCuts->IsAccepted(evTag)) fLocalList->Enter(i);
169 }//event loop
170
171 if(path != "") fESDchain->AddFile(path);
172 else if(turl != "") fESDchain->AddFile(turl);
173 fGlobalList->Add(fLocalList);
174 iAccepted += fLocalList->GetN();
175 }//detector tag cuts
176 }//lhc tag cuts
d08133e6 177 }//run tags cut
178 }//tag file loop
b97856f9 179 AliInfo(Form("Accepted events: %d",iAccepted));
180 fESDchain->SetEntryList(fGlobalList,"ne");
d08133e6 181
182 return fESDchain;
183}
184
185//___________________________________________________________________________
2ec6a1d3 186TChain *AliTagAnalysis::QueryTags(const char *fRunCut, const char *fLHCCut, const char *fDetectorCut, const char *fEventCut) {
d08133e6 187 //Queries the tag chain using the defined
188 //event tag cuts from the AliEventTagCuts object
189 //and returns a TChain along with the associated TEventList
190 AliInfo(Form("Querying the tags........"));
191
b97856f9 192 TString fAliceFile = "esdTree";
193
194 //ESD file chain
195 TChain *fESDchain = new TChain(fAliceFile.Data());
196 //global entry list
197 TEntryList *fGlobalList = new TEntryList();
d08133e6 198
199 //Defining tag objects
200 AliRunTag *tag = new AliRunTag;
201 AliEventTag *evTag = new AliEventTag;
202 fChain->SetBranchAddress("AliTAG",&tag);
203
204 TString guid = 0;
205 TString turl = 0;
206 TString path = 0;
207
208 TTreeFormula *fRunFormula = new TTreeFormula("fRun",fRunCut,fChain);
2ec6a1d3 209 TTreeFormula *fLHCFormula = new TTreeFormula("fLHC",fLHCCut,fChain);
210 TTreeFormula *fDetectorFormula = new TTreeFormula("fDetector",fDetectorCut,fChain);
211 TTreeFormula *fEventFormula = new TTreeFormula("fEvent",fEventCut,fChain);
d08133e6 212
213 Int_t current = -1;
214 Int_t iAccepted = 0;
215 for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
216 fChain->GetEntry(iTagFiles);
217 if (current != fChain->GetTreeNumber()) {
218 fRunFormula->UpdateFormulaLeaves();
2ec6a1d3 219 fLHCFormula->UpdateFormulaLeaves();
220 fDetectorFormula->UpdateFormulaLeaves();
d08133e6 221 fEventFormula->UpdateFormulaLeaves();
222 current = fChain->GetTreeNumber();
223 }
224 if(fRunFormula->EvalInstance(iTagFiles) == 1) {
2ec6a1d3 225 if(fLHCFormula->EvalInstance(iTagFiles) == 1) {
226 if(fDetectorFormula->EvalInstance(iTagFiles) == 1) {
227 TEntryList *fLocalList = new TEntryList();
228 Int_t iEvents = fEventFormula->GetNdata();
229 const TClonesArray *tagList = tag->GetEventTags();
230 for(Int_t i = 0; i < iEvents; i++) {
231 evTag = (AliEventTag *) tagList->At(i);
232 guid = evTag->GetGUID();
233 turl = evTag->GetTURL();
234 path = evTag->GetPath();
235 fLocalList->SetTreeName(fAliceFile.Data());
236 fLocalList->SetFileName(turl.Data());
237 if(fEventFormula->EvalInstance(i) == 1) fLocalList->Enter(i);
238 }//event loop
239 iAccepted += fLocalList->GetN();
240
241 if(path != "") fESDchain->AddFile(path);
242 else if(turl != "") fESDchain->AddFile(turl);
243 fGlobalList->Add(fLocalList);
244 iAccepted += fLocalList->GetN();
245 }//detector tag cuts
246 }//lhc tag cuts
d08133e6 247 }//run tag cut
248 }//tag file loop
b97856f9 249 AliInfo(Form("Accepted events: %d",iAccepted));
250 fESDchain->SetEntryList(fGlobalList,"ne");
d08133e6 251
252 return fESDchain;
253}
254
255//___________________________________________________________________________
2ec6a1d3 256Bool_t AliTagAnalysis::CreateXMLCollection(const char* name, AliRunTagCuts *runTagCuts, AliLHCTagCuts *lhcTagCuts, AliDetectorTagCuts *detTagCuts, AliEventTagCuts *evTagCuts) {
d08133e6 257 //Queries the tag chain using the defined
258 //event tag cuts from the AliEventTagCuts object
259 //and returns a XML collection
260 AliInfo(Form("Creating the collection........"));
261
262 AliXMLCollection *collection = new AliXMLCollection();
263 collection->SetCollectionName(name);
264 collection->WriteHeader();
265
d08133e6 266 TString guid = 0x0;
267 TString turl = 0x0;
268 TString lfn = 0x0;
269
270 //Defining tag objects
271 AliRunTag *tag = new AliRunTag;
272 AliEventTag *evTag = new AliEventTag;
273 fChain->SetBranchAddress("AliTAG",&tag);
274
275 for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
276 //Event list
277 TEntryList *fList = new TEntryList();
278 fChain->GetEntry(iTagFiles);
2ec6a1d3 279 if(runTagCuts->IsAccepted(tag)) {
280 if(lhcTagCuts->IsAccepted(tag->GetLHCTag())) {
281 if(detTagCuts->IsAccepted(tag->GetDetectorTags())) {
282 Int_t iEvents = tag->GetNEvents();
283 const TClonesArray *tagList = tag->GetEventTags();
284 for(Int_t i = 0; i < iEvents; i++) {
285 evTag = (AliEventTag *) tagList->At(i);
286 guid = evTag->GetGUID();
287 turl = evTag->GetTURL();
288 lfn = turl(8,turl.Length());
289 if(evTagCuts->IsAccepted(evTag)) fList->Enter(i);
290 }//event loop
291 collection->WriteBody(iTagFiles+1,guid,lfn,turl,fList);
292 }//detector tag cuts
293 }//lhc tag cuts
d08133e6 294 }//run tag cuts
295 }//tag file loop
296 collection->Export();
297
298 return kTRUE;
299}
300
301//___________________________________________________________________________
2ec6a1d3 302Bool_t AliTagAnalysis::CreateXMLCollection(const char* name, const char *fRunCut, const char *fLHCCut, const char *fDetectorCut, const char *fEventCut) {
d08133e6 303 //Queries the tag chain using the defined
304 //event tag cuts from the AliEventTagCuts object
305 //and returns a XML collection
306 AliInfo(Form("Creating the collection........"));
307
308 AliXMLCollection *collection = new AliXMLCollection();
309 collection->SetCollectionName(name);
310 collection->WriteHeader();
311
312 TString guid = 0x0;
313 TString turl = 0x0;
314 TString lfn = 0x0;
315
316 //Defining tag objects
317 AliRunTag *tag = new AliRunTag;
318 AliEventTag *evTag = new AliEventTag;
319 fChain->SetBranchAddress("AliTAG",&tag);
320
321 TTreeFormula *fRunFormula = new TTreeFormula("fRun",fRunCut,fChain);
2ec6a1d3 322 TTreeFormula *fLHCFormula = new TTreeFormula("fLHC",fLHCCut,fChain);
323 TTreeFormula *fDetectorFormula = new TTreeFormula("fDetector",fDetectorCut,fChain);
d08133e6 324 TTreeFormula *fEventFormula = new TTreeFormula("fEvent",fEventCut,fChain);
325
326 Int_t current = -1;
327 for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
328 //Event list
329 TEntryList *fList = new TEntryList();
330 fChain->GetEntry(iTagFiles);
331 if (current != fChain->GetTreeNumber()) {
332 fRunFormula->UpdateFormulaLeaves();
2ec6a1d3 333 fLHCFormula->UpdateFormulaLeaves();
334 fDetectorFormula->UpdateFormulaLeaves();
d08133e6 335 fEventFormula->UpdateFormulaLeaves();
336 current = fChain->GetTreeNumber();
337 }
338 if(fRunFormula->EvalInstance(iTagFiles) == 1) {
2ec6a1d3 339 if(fLHCFormula->EvalInstance(iTagFiles) == 1) {
340 if(fDetectorFormula->EvalInstance(iTagFiles) == 1) {
341 Int_t iEvents = fEventFormula->GetNdata();
342 const TClonesArray *tagList = tag->GetEventTags();
343 for(Int_t i = 0; i < iEvents; i++) {
344 evTag = (AliEventTag *) tagList->At(i);
345 guid = evTag->GetGUID();
346 turl = evTag->GetTURL();
347 lfn = turl(8,turl.Length());
348 if(fEventFormula->EvalInstance(i) == 1) fList->Enter(i);
349 }//event loop
350 collection->WriteBody(iTagFiles+1,guid,lfn,turl,fList);
351 }//detector tag cuts
352 }//lhc tag cuts
d08133e6 353 }//run tag cuts
354 }//tag file loop
355 collection->Export();
356
357 return kTRUE;
358}
359
360//___________________________________________________________________________
361TChain *AliTagAnalysis::GetInputChain(const char* system, const char *wn) {
362 //returns the chain+event list - used in batch sessions
363 // this function will be removed once the new root
364 // improvements are committed
365 TString fsystem = system;
366 Int_t iAccepted = 0;
367 //ESD file chain
368 TChain *fESDchain = new TChain("esdTree");
369 //Event list
370 TEventList *fEventList = new TEventList();
371 AliXMLCollection *collection = AliXMLCollection::Open(wn);
372
373 collection->Reset();
374 while (collection->Next()) {
375 AliInfo(Form("Adding: %s",collection->GetTURL("")));
376 fESDchain->Add(collection->GetTURL(""));
377 TEntryList *list = (TEntryList *)collection->GetEventList("");
378 for(Int_t i = 0; i < list->GetN(); i++) fEventList->Enter(iAccepted+list->GetEntry(i));
379
380 if(fsystem == "pp") iAccepted += 100;
381 else if(fsystem == "PbPb") iAccepted += 1;
382 }
383
384 fESDchain->SetEventList(fEventList);
385
386 AliInfo(Form("Number of selected events: %d",fEventList->GetN()));
387
388 return fESDchain;
389}
390
391//___________________________________________________________________________
392TChain *AliTagAnalysis::GetChainFromCollection(const char* collectionname, const char* treename) {
393 //returns the TChain+TEntryList object- used in batch sessions
394 TString fAliceFile = treename;
395 Int_t iAccepted = 0;
396 TChain *fAnalysisChain = new TChain();
397 if(fAliceFile == "esdTree") {
398 //ESD file chain
399 fAnalysisChain->SetName("esdTree");
400 } else if(fAliceFile == "aodTree") {
401 //AOD file chain
402 fAnalysisChain->SetName("aodTree");
403 AliFatal("AOD case not yet implemented!!!");
404 }
405 else AliFatal("Inconsistent tree name - use esdTree or aodTree!");
406 //Event list
407 TEntryList *fGlobalList = new TEntryList();
408 AliXMLCollection *collection = AliXMLCollection::Open(collectionname);
409
410 collection->Reset();
411 while (collection->Next()) {
412 AliInfo(Form("Adding: %s",collection->GetTURL("")));
413 fAnalysisChain->Add(collection->GetTURL(""));
414 TEntryList *list = (TEntryList *)collection->GetEventList("");
415 list->SetTreeName(fAliceFile.Data());
416 list->SetFileName(collection->GetTURL(""));
417 fGlobalList->Add(list);
418 iAccepted += list->GetN();
419 }
420
421 fAnalysisChain->SetEntryList(fGlobalList,"ne");
422
423 AliInfo(Form("Number of selected events: %d",iAccepted));
424
425 return fAnalysisChain;
426}