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 //___________________________________________________________________________
82 AliTagAnalysis::AddTagsFile(const char* alienUrl, Bool_t checkFile)
84 /// Add a single tags file to the chain
86 /// If checkFile=kTRUE (default) the file is opened to check
87 /// it can be and that it contains data.
88 /// It's safer but a lot longer...
90 if (!fChain) fChain = new TChain("T");
94 return ( fChain->AddFile(alienUrl,-1) > 0 );
98 return ( fChain->AddFile(alienUrl) > 0 );
103 //___________________________________________________________________________
104 void AliTagAnalysis::ChainLocalTags(const char *dirname) {
105 //Searches the entries of the provided direcory
106 //Chains the tags that are stored locally
107 fTagDirName = dirname;
108 TString fTagFilename;
110 if (! fChain) fChain = new TChain("T");
111 const char * tagPattern = 0x0;
112 if(fAnalysisType == "ESD") tagPattern = "ESD.tag.root";
113 else if(fAnalysisType == "AOD") tagPattern = "AOD.tag.root";
114 else AliFatal("Only ESD and AOD type is implemented!!!");
116 // Open the working directory
117 void * dirp = gSystem->OpenDirectory(fTagDirName);
118 const char * name = 0x0;
119 // Add all files matching *pattern* to the chain
120 while((name = gSystem->GetDirEntry(dirp))) {
121 if (strstr(name,tagPattern)) {
122 fTagFilename = fTagDirName;
124 fTagFilename += name;
126 fChain->Add(fTagFilename);
127 printf("Tag file %s\n", fTagFilename.Data());
131 //AliInfo(Form("Chained tag files: %d ",fChain->GetEntries()));
132 // AliDebug(Form("Chained tag files: %d ",fChain->GetEntries()));
138 //___________________________________________________________________________
139 TChain * AliTagAnalysis::ChainGridTags(TGridResult *res) {
140 //Loops overs the entries of the TGridResult
141 //Chains the tags that are stored in the GRID
143 Int_t nEntries = ftagresult->GetEntries();
145 if (! fChain) fChain = new TChain("T");
147 TString gridname = "alien://";
150 for(Int_t i = 0; i < nEntries; i++) {
151 alienUrl = ftagresult->GetKey(i,"turl");
152 fChain->Add(alienUrl);
158 //___________________________________________________________________________
159 TChain *AliTagAnalysis::QueryTags(AliRunTagCuts *runTagCuts,
160 AliLHCTagCuts *lhcTagCuts,
161 AliDetectorTagCuts *detTagCuts,
162 AliEventTagCuts *evTagCuts) {
163 //Queries the tag chain using the defined
164 //event tag cuts from the AliEventTagCuts object
165 //and returns a TChain along with the associated TEventList
166 AliInfo(Form("Querying the tags........"));
170 if(fAnalysisType == "ESD") aliceFile = "esdTree";
171 else if(fAnalysisType == "AOD") {
172 aliceFile = "aodTree";
175 else AliFatal("Only ESD and AOD type is implemented!!!");
178 TChain *esdChain = new TChain(aliceFile.Data());
180 fGlobalList = new TEntryList();
182 //Defining tag objects
183 AliRunTag *tag = new AliRunTag;
184 AliEventTag *evTag = new AliEventTag;
185 fChain->SetBranchAddress("AliTAG",&tag);
192 TEntryList* localList = new TEntryList();
199 for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
200 fChain->GetEntry(iTagFiles);
201 TTree* tree = fChain->GetTree();
203 // Fix for aod tags: for each tree in the chain, merge the entries
205 cEntries = tree->GetEntries();
210 if(runTagCuts->IsAccepted(tag)) {
211 if(lhcTagCuts->IsAccepted(tag->GetLHCTag())) {
212 if(detTagCuts->IsAccepted(tag->GetDetectorTags())) {
213 if ((iev == 0) || !aod) localList->Reset();
214 Int_t iEvents = tag->GetNEvents();
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 localList->SetTreeName(aliceFile.Data());
222 if(turl!="") localList->SetFileName(turl.Data());
223 else localList->SetFileName(path.Data());
225 if(evTagCuts->IsAccepted(evTag)) {
226 if(aod) localList->Enter(iev);
227 else localList->Enter(i);
231 if ((ientry == cEntries-1) || !aod) {
232 iAccepted += localList->GetN();
233 if(path != "") esdChain->AddFile(path);
234 else if(turl != "") esdChain->AddFile(turl);
235 fGlobalList->Add(localList);
243 AliInfo(Form("Accepted events: %d",iAccepted));
245 esdChain->SetEntryList(fGlobalList,"ne");
252 //___________________________________________________________________________
253 TChain *AliTagAnalysis::QueryTags(const char *fRunCut,
255 const char *fDetectorCut,
256 const char *fEventCut) {
257 //Queries the tag chain using the defined
258 //event tag cuts from the AliEventTagCuts object
259 //and returns a TChain along with the associated TEventList
260 AliInfo(Form("Querying the tags........"));
264 if(fAnalysisType == "ESD") aliceFile = "esdTree";
265 else if(fAnalysisType == "AOD") {
266 aliceFile = "aodTree";
269 else AliFatal("Only ESD and AOD type is implemented!!!");
272 TChain *esdChain = new TChain(aliceFile.Data());
274 fGlobalList = new TEntryList();
276 //Defining tag objects
277 AliRunTag *tag = new AliRunTag;
278 AliEventTag *evTag = new AliEventTag;
279 fChain->SetBranchAddress("AliTAG",&tag);
285 TTreeFormula *fRunFormula = new TTreeFormula("fRun",fRunCut,fChain);
286 TTreeFormula *fLHCFormula = new TTreeFormula("fLHC",fLHCCut,fChain);
287 TTreeFormula *fDetectorFormula = new TTreeFormula("fDetector",fDetectorCut,fChain);
288 TTreeFormula *fEventFormula = new TTreeFormula("fEvent",fEventCut,fChain);
290 TEntryList* localList = new TEntryList();
298 for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
299 fChain->GetEntry(iTagFiles);
300 if (current != fChain->GetTreeNumber()) {
301 fRunFormula->UpdateFormulaLeaves();
302 fLHCFormula->UpdateFormulaLeaves();
303 fDetectorFormula->UpdateFormulaLeaves();
304 fEventFormula->UpdateFormulaLeaves();
305 // Fix for aod tags: for each tree in the chain, merge the entries
306 cEntries = (fChain->GetTree())->GetEntries();
310 current = fChain->GetTreeNumber();
312 if(fRunFormula->EvalInstance(iTagFiles) == 1) {
313 if(fLHCFormula->EvalInstance(iTagFiles) == 1) {
314 if(fDetectorFormula->EvalInstance(iTagFiles) == 1) {
315 if ((iev == 0) || !aod) localList->Reset();
316 Int_t iEvents = fEventFormula->GetNdata();
317 const TClonesArray *tagList = tag->GetEventTags();
318 for(Int_t i = 0; i < iEvents; i++) {
319 evTag = (AliEventTag *) tagList->At(i);
320 guid = evTag->GetGUID();
321 turl = evTag->GetTURL();
322 path = evTag->GetPath();
323 localList->SetTreeName(aliceFile.Data());
324 localList->SetFileName(turl.Data());
325 if(fEventFormula->EvalInstance(i) == 1) {
326 if(aod) localList->Enter(iev);
327 else localList->Enter(i);
332 if ((ientry == cEntries-1) || !aod) {
333 if(path != "") esdChain->AddFile(path);
334 else if(turl != "") esdChain->AddFile(turl);
335 fGlobalList->Add(localList);
336 iAccepted += localList->GetN();
344 AliInfo(Form("Accepted events: %d",iAccepted));
345 esdChain->SetEntryList(fGlobalList,"ne");
352 //___________________________________________________________________________
354 AliTagAnalysis::CreateXMLCollection(const char* name,
355 AliRunTagCuts *runTagCuts,
356 AliLHCTagCuts *lhcTagCuts,
357 AliDetectorTagCuts *detTagCuts,
358 AliEventTagCuts *evTagCuts)
360 /// Queries the tag chain using the defined run, lhc, detector and event tag objects
361 /// and create a XML collection named "name.xml"
362 /// if any of the runTagCuts, lhcTagCuts, detTagCuts or evTagCuts is NULL
363 /// check on that object will be skipped.
365 AliInfo(Form("Creating the collection........"));
369 AliError("fChain is NULL. Cannot make a collection from that !");
374 if(fAnalysisType == "AOD") aod = kTRUE;
377 AliXMLCollection collection;
378 collection.SetCollectionName(name);
379 collection.WriteHeader();
386 TEntryList localList;
392 Int_t iRejectedRun = 0;
393 Int_t iRejectedLHC = 0;
394 Int_t iRejectedDet = 0;
395 Int_t iRejectedEvt = 0;
397 Int_t iTotalEvents = 0;
399 Int_t iAcceptedEvtInFile = 0;
400 Int_t iRejectedEvtInFile = 0;
402 //Defining tag objects
403 AliRunTag* tag = new AliRunTag;
404 fChain->SetBranchAddress("AliTAG",&tag);
406 for(Int_t iTagFiles = 0; iTagFiles < fChain->GetListOfFiles()->GetEntries(); ++iTagFiles)
408 fChain->GetEntry(iTagFiles);
409 TTree* tree = fChain->GetTree();
411 // Fix for aod tags: for each tree in the chain, merge the entries
413 cEntries = tree->GetEntries();
418 iTotalEvents += tag->GetNEvents();
419 if ((iev == 0) || !aod) localList.Reset();
421 if ( !runTagCuts || ( runTagCuts && runTagCuts->IsAccepted(tag) ) )
423 if ( !lhcTagCuts || ( lhcTagCuts && lhcTagCuts->IsAccepted(tag->GetLHCTag())) )
425 if ( !detTagCuts || ( detTagCuts && detTagCuts->IsAccepted(tag->GetDetectorTags())) )
428 TIter next(tag->GetEventTags());
429 AliEventTag* evTag(0x0);
430 iRejectedEvtInFile = 0;
431 iAcceptedEvtInFile = 0;
432 while ( ( evTag = static_cast<AliEventTag*>(next()) ) )
434 guid = evTag->GetGUID();
435 turl = evTag->GetTURL();
436 lfn = turl(8,turl.Length());
437 if( !evTagCuts || ( evTagCuts && evTagCuts->IsAccepted(evTag)) )
439 if (aod) localList.Enter(iev);
440 else localList.Enter(i);
441 iAcceptedEvtInFile++;
443 // AliInfo(Form("Period %5d Orbit# %12d BC %10d (%5d ? in TURL %s) Trigger %s nmus %d",
444 // evTag->GetPeriodNumber(),
445 // evTag->GetOrbitNumber(),
446 // evTag->GetBunchCrossNumber(),
449 // evTag->GetFiredTriggerClasses().Data(),
450 // evTag->GetNumOfMuons()));
455 ++iRejectedEvtInFile;
460 if ((ientry == cEntries-1) || !aod)
462 iAccepted += localList.GetN();
463 collection.WriteBody(iTagFiles+1,guid,lfn,turl,&localList,iAcceptedEvtInFile,iRejectedEvtInFile);
467 iRejectedDet += tag->GetNEvents();
471 iRejectedLHC += tag->GetNEvents();
475 iRejectedRun += tag->GetNEvents();
481 collection.WriteSummary(iTotalEvents, iAccepted, iRejectedRun, iRejectedLHC, iRejectedDet, iRejectedEvt);
487 //___________________________________________________________________________
488 Bool_t AliTagAnalysis::CreateXMLCollection(const char* name,
491 const char *fDetectorCut,
492 const char *fEventCut) {
493 //Queries the tag chain using the defined
494 //event tag cuts from the AliEventTagCuts object
495 //and returns a XML collection
496 AliInfo(Form("Creating the collection........"));
499 if(fAnalysisType == "AOD") aod = kTRUE;
501 AliXMLCollection *collection = new AliXMLCollection();
502 collection->SetCollectionName(name);
503 collection->WriteHeader();
508 TEntryList* localList = new TEntryList();
515 Int_t iRejectedRun = 0;
516 Int_t iRejectedLHC = 0;
517 Int_t iRejectedDet = 0;
518 Int_t iRejectedEvt = 0;
520 Int_t iTotalEvents = 0;
522 Int_t iAcceptedEvtInFile = 0;
523 Int_t iRejectedEvtInFile = 0;
525 //Defining tag objects
526 AliRunTag *tag = new AliRunTag;
527 AliEventTag *evTag = new AliEventTag;
528 fChain->SetBranchAddress("AliTAG",&tag);
530 TTreeFormula *fRunFormula = new TTreeFormula("fRun",fRunCut,fChain);
531 TTreeFormula *fLHCFormula = new TTreeFormula("fLHC",fLHCCut,fChain);
532 TTreeFormula *fDetectorFormula = new TTreeFormula("fDetector",fDetectorCut,fChain);
533 TTreeFormula *fEventFormula = new TTreeFormula("fEvent",fEventCut,fChain);
536 for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
538 fChain->GetEntry(iTagFiles);
539 if (current != fChain->GetTreeNumber()) {
540 fRunFormula->UpdateFormulaLeaves();
541 fLHCFormula->UpdateFormulaLeaves();
542 fDetectorFormula->UpdateFormulaLeaves();
543 fEventFormula->UpdateFormulaLeaves();
544 // Fix for aod tags: for each tree in the chain, merge the entries
545 cEntries = (fChain->GetTree())->GetEntries();
549 current = fChain->GetTreeNumber();
552 iTotalEvents += tag->GetNEvents();
553 if ((iev == 0) || !aod) localList->Reset();
554 if(fRunFormula->EvalInstance(iTagFiles) == 1) {
555 if(fLHCFormula->EvalInstance(iTagFiles) == 1) {
556 if(fDetectorFormula->EvalInstance(iTagFiles) == 1) {
557 Int_t iEvents = fEventFormula->GetNdata();
558 const TClonesArray *tagList = tag->GetEventTags();
559 iRejectedEvtInFile = 0;
560 iAcceptedEvtInFile = 0;
561 for(Int_t i = 0; i < iEvents; i++) {
562 evTag = (AliEventTag *) tagList->At(i);
563 guid = evTag->GetGUID();
564 turl = evTag->GetTURL();
565 lfn = turl(8,turl.Length());
566 if(fEventFormula->EvalInstance(i) == 1) {
567 if(aod) localList->Enter(iev);
568 else localList->Enter(i);
569 iAcceptedEvtInFile++;
573 iRejectedEvtInFile++;
577 if ((ientry == cEntries-1) || !aod) {
578 collection->WriteBody(iTagFiles+1,guid,lfn,turl,localList,iAcceptedEvtInFile, iRejectedEvtInFile);
579 iAccepted += localList->GetN();
583 iRejectedDet += tag->GetNEvents();
587 iRejectedLHC += tag->GetNEvents();
591 iRejectedRun += tag->GetNEvents();
595 collection->WriteSummary(iTotalEvents, iAccepted, iRejectedRun, iRejectedLHC, iRejectedDet, iRejectedEvt);
596 collection->Export();
602 //___________________________________________________________________________
603 TChain *AliTagAnalysis::GetInputChain(const char* system, const char *wn) {
604 //returns the chain+event list - used in batch sessions
605 // this function will be removed once the new root
606 // improvements are committed
607 TString fsystem = system;
610 TChain *fAnalysisChain = 0;
611 if(fAnalysisType == "ESD") fAnalysisChain = new TChain("esdTree");
612 else if(fAnalysisType == "AOD") fAnalysisChain = new TChain("aodTree");
613 else AliFatal("Only ESD and AOD type is implemented!!!");
616 TEventList *fEventList = new TEventList();
617 AliXMLCollection *collection = AliXMLCollection::Open(wn);
620 while (collection->Next()) {
621 AliInfo(Form("Adding: %s",collection->GetTURL("")));
622 fAnalysisChain->Add(collection->GetTURL(""));
623 TEntryList *list = (TEntryList *)collection->GetEventList("");
624 for(Int_t i = 0; i < list->GetN(); i++) fEventList->Enter(iAccepted+list->GetEntry(i));
626 if(fsystem == "pp") iAccepted += 100;
627 else if(fsystem == "PbPb") iAccepted += 1;
630 fAnalysisChain->SetEventList(fEventList);
632 AliInfo(Form("Number of selected events: %d",fEventList->GetN()));
634 return fAnalysisChain;
637 //___________________________________________________________________________
639 AliTagAnalysis::CreateChainFromCollection(const char* collectionname, const char* treename)
641 /// Build a TChain (with its TEntryList object attached) from an XML collection.
642 /// Returned chain must be deleted by the client.
644 TString streename(treename);
645 if ( streename != "esdTree" && streename != "aodTree" )
647 AliErrorClass("Only esdTree and aodTree implemented so far...");
651 TChain* chain = new TChain(streename.Data());
653 // create the event list for the chain. Will be attached to the chain
654 // which thus becomes the owner of it.
655 TEntryList* elist = new TEntryList;
657 AliXMLCollection* collection = AliXMLCollection::Open(collectionname);
659 // Tag selection summary per file
660 TMap* tagCutSummary = new TMap();
661 tagCutSummary->SetName("TagCutSumm");
667 while (collection->Next())
669 AliDebugClass(1,Form("Adding: %s",collection->GetTURL("")));
670 chain->Add(collection->GetTURL(""));
671 TEntryList *list = collection->GetEventList("");
672 list->SetTreeName(streename.Data());
673 list->SetFileName(collection->GetTURL(""));
675 iAccepted += list->GetN();
676 if (collection->GetCutSumm())
678 tagCutSummary->Add(new TObjString(collection->GetTURL("")), new TObjString(collection->GetCutSumm()));
682 chain->SetEntryList(elist,"ne"); // ne => do not expand tree name and/or file names
684 AliDebugClass(1,Form("Number of selected events: %d",iAccepted));
686 TList *aUserInfo = chain->GetUserInfo();
687 aUserInfo->Add(tagCutSummary);
696 collection->GetCollectionSummary(&iTotalEvents, &iAccEv, &iRejRun, &iRejLHC, &iRejDet, &iRejEvt);
700 sprintf(nstr, "TotalEvents=%i", iTotalEvents);
701 TObjString *iTotStr = new TObjString(nstr);
702 aUserInfo->Add(iTotStr);
704 sprintf(nstr, "AcceptedEvents=%i", iAccepted);
705 TObjString *iAccStr = new TObjString(nstr);
706 aUserInfo->Add(iAccStr);
708 sprintf(nstr, "RejectedRun=%i", iRejRun);
709 TObjString *iRejRunStr = new TObjString(nstr);
710 aUserInfo->Add(iRejRunStr);
712 sprintf(nstr, "RejectedLHC=%i", iRejLHC);
713 TObjString *iRejLHCStr = new TObjString(nstr);
714 aUserInfo->Add(iRejLHCStr);
716 sprintf(nstr, "RejectedDet=%i", iRejDet);
717 TObjString *iRejDetStr = new TObjString(nstr);
718 aUserInfo->Add(iRejDetStr);
720 sprintf(nstr, "RejectedEvt=%i", iRejEvt);
721 TObjString *iRejEvtStr = new TObjString(nstr);
722 aUserInfo->Add(iRejEvtStr);