]> git.uio.no Git - u/mrichter/AliRoot.git/blame - ANALYSIS/AliTagAnalysis.cxx
matching performance ITS-TPC-TRD class
[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
cd305eb1 23#include <Riostream.h>
d08133e6 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
31//ROOT-AliEn
32#include <TGridResult.h>
33
34#include "AliLog.h"
35
36#include "AliRunTag.h"
37#include "AliEventTag.h"
38#include "AliTagAnalysis.h"
39#include "AliEventTagCuts.h"
2ec6a1d3 40#include "AliDetectorTagCuts.h"
41#include "AliLHCTagCuts.h"
d08133e6 42#include "AliRunTagCuts.h"
43#include "AliXMLCollection.h"
44
45class TTree;
46
47ClassImp(AliTagAnalysis)
48
49//___________________________________________________________________________
50AliTagAnalysis::AliTagAnalysis():
51 TObject(),
52 ftagresult(0x0),
53 fTagDirName(),
6cfbb923 54 fChain(0x0),
cd305eb1 55 fAnalysisType(),
56 fGlobalList(0) {
d08133e6 57 //Default constructor for a AliTagAnalysis
58}
59
6cfbb923 60//___________________________________________________________________________
61AliTagAnalysis::AliTagAnalysis(const char* type):
62 TObject(),
63 ftagresult(0x0),
64 fTagDirName(),
65 fChain(0x0),
cd305eb1 66 fAnalysisType(type),
67 fGlobalList(0) {
6cfbb923 68 //constructor for a AliTagAnalysis
69}
70
d08133e6 71//___________________________________________________________________________
72AliTagAnalysis::~AliTagAnalysis() {
cd305eb1 73 //Default destructor for a AliTagAnalysis
74 if(ftagresult) delete ftagresult;
75 if(fChain) delete fChain;
76 if(fGlobalList) delete fGlobalList;
d08133e6 77}
78
79//___________________________________________________________________________
80Bool_t AliTagAnalysis::AddTagsFile(const char *alienUrl) {
d08133e6 81 // Add a single tags file to the chain
82
83 Bool_t rv = kTRUE ;
84
85 if (! fChain) fChain = new TChain("T");
86
87 TFile *f = TFile::Open(alienUrl,"READ");
88 fChain->Add(alienUrl);
89 AliInfo(Form("Chained tag files: %d ",fChain->GetEntries()));
90 delete f;
91
92 if (fChain->GetEntries() == 0 )
93 rv = kFALSE ;
94
95 return rv ;
96}
97
98//___________________________________________________________________________
99void AliTagAnalysis::ChainLocalTags(const char *dirname) {
100 //Searches the entries of the provided direcory
101 //Chains the tags that are stored locally
102 fTagDirName = dirname;
103 TString fTagFilename;
104
105 if (! fChain) fChain = new TChain("T");
6cfbb923 106 const char * tagPattern = 0x0;
107 if(fAnalysisType == "ESD") tagPattern = "ESD.tag.root";
108 else if(fAnalysisType == "AOD") tagPattern = "AOD.tag.root";
109 else AliFatal("Only ESD and AOD type is implemented!!!");
110
d08133e6 111 // Open the working directory
112 void * dirp = gSystem->OpenDirectory(fTagDirName);
113 const char * name = 0x0;
114 // Add all files matching *pattern* to the chain
115 while((name = gSystem->GetDirEntry(dirp))) {
116 if (strstr(name,tagPattern)) {
117 fTagFilename = fTagDirName;
118 fTagFilename += "/";
119 fTagFilename += name;
120
121 fChain->Add(fTagFilename);
68550043 122 printf("Tag file %s\n", fTagFilename.Data());
123
d08133e6 124 }//pattern check
125 }//directory loop
126 AliInfo(Form("Chained tag files: %d ",fChain->GetEntries()));
68550043 127 fChain->ls();
128
d08133e6 129}
130
131
132//___________________________________________________________________________
727d922c 133TChain * AliTagAnalysis::ChainGridTags(TGridResult *res) {
d08133e6 134 //Loops overs the entries of the TGridResult
135 //Chains the tags that are stored in the GRID
136 ftagresult = res;
137 Int_t nEntries = ftagresult->GetEntries();
138
139 if (! fChain) fChain = new TChain("T");
140
141 TString gridname = "alien://";
142 TString alienUrl;
143
144 for(Int_t i = 0; i < nEntries; i++) {
145 alienUrl = ftagresult->GetKey(i,"turl");
146 fChain->Add(alienUrl);
727d922c 147 }//grid result loop
148 return fChain;
d08133e6 149}
150
151
152//___________________________________________________________________________
cd305eb1 153TChain *AliTagAnalysis::QueryTags(AliRunTagCuts *runTagCuts,
154 AliLHCTagCuts *lhcTagCuts,
155 AliDetectorTagCuts *detTagCuts,
156 AliEventTagCuts *evTagCuts) {
d08133e6 157 //Queries the tag chain using the defined
158 //event tag cuts from the AliEventTagCuts object
159 //and returns a TChain along with the associated TEventList
160 AliInfo(Form("Querying the tags........"));
b97856f9 161
68550043 162 Bool_t aod = kFALSE;
163 TString aliceFile;
164 if(fAnalysisType == "ESD") aliceFile = "esdTree";
165 else if(fAnalysisType == "AOD") {
166 aliceFile = "aodTree";
167 aod = kTRUE;
168 }
6cfbb923 169 else AliFatal("Only ESD and AOD type is implemented!!!");
b97856f9 170
d08133e6 171 //ESD file chain
68550043 172 TChain *esdChain = new TChain(aliceFile.Data());
b97856f9 173 //global entry list
cd305eb1 174 fGlobalList = new TEntryList();
d08133e6 175
176 //Defining tag objects
5a561fa0 177 AliRunTag *tag = new AliRunTag;
178 AliEventTag *evTag = new AliEventTag;
d08133e6 179 fChain->SetBranchAddress("AliTAG",&tag);
180
9b4aee57 181 TString guid;
182 TString turl;
183 TString path;
d08133e6 184
68550043 185 TTree* cTree = 0;
186 TEntryList* localList = 0;
187
d08133e6 188 Int_t iAccepted = 0;
68550043 189 Int_t iev = 0;
190 Int_t ientry = 0;
191 Int_t cEntries = 0;
192
d08133e6 193 for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
194 fChain->GetEntry(iTagFiles);
68550043 195 TTree* tree = fChain->GetTree();
196 if (cTree != tree) {
197 // Fix for aod tags: for each tree in the chain, merge the entries
198 cTree = tree;
199 cEntries = tree->GetEntries();
200 iev = 0;
201 ientry = 0;
202 }
203
2ec6a1d3 204 if(runTagCuts->IsAccepted(tag)) {
205 if(lhcTagCuts->IsAccepted(tag->GetLHCTag())) {
206 if(detTagCuts->IsAccepted(tag->GetDetectorTags())) {
68550043 207 if ((iev == 0) || !aod) localList = new TEntryList();
2ec6a1d3 208 Int_t iEvents = tag->GetNEvents();
209 const TClonesArray *tagList = tag->GetEventTags();
210 for(Int_t i = 0; i < iEvents; i++) {
211 evTag = (AliEventTag *) tagList->At(i);
212 guid = evTag->GetGUID();
213 turl = evTag->GetTURL();
214 path = evTag->GetPath();
68550043 215 localList->SetTreeName(aliceFile.Data());
216 if(turl!="") localList->SetFileName(turl.Data());
217 else localList->SetFileName(path.Data());
218
219 if(evTagCuts->IsAccepted(evTag)) {
220 if(aod) localList->Enter(iev);
221 else localList->Enter(i);
222 }
223 iev++;
2ec6a1d3 224 }//event loop
68550043 225 if ((ientry == cEntries-1) || !aod) {
226 iAccepted += localList->GetN();
227 if(path != "") esdChain->AddFile(path);
228 else if(turl != "") esdChain->AddFile(turl);
229 fGlobalList->Add(localList);
230 }
2ec6a1d3 231 }//detector tag cuts
232 }//lhc tag cuts
d08133e6 233 }//run tags cut
5a561fa0 234 tag->Clear();
68550043 235 ientry++;
d08133e6 236 }//tag file loop
b97856f9 237 AliInfo(Form("Accepted events: %d",iAccepted));
5d0e8364 238 esdChain->ls();
68550043 239 esdChain->SetEntryList(fGlobalList,"ne");
d08133e6 240
68550043 241 return esdChain;
d08133e6 242}
243
244//___________________________________________________________________________
cd305eb1 245TChain *AliTagAnalysis::QueryTags(const char *fRunCut,
246 const char *fLHCCut,
247 const char *fDetectorCut,
248 const char *fEventCut) {
d08133e6 249 //Queries the tag chain using the defined
250 //event tag cuts from the AliEventTagCuts object
251 //and returns a TChain along with the associated TEventList
252 AliInfo(Form("Querying the tags........"));
253
68550043 254 Bool_t aod = kFALSE;
255 TString aliceFile;
256 if(fAnalysisType == "ESD") aliceFile = "esdTree";
257 else if(fAnalysisType == "AOD") {
258 aliceFile = "aodTree";
259 aod = kTRUE;
260 }
6cfbb923 261 else AliFatal("Only ESD and AOD type is implemented!!!");
b97856f9 262
263 //ESD file chain
68550043 264 TChain *esdChain = new TChain(aliceFile.Data());
b97856f9 265 //global entry list
cd305eb1 266 fGlobalList = new TEntryList();
d08133e6 267
268 //Defining tag objects
269 AliRunTag *tag = new AliRunTag;
270 AliEventTag *evTag = new AliEventTag;
271 fChain->SetBranchAddress("AliTAG",&tag);
272
9b4aee57 273 TString guid;
274 TString turl;
275 TString path;
d08133e6 276
277 TTreeFormula *fRunFormula = new TTreeFormula("fRun",fRunCut,fChain);
2ec6a1d3 278 TTreeFormula *fLHCFormula = new TTreeFormula("fLHC",fLHCCut,fChain);
279 TTreeFormula *fDetectorFormula = new TTreeFormula("fDetector",fDetectorCut,fChain);
280 TTreeFormula *fEventFormula = new TTreeFormula("fEvent",fEventCut,fChain);
d08133e6 281
68550043 282 TEntryList* localList = 0;
283
284 Int_t iev = 0;
285 Int_t ientry = 0;
286 Int_t cEntries = 0;
287 Int_t current = -1;
d08133e6 288 Int_t iAccepted = 0;
68550043 289
cd305eb1 290 for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
d08133e6 291 fChain->GetEntry(iTagFiles);
292 if (current != fChain->GetTreeNumber()) {
293 fRunFormula->UpdateFormulaLeaves();
2ec6a1d3 294 fLHCFormula->UpdateFormulaLeaves();
295 fDetectorFormula->UpdateFormulaLeaves();
d08133e6 296 fEventFormula->UpdateFormulaLeaves();
68550043 297 // Fix for aod tags: for each tree in the chain, merge the entries
298 cEntries = (fChain->GetTree())->GetEntries();
299 iev = 0;
300 ientry = 0;
301 //
d08133e6 302 current = fChain->GetTreeNumber();
303 }
304 if(fRunFormula->EvalInstance(iTagFiles) == 1) {
2ec6a1d3 305 if(fLHCFormula->EvalInstance(iTagFiles) == 1) {
68550043 306 if(fDetectorFormula->EvalInstance(iTagFiles) == 1) {
307 if ((iev == 0) || !aod) localList = new TEntryList();
2ec6a1d3 308 Int_t iEvents = fEventFormula->GetNdata();
309 const TClonesArray *tagList = tag->GetEventTags();
310 for(Int_t i = 0; i < iEvents; i++) {
311 evTag = (AliEventTag *) tagList->At(i);
312 guid = evTag->GetGUID();
313 turl = evTag->GetTURL();
314 path = evTag->GetPath();
68550043 315 localList->SetTreeName(aliceFile.Data());
316 localList->SetFileName(turl.Data());
317 if(fEventFormula->EvalInstance(i) == 1) {
318 if(aod) localList->Enter(iev);
319 else localList->Enter(i);
320 }
321 iev++;
2ec6a1d3 322 }//event loop
68550043 323
324 if ((ientry == cEntries-1) || !aod) {
325 if(path != "") esdChain->AddFile(path);
326 else if(turl != "") esdChain->AddFile(turl);
327 fGlobalList->Add(localList);
328 iAccepted += localList->GetN();
329 }
2ec6a1d3 330 }//detector tag cuts
331 }//lhc tag cuts
d08133e6 332 }//run tag cut
68550043 333 tag->Clear();
334 ientry++;
d08133e6 335 }//tag file loop
b97856f9 336 AliInfo(Form("Accepted events: %d",iAccepted));
68550043 337 esdChain->SetEntryList(fGlobalList,"ne");
d08133e6 338
68550043 339 return esdChain;
d08133e6 340}
341
342//___________________________________________________________________________
cd305eb1 343Bool_t AliTagAnalysis::CreateXMLCollection(const char* name,
344 AliRunTagCuts *runTagCuts,
345 AliLHCTagCuts *lhcTagCuts,
346 AliDetectorTagCuts *detTagCuts,
347 AliEventTagCuts *evTagCuts) {
d08133e6 348 //Queries the tag chain using the defined
349 //event tag cuts from the AliEventTagCuts object
350 //and returns a XML collection
351 AliInfo(Form("Creating the collection........"));
352
68550043 353 Bool_t aod = kFALSE;
354 if(fAnalysisType == "AOD") aod = kTRUE;
355
356
d08133e6 357 AliXMLCollection *collection = new AliXMLCollection();
358 collection->SetCollectionName(name);
359 collection->WriteHeader();
360
9b4aee57 361 TString guid;
362 TString turl;
363 TString lfn;
d08133e6 364
68550043 365 TTree* cTree = 0;
366 TEntryList* localList = 0;
367 Int_t iAccepted = 0;
368 Int_t iev = 0;
369 Int_t ientry = 0;
370 Int_t cEntries = 0;
371
d08133e6 372 //Defining tag objects
373 AliRunTag *tag = new AliRunTag;
374 AliEventTag *evTag = new AliEventTag;
375 fChain->SetBranchAddress("AliTAG",&tag);
376
377 for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
68550043 378
d08133e6 379 fChain->GetEntry(iTagFiles);
68550043 380 TTree* tree = fChain->GetTree();
381 if (cTree != tree) {
382 // Fix for aod tags: for each tree in the chain, merge the entries
383 cTree = tree;
384 cEntries = tree->GetEntries();
385 iev = 0;
386 ientry = 0;
387 }
388 //Event list
389 if ((iev == 0) || !aod) localList = new TEntryList();
2ec6a1d3 390 if(runTagCuts->IsAccepted(tag)) {
391 if(lhcTagCuts->IsAccepted(tag->GetLHCTag())) {
392 if(detTagCuts->IsAccepted(tag->GetDetectorTags())) {
393 Int_t iEvents = tag->GetNEvents();
394 const TClonesArray *tagList = tag->GetEventTags();
395 for(Int_t i = 0; i < iEvents; i++) {
396 evTag = (AliEventTag *) tagList->At(i);
397 guid = evTag->GetGUID();
398 turl = evTag->GetTURL();
399 lfn = turl(8,turl.Length());
68550043 400 if(evTagCuts->IsAccepted(evTag)) {
401 if(aod) localList->Enter(iev);
402 else localList->Enter(i);
403 }
404 iev++;
2ec6a1d3 405 }//event loop
68550043 406 if ((ientry == cEntries-1) || !aod) {
407 collection->WriteBody(iTagFiles+1,guid,lfn,turl,localList);
5d0e8364 408 iAccepted += localList->GetN();
68550043 409 }
2ec6a1d3 410 }//detector tag cuts
411 }//lhc tag cuts
d08133e6 412 }//run tag cuts
5a561fa0 413 tag->Clear();
68550043 414 ientry++;
d08133e6 415 }//tag file loop
416 collection->Export();
417
418 return kTRUE;
419}
420
421//___________________________________________________________________________
cd305eb1 422Bool_t AliTagAnalysis::CreateXMLCollection(const char* name,
423 const char *fRunCut,
424 const char *fLHCCut,
425 const char *fDetectorCut,
426 const char *fEventCut) {
d08133e6 427 //Queries the tag chain using the defined
428 //event tag cuts from the AliEventTagCuts object
429 //and returns a XML collection
430 AliInfo(Form("Creating the collection........"));
431
68550043 432 Bool_t aod = kFALSE;
433 if(fAnalysisType == "AOD") aod = kTRUE;
434
d08133e6 435 AliXMLCollection *collection = new AliXMLCollection();
436 collection->SetCollectionName(name);
437 collection->WriteHeader();
438
9b4aee57 439 TString guid;
440 TString turl;
441 TString lfn;
68550043 442 TEntryList* localList = 0;
d08133e6 443
cd305eb1 444
68550043 445 Int_t iAccepted = 0;
446 Int_t iev = 0;
447 Int_t ientry = 0;
448 Int_t cEntries = 0;
cd305eb1 449
cd305eb1 450 //Defining tag objects
451 AliRunTag *tag = new AliRunTag;
452 AliEventTag *evTag = new AliEventTag;
453 fChain->SetBranchAddress("AliTAG",&tag);
454
455 TTreeFormula *fRunFormula = new TTreeFormula("fRun",fRunCut,fChain);
456 TTreeFormula *fLHCFormula = new TTreeFormula("fLHC",fLHCCut,fChain);
457 TTreeFormula *fDetectorFormula = new TTreeFormula("fDetector",fDetectorCut,fChain);
458 TTreeFormula *fEventFormula = new TTreeFormula("fEvent",fEventCut,fChain);
459
460 Int_t current = -1;
461 for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
68550043 462
cd305eb1 463 fChain->GetEntry(iTagFiles);
464 if (current != fChain->GetTreeNumber()) {
465 fRunFormula->UpdateFormulaLeaves();
466 fLHCFormula->UpdateFormulaLeaves();
467 fDetectorFormula->UpdateFormulaLeaves();
468 fEventFormula->UpdateFormulaLeaves();
68550043 469 // Fix for aod tags: for each tree in the chain, merge the entries
470 cEntries = (fChain->GetTree())->GetEntries();
471 iev = 0;
472 ientry = 0;
473 //
cd305eb1 474 current = fChain->GetTreeNumber();
475 }
68550043 476 //Event list
477 if ((iev == 0) || !aod) localList = new TEntryList();
cd305eb1 478 if(fRunFormula->EvalInstance(iTagFiles) == 1) {
479 if(fLHCFormula->EvalInstance(iTagFiles) == 1) {
480 if(fDetectorFormula->EvalInstance(iTagFiles) == 1) {
481 Int_t iEvents = fEventFormula->GetNdata();
482 const TClonesArray *tagList = tag->GetEventTags();
483 for(Int_t i = 0; i < iEvents; i++) {
484 evTag = (AliEventTag *) tagList->At(i);
485 guid = evTag->GetGUID();
486 turl = evTag->GetTURL();
487 lfn = turl(8,turl.Length());
68550043 488 if(fEventFormula->EvalInstance(i) == 1) {
489 if(aod) localList->Enter(iev);
490 else localList->Enter(i);
491 }
492 iev++;
cd305eb1 493 }//event loop
68550043 494 if ((ientry == cEntries-1) || !aod) {
495 collection->WriteBody(iTagFiles+1,guid,lfn,turl,localList);
5d0e8364 496 iAccepted += localList->GetN();
68550043 497 }
cd305eb1 498 }//detector tag cuts
499 }//lhc tag cuts
500 }//run tag cuts
68550043 501 ientry++;
cd305eb1 502 }//tag file loop
68550043 503 collection->Export();
cd305eb1 504 return kTRUE;
505}
506
d08133e6 507//___________________________________________________________________________
508TChain *AliTagAnalysis::GetInputChain(const char* system, const char *wn) {
509 //returns the chain+event list - used in batch sessions
510 // this function will be removed once the new root
511 // improvements are committed
512 TString fsystem = system;
513 Int_t iAccepted = 0;
6cfbb923 514
88fb0edc 515 TChain *fAnalysisChain = 0;
516 if(fAnalysisType == "ESD") fAnalysisChain = new TChain("esdTree");
517 else if(fAnalysisType == "AOD") fAnalysisChain = new TChain("aodTree");
6cfbb923 518 else AliFatal("Only ESD and AOD type is implemented!!!");
519
d08133e6 520 //Event list
521 TEventList *fEventList = new TEventList();
522 AliXMLCollection *collection = AliXMLCollection::Open(wn);
523
524 collection->Reset();
525 while (collection->Next()) {
526 AliInfo(Form("Adding: %s",collection->GetTURL("")));
6cfbb923 527 fAnalysisChain->Add(collection->GetTURL(""));
d08133e6 528 TEntryList *list = (TEntryList *)collection->GetEventList("");
529 for(Int_t i = 0; i < list->GetN(); i++) fEventList->Enter(iAccepted+list->GetEntry(i));
530
531 if(fsystem == "pp") iAccepted += 100;
532 else if(fsystem == "PbPb") iAccepted += 1;
533 }
534
6cfbb923 535 fAnalysisChain->SetEventList(fEventList);
d08133e6 536
537 AliInfo(Form("Number of selected events: %d",fEventList->GetN()));
538
6cfbb923 539 return fAnalysisChain;
d08133e6 540}
541
542//___________________________________________________________________________
cd305eb1 543TChain *AliTagAnalysis::GetChainFromCollection(const char* collectionname,
544 const char* treename) {
d08133e6 545 //returns the TChain+TEntryList object- used in batch sessions
68550043 546 TString aliceFile = treename;
d08133e6 547 Int_t iAccepted = 0;
88fb0edc 548 TChain *fAnalysisChain = 0;
68550043 549 if(aliceFile == "esdTree") fAnalysisChain = new TChain("esdTree");
550 else if(aliceFile == "aodTree") fAnalysisChain = new TChain("aodTree");
d08133e6 551 else AliFatal("Inconsistent tree name - use esdTree or aodTree!");
6cfbb923 552
d08133e6 553 //Event list
cd305eb1 554 fGlobalList = new TEntryList();
d08133e6 555 AliXMLCollection *collection = AliXMLCollection::Open(collectionname);
556
557 collection->Reset();
558 while (collection->Next()) {
559 AliInfo(Form("Adding: %s",collection->GetTURL("")));
560 fAnalysisChain->Add(collection->GetTURL(""));
561 TEntryList *list = (TEntryList *)collection->GetEventList("");
68550043 562 list->SetTreeName(aliceFile.Data());
d08133e6 563 list->SetFileName(collection->GetTURL(""));
564 fGlobalList->Add(list);
565 iAccepted += list->GetN();
566 }
567
568 fAnalysisChain->SetEntryList(fGlobalList,"ne");
569
570 AliInfo(Form("Number of selected events: %d",iAccepted));
571
572 return fAnalysisChain;
573}