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>
33 #include <TGridResult.h>
37 #include "AliRunTag.h"
38 #include "AliEventTag.h"
39 #include "AliTagAnalysis.h"
40 #include "AliEventTagCuts.h"
41 #include "AliDetectorTagCuts.h"
42 #include "AliLHCTagCuts.h"
43 #include "AliRunTagCuts.h"
44 #include "AliXMLCollection.h"
48 ClassImp(AliTagAnalysis)
50 //___________________________________________________________________________
51 AliTagAnalysis::AliTagAnalysis():
58 //Default constructor for a AliTagAnalysis
61 //___________________________________________________________________________
62 AliTagAnalysis::AliTagAnalysis(const char* type):
69 //constructor for a AliTagAnalysis
72 //___________________________________________________________________________
73 AliTagAnalysis::~AliTagAnalysis() {
74 //Default destructor for a AliTagAnalysis
75 if(ftagresult) delete ftagresult;
76 if(fChain) delete fChain;
77 if(fGlobalList) delete fGlobalList;
80 //___________________________________________________________________________
81 Bool_t AliTagAnalysis::AddTagsFile(const char *alienUrl) {
82 // Add a single tags file to the chain
86 if (! fChain) fChain = new TChain("T");
88 TFile *f = TFile::Open(alienUrl,"READ");
89 fChain->Add(alienUrl);
90 AliInfo(Form("Chained tag files: %d ",fChain->GetEntries()));
93 if (fChain->GetEntries() == 0 )
99 //___________________________________________________________________________
100 void AliTagAnalysis::ChainLocalTags(const char *dirname) {
101 //Searches the entries of the provided direcory
102 //Chains the tags that are stored locally
103 fTagDirName = dirname;
104 TString fTagFilename;
106 if (! fChain) fChain = new TChain("T");
107 const char * tagPattern = 0x0;
108 if(fAnalysisType == "ESD") tagPattern = "ESD.tag.root";
109 else if(fAnalysisType == "AOD") tagPattern = "AOD.tag.root";
110 else AliFatal("Only ESD and AOD type is implemented!!!");
112 // Open the working directory
113 void * dirp = gSystem->OpenDirectory(fTagDirName);
114 const char * name = 0x0;
115 // Add all files matching *pattern* to the chain
116 while((name = gSystem->GetDirEntry(dirp))) {
117 if (strstr(name,tagPattern)) {
118 fTagFilename = fTagDirName;
120 fTagFilename += name;
122 fChain->Add(fTagFilename);
123 printf("Tag file %s\n", fTagFilename.Data());
127 //AliInfo(Form("Chained tag files: %d ",fChain->GetEntries()));
128 // AliDebug(Form("Chained tag files: %d ",fChain->GetEntries()));
134 //___________________________________________________________________________
135 TChain * AliTagAnalysis::ChainGridTags(TGridResult *res) {
136 //Loops overs the entries of the TGridResult
137 //Chains the tags that are stored in the GRID
139 Int_t nEntries = ftagresult->GetEntries();
141 if (! fChain) fChain = new TChain("T");
143 TString gridname = "alien://";
146 for(Int_t i = 0; i < nEntries; i++) {
147 alienUrl = ftagresult->GetKey(i,"turl");
148 fChain->Add(alienUrl);
154 //___________________________________________________________________________
155 TChain *AliTagAnalysis::QueryTags(AliRunTagCuts *runTagCuts,
156 AliLHCTagCuts *lhcTagCuts,
157 AliDetectorTagCuts *detTagCuts,
158 AliEventTagCuts *evTagCuts) {
159 //Queries the tag chain using the defined
160 //event tag cuts from the AliEventTagCuts object
161 //and returns a TChain along with the associated TEventList
162 AliInfo(Form("Querying the tags........"));
166 if(fAnalysisType == "ESD") aliceFile = "esdTree";
167 else if(fAnalysisType == "AOD") {
168 aliceFile = "aodTree";
171 else AliFatal("Only ESD and AOD type is implemented!!!");
174 TChain *esdChain = new TChain(aliceFile.Data());
176 fGlobalList = new TEntryList();
178 //Defining tag objects
179 AliRunTag *tag = new AliRunTag;
180 AliEventTag *evTag = new AliEventTag;
181 fChain->SetBranchAddress("AliTAG",&tag);
188 TEntryList* localList = new TEntryList();
195 for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
196 fChain->GetEntry(iTagFiles);
197 TTree* tree = fChain->GetTree();
199 // Fix for aod tags: for each tree in the chain, merge the entries
201 cEntries = tree->GetEntries();
206 if(runTagCuts->IsAccepted(tag)) {
207 if(lhcTagCuts->IsAccepted(tag->GetLHCTag())) {
208 if(detTagCuts->IsAccepted(tag->GetDetectorTags())) {
209 if ((iev == 0) || !aod) localList->Reset();
210 Int_t iEvents = tag->GetNEvents();
211 const TClonesArray *tagList = tag->GetEventTags();
212 for(Int_t i = 0; i < iEvents; i++) {
213 evTag = (AliEventTag *) tagList->At(i);
214 guid = evTag->GetGUID();
215 turl = evTag->GetTURL();
216 path = evTag->GetPath();
217 localList->SetTreeName(aliceFile.Data());
218 if(turl!="") localList->SetFileName(turl.Data());
219 else localList->SetFileName(path.Data());
221 if(evTagCuts->IsAccepted(evTag)) {
222 if(aod) localList->Enter(iev);
223 else localList->Enter(i);
227 if ((ientry == cEntries-1) || !aod) {
228 iAccepted += localList->GetN();
229 if(path != "") esdChain->AddFile(path);
230 else if(turl != "") esdChain->AddFile(turl);
231 fGlobalList->Add(localList);
239 AliInfo(Form("Accepted events: %d",iAccepted));
241 esdChain->SetEntryList(fGlobalList,"ne");
248 //___________________________________________________________________________
249 TChain *AliTagAnalysis::QueryTags(const char *fRunCut,
251 const char *fDetectorCut,
252 const char *fEventCut) {
253 //Queries the tag chain using the defined
254 //event tag cuts from the AliEventTagCuts object
255 //and returns a TChain along with the associated TEventList
256 AliInfo(Form("Querying the tags........"));
260 if(fAnalysisType == "ESD") aliceFile = "esdTree";
261 else if(fAnalysisType == "AOD") {
262 aliceFile = "aodTree";
265 else AliFatal("Only ESD and AOD type is implemented!!!");
268 TChain *esdChain = new TChain(aliceFile.Data());
270 fGlobalList = new TEntryList();
272 //Defining tag objects
273 AliRunTag *tag = new AliRunTag;
274 AliEventTag *evTag = new AliEventTag;
275 fChain->SetBranchAddress("AliTAG",&tag);
281 TTreeFormula *fRunFormula = new TTreeFormula("fRun",fRunCut,fChain);
282 TTreeFormula *fLHCFormula = new TTreeFormula("fLHC",fLHCCut,fChain);
283 TTreeFormula *fDetectorFormula = new TTreeFormula("fDetector",fDetectorCut,fChain);
284 TTreeFormula *fEventFormula = new TTreeFormula("fEvent",fEventCut,fChain);
286 TEntryList* localList = new TEntryList();
294 for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
295 fChain->GetEntry(iTagFiles);
296 if (current != fChain->GetTreeNumber()) {
297 fRunFormula->UpdateFormulaLeaves();
298 fLHCFormula->UpdateFormulaLeaves();
299 fDetectorFormula->UpdateFormulaLeaves();
300 fEventFormula->UpdateFormulaLeaves();
301 // Fix for aod tags: for each tree in the chain, merge the entries
302 cEntries = (fChain->GetTree())->GetEntries();
306 current = fChain->GetTreeNumber();
308 if(fRunFormula->EvalInstance(iTagFiles) == 1) {
309 if(fLHCFormula->EvalInstance(iTagFiles) == 1) {
310 if(fDetectorFormula->EvalInstance(iTagFiles) == 1) {
311 if ((iev == 0) || !aod) localList->Reset();
312 Int_t iEvents = fEventFormula->GetNdata();
313 const TClonesArray *tagList = tag->GetEventTags();
314 for(Int_t i = 0; i < iEvents; i++) {
315 evTag = (AliEventTag *) tagList->At(i);
316 guid = evTag->GetGUID();
317 turl = evTag->GetTURL();
318 path = evTag->GetPath();
319 localList->SetTreeName(aliceFile.Data());
320 localList->SetFileName(turl.Data());
321 if(fEventFormula->EvalInstance(i) == 1) {
322 if(aod) localList->Enter(iev);
323 else localList->Enter(i);
328 if ((ientry == cEntries-1) || !aod) {
329 if(path != "") esdChain->AddFile(path);
330 else if(turl != "") esdChain->AddFile(turl);
331 fGlobalList->Add(localList);
332 iAccepted += localList->GetN();
340 AliInfo(Form("Accepted events: %d",iAccepted));
341 esdChain->SetEntryList(fGlobalList,"ne");
348 //___________________________________________________________________________
349 Bool_t AliTagAnalysis::CreateXMLCollection(const char* name,
350 AliRunTagCuts *runTagCuts,
351 AliLHCTagCuts *lhcTagCuts,
352 AliDetectorTagCuts *detTagCuts,
353 AliEventTagCuts *evTagCuts) {
354 //Queries the tag chain using the defined
355 //event tag cuts from the AliEventTagCuts object
356 //and returns a XML collection
357 AliInfo(Form("Creating the collection........"));
360 if(fAnalysisType == "AOD") aod = kTRUE;
363 AliXMLCollection *collection = new AliXMLCollection();
364 collection->SetCollectionName(name);
365 collection->WriteHeader();
372 TEntryList* localList = new TEntryList();
378 Int_t iRejectedRun = 0;
379 Int_t iRejectedLHC = 0;
380 Int_t iRejectedDet = 0;
381 Int_t iRejectedEvt = 0;
383 Int_t iTotalEvents = 0;
385 Int_t iAcceptedEvtInFile = 0;
386 Int_t iRejectedEvtInFile = 0;
388 //Defining tag objects
389 AliRunTag *tag = new AliRunTag;
390 AliEventTag *evTag = new AliEventTag;
391 fChain->SetBranchAddress("AliTAG",&tag);
393 for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
395 fChain->GetEntry(iTagFiles);
396 TTree* tree = fChain->GetTree();
398 // Fix for aod tags: for each tree in the chain, merge the entries
400 cEntries = tree->GetEntries();
405 iTotalEvents += tag->GetNEvents();
406 if ((iev == 0) || !aod) localList->Reset();
407 if(runTagCuts->IsAccepted(tag)) {
408 if(lhcTagCuts->IsAccepted(tag->GetLHCTag())) {
409 if(detTagCuts->IsAccepted(tag->GetDetectorTags())) {
410 Int_t iEvents = tag->GetNEvents();
411 const TClonesArray *tagList = tag->GetEventTags();
412 iRejectedEvtInFile = 0;
413 iAcceptedEvtInFile = 0;
414 for(Int_t i = 0; i < iEvents; i++) {
415 evTag = (AliEventTag *) tagList->At(i);
416 guid = evTag->GetGUID();
417 turl = evTag->GetTURL();
418 lfn = turl(8,turl.Length());
419 if(evTagCuts->IsAccepted(evTag)) {
420 if(aod) localList->Enter(iev);
421 else localList->Enter(i);
422 iAcceptedEvtInFile++;
426 iRejectedEvtInFile++;
430 if ((ientry == cEntries-1) || !aod) {
431 iAccepted += localList->GetN();
432 collection->WriteBody(iTagFiles+1,guid,lfn,turl,localList,iAcceptedEvtInFile,iRejectedEvtInFile);
436 iRejectedDet += tag->GetNEvents();
440 iRejectedLHC += tag->GetNEvents();
444 iRejectedRun += tag->GetNEvents();
449 collection->WriteSummary(iTotalEvents, iAccepted, iRejectedRun, iRejectedLHC, iRejectedDet, iRejectedEvt);
450 collection->Export();
457 //___________________________________________________________________________
458 Bool_t AliTagAnalysis::CreateXMLCollection(const char* name,
461 const char *fDetectorCut,
462 const char *fEventCut) {
463 //Queries the tag chain using the defined
464 //event tag cuts from the AliEventTagCuts object
465 //and returns a XML collection
466 AliInfo(Form("Creating the collection........"));
469 if(fAnalysisType == "AOD") aod = kTRUE;
471 AliXMLCollection *collection = new AliXMLCollection();
472 collection->SetCollectionName(name);
473 collection->WriteHeader();
478 TEntryList* localList = new TEntryList();
485 Int_t iRejectedRun = 0;
486 Int_t iRejectedLHC = 0;
487 Int_t iRejectedDet = 0;
488 Int_t iRejectedEvt = 0;
490 Int_t iTotalEvents = 0;
492 Int_t iAcceptedEvtInFile = 0;
493 Int_t iRejectedEvtInFile = 0;
495 //Defining tag objects
496 AliRunTag *tag = new AliRunTag;
497 AliEventTag *evTag = new AliEventTag;
498 fChain->SetBranchAddress("AliTAG",&tag);
500 TTreeFormula *fRunFormula = new TTreeFormula("fRun",fRunCut,fChain);
501 TTreeFormula *fLHCFormula = new TTreeFormula("fLHC",fLHCCut,fChain);
502 TTreeFormula *fDetectorFormula = new TTreeFormula("fDetector",fDetectorCut,fChain);
503 TTreeFormula *fEventFormula = new TTreeFormula("fEvent",fEventCut,fChain);
506 for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
508 fChain->GetEntry(iTagFiles);
509 if (current != fChain->GetTreeNumber()) {
510 fRunFormula->UpdateFormulaLeaves();
511 fLHCFormula->UpdateFormulaLeaves();
512 fDetectorFormula->UpdateFormulaLeaves();
513 fEventFormula->UpdateFormulaLeaves();
514 // Fix for aod tags: for each tree in the chain, merge the entries
515 cEntries = (fChain->GetTree())->GetEntries();
519 current = fChain->GetTreeNumber();
522 iTotalEvents += tag->GetNEvents();
523 if ((iev == 0) || !aod) localList->Reset();
524 if(fRunFormula->EvalInstance(iTagFiles) == 1) {
525 if(fLHCFormula->EvalInstance(iTagFiles) == 1) {
526 if(fDetectorFormula->EvalInstance(iTagFiles) == 1) {
527 Int_t iEvents = fEventFormula->GetNdata();
528 const TClonesArray *tagList = tag->GetEventTags();
529 iRejectedEvtInFile = 0;
530 iAcceptedEvtInFile = 0;
531 for(Int_t i = 0; i < iEvents; i++) {
532 evTag = (AliEventTag *) tagList->At(i);
533 guid = evTag->GetGUID();
534 turl = evTag->GetTURL();
535 lfn = turl(8,turl.Length());
536 if(fEventFormula->EvalInstance(i) == 1) {
537 if(aod) localList->Enter(iev);
538 else localList->Enter(i);
539 iAcceptedEvtInFile++;
543 iRejectedEvtInFile++;
547 if ((ientry == cEntries-1) || !aod) {
548 collection->WriteBody(iTagFiles+1,guid,lfn,turl,localList,iAcceptedEvtInFile, iRejectedEvtInFile);
549 iAccepted += localList->GetN();
553 iRejectedDet += tag->GetNEvents();
557 iRejectedLHC += tag->GetNEvents();
561 iRejectedRun += tag->GetNEvents();
565 collection->WriteSummary(iTotalEvents, iAccepted, iRejectedRun, iRejectedLHC, iRejectedDet, iRejectedEvt);
566 collection->Export();
572 //___________________________________________________________________________
573 TChain *AliTagAnalysis::GetInputChain(const char* system, const char *wn) {
574 //returns the chain+event list - used in batch sessions
575 // this function will be removed once the new root
576 // improvements are committed
577 TString fsystem = system;
580 TChain *fAnalysisChain = 0;
581 if(fAnalysisType == "ESD") fAnalysisChain = new TChain("esdTree");
582 else if(fAnalysisType == "AOD") fAnalysisChain = new TChain("aodTree");
583 else AliFatal("Only ESD and AOD type is implemented!!!");
586 TEventList *fEventList = new TEventList();
587 AliXMLCollection *collection = AliXMLCollection::Open(wn);
590 while (collection->Next()) {
591 AliInfo(Form("Adding: %s",collection->GetTURL("")));
592 fAnalysisChain->Add(collection->GetTURL(""));
593 TEntryList *list = (TEntryList *)collection->GetEventList("");
594 for(Int_t i = 0; i < list->GetN(); i++) fEventList->Enter(iAccepted+list->GetEntry(i));
596 if(fsystem == "pp") iAccepted += 100;
597 else if(fsystem == "PbPb") iAccepted += 1;
600 fAnalysisChain->SetEventList(fEventList);
602 AliInfo(Form("Number of selected events: %d",fEventList->GetN()));
604 return fAnalysisChain;
607 //___________________________________________________________________________
608 TChain *AliTagAnalysis::GetChainFromCollection(const char* collectionname,
609 const char* treename) {
610 //returns the TChain+TEntryList object- used in batch sessions
611 TString aliceFile = treename;
613 TChain *fAnalysisChain = 0;
614 if(aliceFile == "esdTree") fAnalysisChain = new TChain("esdTree");
615 else if(aliceFile == "aodTree") fAnalysisChain = new TChain("aodTree");
616 else AliFatal("Inconsistent tree name - use esdTree or aodTree!");
619 fGlobalList = new TEntryList();
620 AliXMLCollection *collection = AliXMLCollection::Open(collectionname);
622 // Tag selection summary per file
623 TMap *tagCutSummary = new TMap();
624 tagCutSummary->SetName("TagCutSumm");
627 while (collection->Next()) {
628 AliInfo(Form("Adding: %s",collection->GetTURL("")));
629 fAnalysisChain->Add(collection->GetTURL(""));
630 TEntryList *list = (TEntryList *)collection->GetEventList("");
631 list->SetTreeName(aliceFile.Data());
632 list->SetFileName(collection->GetTURL(""));
633 fGlobalList->Add(list);
634 iAccepted += list->GetN();
635 if (collection->GetCutSumm())
636 tagCutSummary->Add(new TObjString(collection->GetTURL("")), new TObjString(collection->GetCutSumm()));
639 fAnalysisChain->SetEntryList(fGlobalList,"ne");
641 AliInfo(Form("Number of selected events: %d",iAccepted));
643 TList *aUserInfo = fAnalysisChain->GetUserInfo();
644 aUserInfo->Add(tagCutSummary);
653 collection->GetCollectionSummary(&iTotalEvents, &iAccEv, &iRejRun, &iRejLHC, &iRejDet, &iRejEvt);
657 sprintf(nstr, "TotalEvents=%i", iTotalEvents);
658 TObjString *iTotStr = new TObjString(nstr);
659 aUserInfo->Add(iTotStr);
661 sprintf(nstr, "AcceptedEvents=%i", iAccepted);
662 TObjString *iAccStr = new TObjString(nstr);
663 aUserInfo->Add(iAccStr);
665 sprintf(nstr, "RejectedRun=%i", iRejRun);
666 TObjString *iRejRunStr = new TObjString(nstr);
667 aUserInfo->Add(iRejRunStr);
669 sprintf(nstr, "RejectedLHC=%i", iRejLHC);
670 TObjString *iRejLHCStr = new TObjString(nstr);
671 aUserInfo->Add(iRejLHCStr);
673 sprintf(nstr, "RejectedDet=%i", iRejDet);
674 TObjString *iRejDetStr = new TObjString(nstr);
675 aUserInfo->Add(iRejDetStr);
677 sprintf(nstr, "RejectedEvt=%i", iRejEvt);
678 TObjString *iRejEvtStr = new TObjString(nstr);
679 aUserInfo->Add(iRejEvtStr);
681 return fAnalysisChain;