1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
15 //_________________________________________________________________________
16 // Various utilities usefull for analysis
19 //////////////////////////////////////////////////////////////////////////////
21 #include "AliAnalysisDataContainer.h"
22 #include "AliTagAnalysis.h"
23 #include "AliEventTagCuts.h"
24 #include "AliRunTagCuts.h"
25 #include "AliXMLCollection.h"
26 #include "AliAnalysisGoodies.h"
27 #include "AliAnalysisManager.h"
28 #include "AliAnalysisTask.h"
31 #include <Riostream.h>
33 #include <TGridResult.h>
34 #include <TFileMerger.h>
40 #include <TEntryList.h>
42 //______________________________________________________________________________
43 AliAnalysisGoodies::AliAnalysisGoodies() :
46 fESDTreeName("esdTree"),
54 TString token = gSystem->Getenv("GRID_TOKEN") ;
57 TGrid::Connect("alien://");
59 AliInfo("You are not connected to the GRID") ;
62 //______________________________________________________________________________
63 AliAnalysisGoodies::AliAnalysisGoodies(const AliAnalysisGoodies& ag) :
73 fESDTreeName = ag.fESDTreeName ;
74 TString token = gSystem->Getenv("GRID_TOKEN") ;
77 TGrid::Connect("alien://");
79 AliInfo("You are not connected to the GRID") ;
82 //______________________________________________________________________________
83 void AliAnalysisGoodies::Help() const
85 AliInfo("Analysis utilities:\n") ;
86 printf(" *** Alien2Local : copy files ESD files listed in an xml collection from AliEn catalog to local storage and creates a local xml collection \n") ;
87 printf(" usage: Alien2Local(in, out)\n") ;
88 printf(" in: a xml esd collection file name \n") ;
89 printf(" ou: the local directory where to save the esd root files \n") ;
90 printf(" *** Make : makes esd collection from tags \n") ;
91 printf(" usage: Make(tags, esds)\n") ;
92 printf(" tags: is either a tag root file or an xml tag collection \n") ;
93 printf(" esds: is an esd collection \n") ;
94 printf(" *** Merge : merges files listed in a xml collection \n") ;
95 printf(" usage Merge(collection, outputDile)\n") ;
96 printf(" collection: is a xml collection \n") ;
97 printf(" *** Process : process the events with an Analysis Task \n") ;
98 printf(" usage: Process(esdFile, tagCuts) \n") ;
99 printf(" esdFile: can be a root file with the ESD Tree ( ex: esd?AliESDs.root) \n") ;
100 printf(" or a root file with the Tag Tree ( ex: tag?Run100.Event0_100.ESD.tag.root) \n") ;
101 printf(" or a local or alien xml file with the ESD collection ( ex: esd?esdCollection.xml) \n") ;
102 printf(" or a local or alien xml file with the TAG collection ( ex: tag?tagCollection.xml) \n") ;
103 printf(" or a TChain of esd TTrees \n") ;
104 printf(" tagCuts: is the AliEventTagCuts (needed only for tag? cases \n") ;
105 printf(" *** Register: register files already stored in a MSS into the AliEn catalog\n") ;
106 printf(" usage: Register(lfndir, pfndir, pfnFileName) \n") ;
107 printf(" lfndir : AliEn directory ( ex: /alice/data/2006/LHC06c/PHOS_TestBeam/\n") ;
108 printf(" pfndir : MSS directory ( ex: /castor/cern.ch/alice/testbeam/phos/2006 \n") ;
109 printf(" file : text file with a list of the file names to be registered\n ") ;
113 //______________________________________________________________________
114 Bool_t AliAnalysisGoodies::Alien2Local(const TString collectionNameIn, const TString localDir)
116 // copy files ESD files listed in an xml collection from AliEn catalog to local storage and creates a local xml collection
117 // usage: Alien2Local(in, out)
118 // in: a xml esd collection file name
119 // ou: the local directory where to save the esd root files
126 AliXMLCollection * collectionIn = AliXMLCollection::Open(collectionNameIn) ;
127 collectionIn->Reset() ;
129 AliXMLCollection * collectionOu = new AliXMLCollection() ;
130 TString collectionNameOu(collectionIn->GetCollectionName()) ;
131 collectionNameOu.Append("Local") ;
132 collectionOu->SetCollectionName(collectionNameOu) ;
133 collectionOu->WriteHeader() ;
137 const char* ocwd = gSystem->WorkingDirectory();
140 while ( collectionIn->Next() ) {
141 gSystem->ChangeDirectory(localDir) ;
142 TString fileTURL = collectionIn->GetTURL("") ;
144 TString tempo(fileTURL) ;
145 tempo.Remove(tempo.Last('/'), tempo.Length()) ;
146 TString evtsNumber = tempo(tempo.Last('/')+1, tempo.Length())+"/";
147 tempo.Remove(tempo.Last('/'), tempo.Length()) ;
148 TString runNumber = tempo(tempo.Last('/')+1, tempo.Length())+"/" ;
149 TString dir = localDir + runNumber ;
152 sprintf(line, ".! mkdir -p %s", dir.Data()) ;
153 gROOT->ProcessLine(line) ;
154 printf("***************************%s\n", line) ;
155 TEntryList * list = collectionIn->GetEventList("") ;
157 list = new TEntryList() ;
159 TString filename = tempo(tempo.Last('/')+1, tempo.Length()) ;
161 AliInfo(Form("Copying %s to %s\n", fileTURL.Data(), dir.Data())) ;
162 collectionOu->WriteBody(counter, collectionIn->GetGUID(""), collectionIn->GetLFN(""), dir, list) ;
164 merger.Cp(fileTURL, dir) ;
166 collectionOu->Export() ;
167 gSystem->ChangeDirectory(ocwd) ;
178 //______________________________________________________________________
179 Bool_t AliAnalysisGoodies::Make(AliRunTagCuts *runCuts, AliLHCTagCuts *lhcCuts, AliDetectorTagCuts *detCuts, AliEventTagCuts *evtCuts, const char * in, const char * out) const
181 // makes esd collection from tags
182 // usage Make(tags, esds)
183 // tags: is either a tag root file or an xml tag collection
184 // esds: is an esd collection
188 if ( !evtCuts && !runCuts ) {
189 AliError("No Tag cuts provided") ;
194 if ( file.Contains(".root") )
195 rv = MakeEsdCollectionFromTagFile(runCuts, lhcCuts, detCuts, evtCuts, file.Data(), out) ;
196 else if ( file.Contains(".xml") )
197 rv = MakeEsdCollectionFromTagCollection(runCuts, lhcCuts, detCuts, evtCuts, file.Data(), out) ;
199 AliError(Form("%s is not a valid file format", in)) ;
206 //______________________________________________________________________
207 Bool_t AliAnalysisGoodies::MakeEsdCollectionFromTagFile(AliRunTagCuts *runCuts, AliLHCTagCuts *lhcCuts, AliDetectorTagCuts *detCuts, AliEventTagCuts *evtCuts, const char * in, const char * out) const
209 // Makes an esd collection from a root tag file
211 // Open the file collection
212 printf("*** Create Collection ***\n");
213 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
214 printf("*** file = |%s| \n",in);
216 AliTagAnalysis * tagAna = new AliTagAnalysis();
217 rv = tagAna->AddTagsFile(in);
221 tagAna->CreateXMLCollection(out, runCuts, lhcCuts, detCuts, evtCuts) ;
227 //______________________________________________________________________
228 Bool_t AliAnalysisGoodies::MakeEsdCollectionFromTagCollection(AliRunTagCuts *runCuts, AliLHCTagCuts *lhcCuts, AliDetectorTagCuts *detCuts, AliEventTagCuts *evtCuts, const char * in, const char * out) const
230 // Makes an esd collection from a xml tag collection
232 // Open the file collection
233 printf("*** Create Collection ***\n");
234 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
235 printf("*** Coll = |%s| \n",in);
239 TGridCollection * collection = (TGridCollection*)gROOT->ProcessLine(Form("TAlienCollection::Open(%s)",in));
240 TGridResult* result = collection->GetGridResult("");
241 AliTagAnalysis * tagAna = new AliTagAnalysis();
242 tagAna->ChainGridTags(result);
244 tagAna->CreateXMLCollection(out, runCuts, lhcCuts, detCuts, evtCuts) ;
252 //______________________________________________________________________
253 Bool_t AliAnalysisGoodies::MakeEsdCollectionFromTagCollection(const char * runCuts, const char *lhcCuts, const char *detCuts, const char * evtCuts, const char * in, const char * out) const
255 // Makes an esd collection from a xml tag collection
259 // Open the file collection
260 printf("*** Create Collection ***\n");
261 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
262 printf("*** Coll = |%s| \n",in);
266 TGridCollection * collection = (TGridCollection*)gROOT->ProcessLine(Form("TAlienCollection::Open(%s)",in));
267 TGridResult* result = collection->GetGridResult("");
268 AliTagAnalysis * tagAna = new AliTagAnalysis();
269 tagAna->ChainGridTags(result);
271 tagAna->CreateXMLCollection(out, runCuts, lhcCuts, detCuts, evtCuts) ;
279 //______________________________________________________________________
280 Bool_t AliAnalysisGoodies::Merge(const char * collectionFile, const char * subFile, const char * outFile)
282 // merges files listed in a xml collection
283 // usage Merge(collection, outputFile))
284 // collection: is a xml collection
288 if ( strstr(collectionFile, ".xml") == 0 ) {
289 AliError("Input collection file must be an \".xml\" file\n") ;
295 // Open the file collection
296 printf("*** Create Collection ***\n");
297 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
298 printf("*** Coll = |%s| \n",collectionFile);
302 TGridCollection * collection = (TGridCollection*)gROOT->ProcessLine(Form("TAlienCollection::Open(%s)",collectionFile));
303 TGridResult* result = collection->GetGridResult("");
309 TString tempo(collectionFile) ;
311 tempo.ReplaceAll(".xml", subFile) ;
313 tempo.ReplaceAll(".xml", "_Merged.root") ;
314 outFile = tempo.Data() ;
316 merger.OutputFile(outFile) ;
318 while ( (turl = result->GetKey(index, "turl")) ) {
321 sprintf(file, "%s#%s", turl, subFile) ;
323 sprintf(file, "%s", turl) ;
325 printf("%s\n", file) ;
326 merger.AddFile(file) ;
333 AliInfo(Form("Files merged into %s\n", outFile)) ;
344 //______________________________________________________________________
345 Bool_t AliAnalysisGoodies::Process(TChain * chain)
347 // process events starting from a chain of esd Trees
351 rv = ProcessChain(chain) ;
359 //______________________________________________________________________
360 Bool_t AliAnalysisGoodies::Process(const char * inFile)
362 // process the events with an Analysis Task
363 // usage Process(esdFile)
364 // esdFile: is of the form opt?file_lfn
366 AliRunTagCuts * runCuts = 0x0 ;
367 AliLHCTagCuts * lhcCuts = 0x0 ;
368 AliDetectorTagCuts * detCuts = 0x0 ;
369 AliEventTagCuts * evtCuts = 0x0 ;
371 rv = Process(inFile, runCuts, lhcCuts, detCuts, evtCuts) ;
376 //______________________________________________________________________
377 Bool_t AliAnalysisGoodies::Process(const char * inFile, AliRunTagCuts *runCuts, AliLHCTagCuts *lhcCuts, AliDetectorTagCuts *detCuts, AliEventTagCuts * evtCuts )
379 // process the events with an Analysis Task
380 // usage Process(esdFile, runtagCuts, evtTagCuts)
381 // esdFile: is of the form opt?file_lfn
387 TString file(inFile) ;
388 if ( file.Contains("esd?") && file.Contains(".root") ) {
389 file.ReplaceAll("esd?", "") ;
390 rv = ProcessEsdFile(file.Data()) ;
392 } else if ( file.Contains("esd?") && file.Contains(".xml") ) {
393 file.ReplaceAll("esd?", "") ;
394 rv = ProcessEsdXmlCollection(file.Data()) ;
396 } else if (file.Contains("tag?") && file.Contains(".root") ) {
397 file.ReplaceAll("tag?", "") ;
398 rv = ProcessTagFile(file.Data(), runCuts, lhcCuts, detCuts, evtCuts) ;
400 } else if (file.Contains("tag?") && file.Contains(".xml") ) {
401 file.ReplaceAll("tag?", "") ;
402 rv = ProcessTagXmlCollection(file.Data(), runCuts, lhcCuts, detCuts, evtCuts) ;
405 AliError(Form("%s is not a valid file format", inFile)) ;
415 //______________________________________________________________________
416 Bool_t AliAnalysisGoodies::Process(const char * inFile, const char * runCuts, const char * lhcCuts, const char * detCuts, const char * evtCuts)
418 // process the events with an Analysis Task
419 // usage Process(esdFile, runtagCuts, evtTagCuts)
420 // esdFile: is of the form opt?file_lfn
426 TString file(inFile) ;
427 if ( file.Contains("esd?") && file.Contains(".root") ) {
428 file.ReplaceAll("esd?", "") ;
429 rv = ProcessEsdFile(file.Data()) ;
431 } else if ( file.Contains("esd?") && file.Contains(".xml") ) {
432 file.ReplaceAll("esd?", "") ;
433 rv = ProcessEsdXmlCollection(file.Data()) ;
435 } else if (file.Contains("tag?") && file.Contains(".root") ) {
436 file.ReplaceAll("tag?", "") ;
437 rv = ProcessTagFile(file.Data(), runCuts, lhcCuts, detCuts, evtCuts) ;
439 } else if (file.Contains("tag?") && file.Contains(".xml") ) {
440 file.ReplaceAll("tag?", "") ;
441 rv = ProcessTagXmlCollection(file.Data(), runCuts, lhcCuts, detCuts, evtCuts) ;
444 AliError(Form("%s is not a valid file format", inFile)) ;
454 //______________________________________________________________________
455 Bool_t AliAnalysisGoodies::ProcessChain(TChain * chain) const
462 AliError("No tasks defined") ;
466 // Make the analysis manager
467 AliAnalysisManager * mgr = new AliAnalysisManager("Goodies Manager", "Analysis manager created by AliAnalysisGoodies") ;
470 // The top input must be common to all top tasks
471 TClass * classIn = fTaskInType[0] ;
472 AliAnalysisDataContainer * taskInput = mgr->CreateContainer("InputContainer", classIn, AliAnalysisManager::kInputContainer) ;
474 for (index = 0; index < fnumberOfTasks; index++) {
475 AliAnalysisTask * task = fTaskList[index] ;
478 // Create containers for input/output
479 TClass * classOu = fTaskOuType[index] ;
480 AliAnalysisDataContainer * taskOutput = mgr->CreateContainer(Form("OutputContainer%d",index), classOu, AliAnalysisManager::kOutputContainer,
481 Form("%s.root",task->GetName())) ;
482 mgr->ConnectInput (task, 0, taskInput);
483 mgr->ConnectOutput(task, 0, taskOutput);
488 if (mgr->InitAnalysis()) {
490 mgr->StartAnalysis("local",chain);
497 //______________________________________________________________________
498 Bool_t AliAnalysisGoodies::ProcessEsdFile(const char * esdFile) const
500 // process the events in a single ESD file with an Analysis Task
501 // usage ProcessLocalEsdFile(esdFile)
502 // esdFile: is the root file (local or in alien) with the ESD Tree ( ex: AliESDs.root)
506 printf("*** Process ***\n");
507 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
508 printf("*** Coll = |%s| \n",esdFile);
510 // Makes the ESD chain
511 printf("*** Getting the Chain ***\n");
512 TChain* analysisChain = new TChain(fESDTreeName) ;
513 analysisChain->AddFile(esdFile);
515 // Process the events
516 rv = ProcessChain(analysisChain) ;
521 //______________________________________________________________________
522 Bool_t AliAnalysisGoodies::ProcessTagFile(const char * tagFile, AliRunTagCuts *runCuts, AliLHCTagCuts *lhcCuts, AliDetectorTagCuts *detCuts, AliEventTagCuts *evtCuts) const
524 // process the events in a single Tag file with an Analysis Task
525 // usage ProcessLocalEsdFile(tagFile)
526 // tagFile: is the root file (local or in alien) with the Tag Tree (ex: Run102.Event0_100.ESD.tag.root)
530 if ( !evtCuts && !runCuts ) {
531 AliError("No Tag cuts provided") ;
535 printf("*** Process ***\n");
536 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
537 printf("*** Coll = |%s| \n",tagFile);
539 AliTagAnalysis * tagAna = new AliTagAnalysis();
540 rv = tagAna->AddTagsFile(tagFile);
544 // Query the tag file and make the analysis chain
545 TChain * analysisChain = new TChain(fESDTreeName) ;
546 analysisChain = tagAna->QueryTags(runCuts, lhcCuts, detCuts, evtCuts);
548 // Process the events
549 rv = ProcessChain(analysisChain) ;
554 //______________________________________________________________________
555 Bool_t AliAnalysisGoodies::ProcessTagFile(const char * tagFile, const char * runCuts, const char * lhcCuts, const char * detCuts, const char * evtCuts) const
557 // process the events in a single Tag file with an Analysis Task
558 // usage ProcessLocalEsdFile(tagFile)
559 // tagFile: is the root file (local or in alien) with the Tag Tree (ex: Run102.Event0_100.ESD.tag.root)
564 if ( !evtCuts && !runCuts ) {
565 AliError("No Tag cuts provided") ;
569 printf("*** Process ***\n");
570 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
571 printf("*** Coll = |%s| \n",tagFile);
573 AliTagAnalysis * tagAna = new AliTagAnalysis();
574 rv = tagAna->AddTagsFile(tagFile);
578 // Query the tag file and make the analysis chain
579 TChain * analysisChain = new TChain(fESDTreeName) ;
580 analysisChain = tagAna->QueryTags(runCuts, lhcCuts, detCuts, evtCuts);
582 // Process the events
583 rv = ProcessChain(analysisChain) ;
588 //______________________________________________________________________
589 Bool_t AliAnalysisGoodies::ProcessEsdXmlCollection(const char * xmlFile) const
591 // process the events in a xml ESD collection with an Analysis Task
592 // usage ProcessLocalEsdFile(xmlFile)
593 // xmlFile: is the local xml file with the ESD collection ( ex: esdCollection.xml)
597 printf("*** Process ***\n");
598 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
599 printf("*** Coll = |%s| \n",xmlFile);
603 TGridCollection * collection = (TGridCollection*)gROOT->ProcessLine(Form("TAlienCollection::Open(%s)",xmlFile));
605 AliError(Form("%s not found", xmlFile)) ;
609 TGridResult* result = collection->GetGridResult("");
610 TList* analysisfilelist = result->GetFileInfoList();
612 // Makes the ESD chain
613 printf("*** Getting the Chain ***\n");
614 TChain* analysisChain = new TChain(fESDTreeName);
615 analysisChain->AddFileInfoList(analysisfilelist);
617 // Process the events
618 rv = ProcessChain(analysisChain) ;
626 //______________________________________________________________________
627 Bool_t AliAnalysisGoodies::ProcessTagXmlCollection(const char * xmlFile, AliRunTagCuts *runCuts, AliLHCTagCuts *lhcCuts, AliDetectorTagCuts *detCuts, AliEventTagCuts * evtCuts) const
629 // process the events in a xml ESD collection with an Analysis Task
630 // usage ProcessLocalEsdFile(xmlFile)
631 // xmlFile: is the local xml file with the tag collection ( ex: tagCollection.xml)
635 if ( !evtCuts && !runCuts ) {
636 AliError("No Tag cuts provided") ;
640 printf("*** Process ***\n");
641 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
642 printf("*** Coll = |%s| \n",xmlFile);
644 // check if file is local or alien
645 if ( gSystem->AccessPathName(xmlFile) )
646 TGrid::Connect("alien://");
650 TGridCollection * collection = (TGridCollection*)gROOT->ProcessLine(Form("TAlienCollection::Open(%s)",xmlFile));
652 AliError(Form("%s not found", xmlFile)) ;
656 TGridResult* result = collection->GetGridResult("");
657 AliTagAnalysis * tagAna = new AliTagAnalysis();
658 tagAna->ChainGridTags(result);
660 // Query the tag file and make the analysis chain
661 TChain * analysisChain = new TChain(fESDTreeName) ;
662 analysisChain = tagAna->QueryTags(runCuts, lhcCuts, detCuts, evtCuts);
664 // Process the events
665 rv = ProcessChain(analysisChain) ;
673 //______________________________________________________________________
674 Bool_t AliAnalysisGoodies::ProcessTagXmlCollection(const char * xmlFile, const char * runCuts, const char * lhcCuts, const char * detCuts, const char * evtCuts) const
676 // process the events in a xml ESD collection with an Analysis Task
677 // usage ProcessLocalEsdFile(xmlFile)
678 // xmlFile: is the local xml file with the tag collection ( ex: tagCollection.xml)
682 if ( !evtCuts && !runCuts ) {
683 AliError("No Tag cuts provided") ;
687 printf("*** Process ***\n");
688 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
689 printf("*** Coll = |%s| \n",xmlFile);
693 // check if file is local or alien
694 if ( gSystem->AccessPathName(xmlFile) )
695 TGrid::Connect("alien://");
697 TGridCollection * collection = (TGridCollection*)gROOT->ProcessLine(Form("TAlienCollection::Open(%s)",xmlFile));
699 AliError(Form("%s not found", xmlFile)) ;
703 TGridResult* result = collection->GetGridResult("");
704 AliTagAnalysis * tagAna = new AliTagAnalysis();
705 tagAna->ChainGridTags(result);
707 // Query the tag file and make the analysis chain
708 TChain * analysisChain = new TChain(fESDTreeName) ;
709 analysisChain = tagAna->QueryTags(runCuts, lhcCuts, detCuts, evtCuts);
711 // Process the events
712 rv = ProcessChain(analysisChain) ;
720 //______________________________________________________________________
721 Bool_t AliAnalysisGoodies::Register( const char * lfndir, const char * pfndir, const char * file)
723 // register files already stored in a MSS into the AliEn catalog
724 // usage: Register(lfndir, pfndir, pfnFileName)
725 // lfndir : AliEn directory ( ex: /alice/data/2006/LHC06c/PHOS_TestBeam/ )
726 // pfndir : MSS directory ( ex: /castor/cern.ch/alice/testbeam/phos/2006 )
727 // file : text file with a list of the file names to be registered
735 AliError(Form("Cannot open file %s\n", file)) ;
739 TGrid::Connect("alien://");
741 char fileName[1024] ;
749 sprintf(lfn, "%s/%s", lfndir, fileName) ;
753 sprintf(pfn, "castor://Alice::CERN::Castor2/%s/%s", pfndir, fileName) ;
755 printf("Register %s as %s\n", pfn, lfn) ;
757 gGrid->Register(lfn, pfn) ;
766 //______________________________________________________________________
767 void AliAnalysisGoodies::SetTasks(Int_t nb, AliAnalysisTask ** taskList, TClass ** inputType, TClass ** outputType)
769 // define a task with its output and input type
773 fTaskList = taskList ;
774 fTaskInType = inputType ;
775 fTaskOuType = outputType ;