1 /**************************************************************************
2 * Author: Panos Christakoglou. *
3 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
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 //-----------------------------------------------------------------
23 #include <Riostream.h>
27 #include <TEventList.h>
28 #include <TEntryList.h>
29 #include <TTreeFormula.h>
32 #include <TGridResult.h>
36 #include "AliRunTag.h"
37 #include "AliEventTag.h"
38 #include "AliTagAnalysis.h"
39 #include "AliEventTagCuts.h"
40 #include "AliDetectorTagCuts.h"
41 #include "AliLHCTagCuts.h"
42 #include "AliRunTagCuts.h"
43 #include "AliXMLCollection.h"
47 ClassImp(AliTagAnalysis)
49 //___________________________________________________________________________
50 AliTagAnalysis::AliTagAnalysis():
57 //Default constructor for a AliTagAnalysis
60 //___________________________________________________________________________
61 AliTagAnalysis::AliTagAnalysis(const char* type):
68 //constructor for a AliTagAnalysis
71 //___________________________________________________________________________
72 AliTagAnalysis::~AliTagAnalysis() {
73 //Default destructor for a AliTagAnalysis
74 if(ftagresult) delete ftagresult;
75 if(fChain) delete fChain;
76 if(fGlobalList) delete fGlobalList;
79 //___________________________________________________________________________
80 Bool_t AliTagAnalysis::AddTagsFile(const char *alienUrl) {
81 // Add a single tags file to the chain
85 if (! fChain) fChain = new TChain("T");
87 TFile *f = TFile::Open(alienUrl,"READ");
88 fChain->Add(alienUrl);
89 AliInfo(Form("Chained tag files: %d ",fChain->GetEntries()));
92 if (fChain->GetEntries() == 0 )
98 //___________________________________________________________________________
99 void 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;
105 if (! fChain) fChain = new TChain("T");
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!!!");
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;
119 fTagFilename += name;
121 fChain->Add(fTagFilename);
122 printf("Tag file %s\n", fTagFilename.Data());
126 AliInfo(Form("Chained tag files: %d ",fChain->GetEntries()));
132 //___________________________________________________________________________
133 TChain * AliTagAnalysis::ChainGridTags(TGridResult *res) {
134 //Loops overs the entries of the TGridResult
135 //Chains the tags that are stored in the GRID
137 Int_t nEntries = ftagresult->GetEntries();
139 if (! fChain) fChain = new TChain("T");
141 TString gridname = "alien://";
144 for(Int_t i = 0; i < nEntries; i++) {
145 alienUrl = ftagresult->GetKey(i,"turl");
146 fChain->Add(alienUrl);
152 //___________________________________________________________________________
153 TChain *AliTagAnalysis::QueryTags(AliRunTagCuts *runTagCuts,
154 AliLHCTagCuts *lhcTagCuts,
155 AliDetectorTagCuts *detTagCuts,
156 AliEventTagCuts *evTagCuts) {
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........"));
164 if(fAnalysisType == "ESD") aliceFile = "esdTree";
165 else if(fAnalysisType == "AOD") {
166 aliceFile = "aodTree";
169 else AliFatal("Only ESD and AOD type is implemented!!!");
172 TChain *esdChain = new TChain(aliceFile.Data());
174 fGlobalList = new TEntryList();
176 //Defining tag objects
177 AliRunTag *tag = new AliRunTag;
178 AliEventTag *evTag = new AliEventTag;
179 fChain->SetBranchAddress("AliTAG",&tag);
186 TEntryList* localList = new TEntryList();
193 for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
194 fChain->GetEntry(iTagFiles);
195 TTree* tree = fChain->GetTree();
197 // Fix for aod tags: for each tree in the chain, merge the entries
199 cEntries = tree->GetEntries();
204 if(runTagCuts->IsAccepted(tag)) {
205 if(lhcTagCuts->IsAccepted(tag->GetLHCTag())) {
206 if(detTagCuts->IsAccepted(tag->GetDetectorTags())) {
207 if ((iev == 0) || !aod) localList->Reset();
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();
215 localList->SetTreeName(aliceFile.Data());
216 if(turl!="") localList->SetFileName(turl.Data());
217 else localList->SetFileName(path.Data());
219 if(evTagCuts->IsAccepted(evTag)) {
220 if(aod) localList->Enter(iev);
221 else localList->Enter(i);
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);
237 AliInfo(Form("Accepted events: %d",iAccepted));
239 esdChain->SetEntryList(fGlobalList,"ne");
245 //___________________________________________________________________________
246 TChain *AliTagAnalysis::QueryTags(const char *fRunCut,
248 const char *fDetectorCut,
249 const char *fEventCut) {
250 //Queries the tag chain using the defined
251 //event tag cuts from the AliEventTagCuts object
252 //and returns a TChain along with the associated TEventList
253 AliInfo(Form("Querying the tags........"));
257 if(fAnalysisType == "ESD") aliceFile = "esdTree";
258 else if(fAnalysisType == "AOD") {
259 aliceFile = "aodTree";
262 else AliFatal("Only ESD and AOD type is implemented!!!");
265 TChain *esdChain = new TChain(aliceFile.Data());
267 fGlobalList = new TEntryList();
269 //Defining tag objects
270 AliRunTag *tag = new AliRunTag;
271 AliEventTag *evTag = new AliEventTag;
272 fChain->SetBranchAddress("AliTAG",&tag);
278 TTreeFormula *fRunFormula = new TTreeFormula("fRun",fRunCut,fChain);
279 TTreeFormula *fLHCFormula = new TTreeFormula("fLHC",fLHCCut,fChain);
280 TTreeFormula *fDetectorFormula = new TTreeFormula("fDetector",fDetectorCut,fChain);
281 TTreeFormula *fEventFormula = new TTreeFormula("fEvent",fEventCut,fChain);
283 TEntryList* localList = new TEntryList();
291 for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
292 fChain->GetEntry(iTagFiles);
293 if (current != fChain->GetTreeNumber()) {
294 fRunFormula->UpdateFormulaLeaves();
295 fLHCFormula->UpdateFormulaLeaves();
296 fDetectorFormula->UpdateFormulaLeaves();
297 fEventFormula->UpdateFormulaLeaves();
298 // Fix for aod tags: for each tree in the chain, merge the entries
299 cEntries = (fChain->GetTree())->GetEntries();
303 current = fChain->GetTreeNumber();
305 if(fRunFormula->EvalInstance(iTagFiles) == 1) {
306 if(fLHCFormula->EvalInstance(iTagFiles) == 1) {
307 if(fDetectorFormula->EvalInstance(iTagFiles) == 1) {
308 if ((iev == 0) || !aod) localList->Reset();
309 Int_t iEvents = fEventFormula->GetNdata();
310 const TClonesArray *tagList = tag->GetEventTags();
311 for(Int_t i = 0; i < iEvents; i++) {
312 evTag = (AliEventTag *) tagList->At(i);
313 guid = evTag->GetGUID();
314 turl = evTag->GetTURL();
315 path = evTag->GetPath();
316 localList->SetTreeName(aliceFile.Data());
317 localList->SetFileName(turl.Data());
318 if(fEventFormula->EvalInstance(i) == 1) {
319 if(aod) localList->Enter(iev);
320 else localList->Enter(i);
325 if ((ientry == cEntries-1) || !aod) {
326 if(path != "") esdChain->AddFile(path);
327 else if(turl != "") esdChain->AddFile(turl);
328 fGlobalList->Add(localList);
329 iAccepted += localList->GetN();
337 AliInfo(Form("Accepted events: %d",iAccepted));
338 esdChain->SetEntryList(fGlobalList,"ne");
344 //___________________________________________________________________________
345 Bool_t AliTagAnalysis::CreateXMLCollection(const char* name,
346 AliRunTagCuts *runTagCuts,
347 AliLHCTagCuts *lhcTagCuts,
348 AliDetectorTagCuts *detTagCuts,
349 AliEventTagCuts *evTagCuts) {
350 //Queries the tag chain using the defined
351 //event tag cuts from the AliEventTagCuts object
352 //and returns a XML collection
353 AliInfo(Form("Creating the collection........"));
356 if(fAnalysisType == "AOD") aod = kTRUE;
359 AliXMLCollection *collection = new AliXMLCollection();
360 collection->SetCollectionName(name);
361 collection->WriteHeader();
368 TEntryList* localList = new TEntryList();
374 //Defining tag objects
375 AliRunTag *tag = new AliRunTag;
376 AliEventTag *evTag = new AliEventTag;
377 fChain->SetBranchAddress("AliTAG",&tag);
379 for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
381 fChain->GetEntry(iTagFiles);
382 TTree* tree = fChain->GetTree();
384 // Fix for aod tags: for each tree in the chain, merge the entries
386 cEntries = tree->GetEntries();
391 if ((iev == 0) || !aod) localList->Reset();
392 if(runTagCuts->IsAccepted(tag)) {
393 if(lhcTagCuts->IsAccepted(tag->GetLHCTag())) {
394 if(detTagCuts->IsAccepted(tag->GetDetectorTags())) {
395 Int_t iEvents = tag->GetNEvents();
396 const TClonesArray *tagList = tag->GetEventTags();
397 for(Int_t i = 0; i < iEvents; i++) {
398 evTag = (AliEventTag *) tagList->At(i);
399 guid = evTag->GetGUID();
400 turl = evTag->GetTURL();
401 lfn = turl(8,turl.Length());
402 if(evTagCuts->IsAccepted(evTag)) {
403 if(aod) localList->Enter(iev);
404 else localList->Enter(i);
408 if ((ientry == cEntries-1) || !aod) {
409 collection->WriteBody(iTagFiles+1,guid,lfn,turl,localList);
410 iAccepted += localList->GetN();
418 collection->Export();
424 //___________________________________________________________________________
425 Bool_t AliTagAnalysis::CreateXMLCollection(const char* name,
428 const char *fDetectorCut,
429 const char *fEventCut) {
430 //Queries the tag chain using the defined
431 //event tag cuts from the AliEventTagCuts object
432 //and returns a XML collection
433 AliInfo(Form("Creating the collection........"));
436 if(fAnalysisType == "AOD") aod = kTRUE;
438 AliXMLCollection *collection = new AliXMLCollection();
439 collection->SetCollectionName(name);
440 collection->WriteHeader();
445 TEntryList* localList = new TEntryList();
453 //Defining tag objects
454 AliRunTag *tag = new AliRunTag;
455 AliEventTag *evTag = new AliEventTag;
456 fChain->SetBranchAddress("AliTAG",&tag);
458 TTreeFormula *fRunFormula = new TTreeFormula("fRun",fRunCut,fChain);
459 TTreeFormula *fLHCFormula = new TTreeFormula("fLHC",fLHCCut,fChain);
460 TTreeFormula *fDetectorFormula = new TTreeFormula("fDetector",fDetectorCut,fChain);
461 TTreeFormula *fEventFormula = new TTreeFormula("fEvent",fEventCut,fChain);
464 for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
466 fChain->GetEntry(iTagFiles);
467 if (current != fChain->GetTreeNumber()) {
468 fRunFormula->UpdateFormulaLeaves();
469 fLHCFormula->UpdateFormulaLeaves();
470 fDetectorFormula->UpdateFormulaLeaves();
471 fEventFormula->UpdateFormulaLeaves();
472 // Fix for aod tags: for each tree in the chain, merge the entries
473 cEntries = (fChain->GetTree())->GetEntries();
477 current = fChain->GetTreeNumber();
480 if ((iev == 0) || !aod) localList->Reset();
481 if(fRunFormula->EvalInstance(iTagFiles) == 1) {
482 if(fLHCFormula->EvalInstance(iTagFiles) == 1) {
483 if(fDetectorFormula->EvalInstance(iTagFiles) == 1) {
484 Int_t iEvents = fEventFormula->GetNdata();
485 const TClonesArray *tagList = tag->GetEventTags();
486 for(Int_t i = 0; i < iEvents; i++) {
487 evTag = (AliEventTag *) tagList->At(i);
488 guid = evTag->GetGUID();
489 turl = evTag->GetTURL();
490 lfn = turl(8,turl.Length());
491 if(fEventFormula->EvalInstance(i) == 1) {
492 if(aod) localList->Enter(iev);
493 else localList->Enter(i);
497 if ((ientry == cEntries-1) || !aod) {
498 collection->WriteBody(iTagFiles+1,guid,lfn,turl,localList);
499 iAccepted += localList->GetN();
506 collection->Export();
512 //___________________________________________________________________________
513 TChain *AliTagAnalysis::GetInputChain(const char* system, const char *wn) {
514 //returns the chain+event list - used in batch sessions
515 // this function will be removed once the new root
516 // improvements are committed
517 TString fsystem = system;
520 TChain *fAnalysisChain = 0;
521 if(fAnalysisType == "ESD") fAnalysisChain = new TChain("esdTree");
522 else if(fAnalysisType == "AOD") fAnalysisChain = new TChain("aodTree");
523 else AliFatal("Only ESD and AOD type is implemented!!!");
526 TEventList *fEventList = new TEventList();
527 AliXMLCollection *collection = AliXMLCollection::Open(wn);
530 while (collection->Next()) {
531 AliInfo(Form("Adding: %s",collection->GetTURL("")));
532 fAnalysisChain->Add(collection->GetTURL(""));
533 TEntryList *list = (TEntryList *)collection->GetEventList("");
534 for(Int_t i = 0; i < list->GetN(); i++) fEventList->Enter(iAccepted+list->GetEntry(i));
536 if(fsystem == "pp") iAccepted += 100;
537 else if(fsystem == "PbPb") iAccepted += 1;
540 fAnalysisChain->SetEventList(fEventList);
542 AliInfo(Form("Number of selected events: %d",fEventList->GetN()));
544 return fAnalysisChain;
547 //___________________________________________________________________________
548 TChain *AliTagAnalysis::GetChainFromCollection(const char* collectionname,
549 const char* treename) {
550 //returns the TChain+TEntryList object- used in batch sessions
551 TString aliceFile = treename;
553 TChain *fAnalysisChain = 0;
554 if(aliceFile == "esdTree") fAnalysisChain = new TChain("esdTree");
555 else if(aliceFile == "aodTree") fAnalysisChain = new TChain("aodTree");
556 else AliFatal("Inconsistent tree name - use esdTree or aodTree!");
559 fGlobalList = new TEntryList();
560 AliXMLCollection *collection = AliXMLCollection::Open(collectionname);
563 while (collection->Next()) {
564 AliInfo(Form("Adding: %s",collection->GetTURL("")));
565 fAnalysisChain->Add(collection->GetTURL(""));
566 TEntryList *list = (TEntryList *)collection->GetEventList("");
567 list->SetTreeName(aliceFile.Data());
568 list->SetFileName(collection->GetTURL(""));
569 fGlobalList->Add(list);
570 iAccepted += list->GetN();
573 fAnalysisChain->SetEntryList(fGlobalList,"ne");
575 AliInfo(Form("Number of selected events: %d",iAccepted));
577 return fAnalysisChain;