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"
29 //#include "AliPhotonAnalysisTask.h"
32 #include <Riostream.h>
33 #include <TAlienCollection.h>
35 #include <TFileMerger.h>
41 //______________________________________________________________________________
42 AliAnalysisGoodies::AliAnalysisGoodies() :
43 fESDTreeName("esdTree"),
51 TString token = gSystem->Getenv("GRID_TOKEN") ;
54 TGrid::Connect("alien://");
56 AliInfo("You are not connected to the GRID") ;
59 //______________________________________________________________________________
60 void AliAnalysisGoodies::Help() const
62 AliInfo("Analysis utilities:\n") ;
63 printf(" *** Copy : copy files ESD files listed in an xml collection from AliEn catalog to local storage and creates a local xml collection \n") ;
64 printf(" usage: Copy(in, out)\n") ;
65 printf(" in: a xml esd collection file name \n") ;
66 printf(" ou: the local directory where to save the esd root files \n") ;
67 printf(" *** Make : makes esd collection from tags \n") ;
68 printf(" usage: Make(tags, esds)\n") ;
69 printf(" tags: is either a tag root file or an xml tag collection \n") ;
70 printf(" esds: is an esd collection \n") ;
71 printf(" *** Merge : merges files listed in a xml collection \n") ;
72 printf(" usage Merge(collection, outputDile)\n") ;
73 printf(" collection: is a xml collection \n") ;
74 printf(" *** Process : process the events with an Analysis Task \n") ;
75 printf(" usage: Process(esdFile, tagCuts) \n") ;
76 printf(" esdFile: can be a root file with the ESD Tree ( ex: esd?AliESDs.root) \n") ;
77 printf(" or a root file with the Tag Tree ( ex: tag?Run100.Event0_100.ESD.tag.root) \n") ;
78 printf(" or a local or alien xml file with the ESD collection ( ex: esd?esdCollection.xml) \n") ;
79 printf(" or a local or alien xml file with the TAG collection ( ex: tag?tagCollection.xml) \n") ;
80 printf(" or a TChain of esd TTrees \n") ;
81 printf(" tagCuts: is the AliEventTagCuts (needed only for tag? cases \n") ;
82 printf(" *** Register: register files already stored in a MSS into the AliEn catalog\n") ;
83 printf(" usage: Register(lfndir, pfndir, pfnFileName) \n") ;
84 printf(" lfndir : AliEn directory ( ex: /alice/data/2006/LHC06c/PHOS_TestBeam/\n") ;
85 printf(" pfndir : MSS directory ( ex: /castor/cern.ch/alice/testbeam/phos/2006 \n") ;
86 printf(" file : text file with a list of the file names to be registered\n ") ;
90 //______________________________________________________________________
91 const Bool_t AliAnalysisGoodies::Alien2Local(const TString collectionNameIn, const TString localDir)
93 // copy files ESD files listed in an xml collection from AliEn catalog to local storage and creates a local xml collection
94 // usage: Alien2Local(in, out)
95 // in: a xml esd collection file name
96 // ou: the local directory where to save the esd root files
99 const char * kFileName = "AliESDs.root" ;
103 AliXMLCollection collectionIn(collectionNameIn) ;
105 AliXMLCollection * collectionOu = new AliXMLCollection() ;
106 TString collectionNameOu(collectionIn.GetCollectionName()) ;
107 collectionNameOu.Append("Local") ;
108 collectionOu->SetCollectionName(collectionNameOu) ;
109 collectionOu->WriteHeader() ;
113 const char* ocwd = gSystem->WorkingDirectory();
116 while ( collectionIn.Next() ) {
117 gSystem->ChangeDirectory(localDir) ;
118 TString fileTURL = collectionIn.GetTURL(kFileName) ;
120 TString tempo(fileTURL) ;
121 tempo.Remove(tempo.Last('/'), tempo.Length()) ;
122 TString evtsNumber = tempo(tempo.Last('/')+1, tempo.Length())+"/";
123 tempo.Remove(tempo.Last('/'), tempo.Length()) ;
124 TString runNumber = tempo(tempo.Last('/')+1, tempo.Length())+"/" ;
125 TString dir = localDir + runNumber ;
126 gSystem->MakeDirectory(dir) ;
127 gSystem->ChangeDirectory(dir) ;
128 dir += evtsNumber + "/";
129 gSystem->MakeDirectory(dir) ;
130 gSystem->ChangeDirectory(dir) ;
131 dir += collectionIn.GetCollectionName() ;
132 TEntryList * list = collectionIn.GetEventList(kFileName) ;
134 collectionOu->WriteBody(counter, collectionIn.GetGUID(kFileName), collectionIn.GetLFN(kFileName), collectionIn.GetTURL(kFileName), list) ;
136 printf("Copying %s to %s\n", fileTURL.Data(), dir.Data()) ;
137 merger.Cp(fileTURL, dir) ;
139 collectionOu->Export() ;
140 gSystem->ChangeDirectory(ocwd) ;
148 //______________________________________________________________________
149 const Bool_t AliAnalysisGoodies::Make(AliRunTagCuts *runCuts, AliEventTagCuts *evtCuts, const char * in, const char * out) const
151 // makes esd collection from tags
152 // usage Make(tags, esds)
153 // tags: is either a tag root file or an xml tag collection
154 // esds: is an esd collection
158 if ( !evtCuts && !runCuts ) {
159 AliError("No Tag cuts provided") ;
164 if ( file.Contains(".root") )
165 rv = MakeEsdCollectionFromTagFile(runCuts, evtCuts, file.Data(), out) ;
166 else if ( file.Contains(".xml") )
167 rv = MakeEsdCollectionFromTagCollection(runCuts, evtCuts, file.Data(), out) ;
169 AliError(Form("%s is not a valid file format", in)) ;
176 //______________________________________________________________________
177 const Bool_t AliAnalysisGoodies::MakeEsdCollectionFromTagFile(AliRunTagCuts *runCuts, AliEventTagCuts *evtCuts, const char * in, const char * out) const
179 // Makes an esd collection from a root tag file
181 // Open the file collection
182 printf("*** Create Collection ***\n");
183 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
184 printf("*** file = |%s| \n",in);
186 AliTagAnalysis * tagAna = new AliTagAnalysis();
187 rv = tagAna->AddTagsFile(in);
191 tagAna->CreateXMLCollection(out, runCuts, evtCuts) ;
197 //______________________________________________________________________
198 const Bool_t AliAnalysisGoodies::MakeEsdCollectionFromTagCollection(AliRunTagCuts * runCuts, AliEventTagCuts * evtCuts, const char * in, const char * out) const
200 // Makes an esd collection from a xml tag collection
202 // Open the file collection
203 printf("*** Create Collection ***\n");
204 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
205 printf("*** Coll = |%s| \n",in);
207 TAlienCollection * collection = TAlienCollection::Open(in);
208 TGridResult* result = collection->GetGridResult("");
209 AliTagAnalysis * tagAna = new AliTagAnalysis();
210 tagAna->ChainGridTags(result);
212 tagAna->CreateXMLCollection(out, runCuts, evtCuts) ;
217 //______________________________________________________________________
218 // const Bool_t AliAnalysisGoodies::MakeEsdCollectionFromTagCollection(const char * runCuts, const char * evtCuts, const char * in, const char * out) const
220 // // Makes an esd collection from a xml tag collection
221 // Bool_t rv = kTRUE ;
223 // // Open the file collection
224 // printf("*** Create Collection ***\n");
225 // printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
226 // printf("*** Coll = |%s| \n",in);
228 // TAlienCollection * collection = TAlienCollection::Open(in);
229 // TGridResult* result = collection->GetGridResult("");
230 // AliTagAnalysis * tagAna = new AliTagAnalysis();
231 // tagAna->ChainGridTags(result);
233 // tagAna->CreateXMLCollection(out, runCuts, evtCuts) ;
238 //______________________________________________________________________
239 const Bool_t AliAnalysisGoodies::Merge(const char * collectionFile, const char * subFile, const char * outFile)
241 // merges files listed in a xml collection
242 // usage Merge(collection, outputFile))
243 // collection: is a xml collection
247 if ( strstr(collectionFile, ".xml") == 0 ) {
248 AliError("Input collection file must be an \".xml\" file\n") ;
254 // Open the file collection
255 printf("*** Create Collection ***\n");
256 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
257 printf("*** Coll = |%s| \n",collectionFile);
259 TAlienCollection * collection = TAlienCollection::Open(collectionFile);
260 TGridResult* result = collection->GetGridResult("");
266 TString tempo(collectionFile) ;
268 tempo.ReplaceAll(".xml", subFile) ;
270 tempo.ReplaceAll(".xml", "_Merged.root") ;
271 outFile = tempo.Data() ;
273 merger.OutputFile(outFile) ;
275 while ( (turl = result->GetKey(index, "turl")) ) {
278 sprintf(file, "%s#%s", turl, subFile) ;
280 sprintf(file, "%s", turl) ;
282 printf("%s\n", file) ;
283 merger.AddFile(file) ;
290 AliInfo(Form("Files merged into %s\n", outFile)) ;
298 //______________________________________________________________________
299 const Bool_t AliAnalysisGoodies::Process(TChain * chain)
301 // process events starting from a chain of esd Trees
306 rv = ProcessChain(chain) ;
314 //______________________________________________________________________
315 const Bool_t AliAnalysisGoodies::Process(const char * inFile)
317 // process the events with an Analysis Task
318 // usage Process(esdFile)
319 // esdFile: is of the form opt?file_lfn
321 AliRunTagCuts * runCuts = 0x0 ;
322 AliEventTagCuts * evtCuts = 0x0 ;
324 rv = Process(inFile, runCuts, evtCuts) ;
329 //______________________________________________________________________
330 const Bool_t AliAnalysisGoodies::Process(const char * inFile, AliRunTagCuts *runCuts, AliEventTagCuts * evtCuts )
332 // process the events with an Analysis Task
333 // usage Process(esdFile, runtagCuts, evtTagCuts)
334 // esdFile: is of the form opt?file_lfn
340 TString file(inFile) ;
341 if ( file.Contains("esd?") && file.Contains(".root") ) {
342 file.ReplaceAll("esd?", "") ;
343 rv = ProcessEsdFile(file.Data()) ;
345 } else if ( file.Contains("esd?") && file.Contains(".xml") ) {
346 file.ReplaceAll("esd?", "") ;
347 rv = ProcessEsdXmlCollection(file.Data()) ;
349 } else if (file.Contains("tag?") && file.Contains(".root") ) {
350 file.ReplaceAll("tag?", "") ;
351 rv = ProcessTagFile(file.Data(), runCuts, evtCuts) ;
353 } else if (file.Contains("tag?") && file.Contains(".xml") ) {
354 file.ReplaceAll("tag?", "") ;
355 rv = ProcessTagXmlCollection(file.Data(), runCuts, evtCuts) ;
358 AliError(Form("%s is not a valid file format", inFile)) ;
368 //______________________________________________________________________
369 const Bool_t AliAnalysisGoodies::Process(const char * inFile, const char * runCuts, const char * evtCuts)
371 // process the events with an Analysis Task
372 // usage Process(esdFile, runtagCuts, evtTagCuts)
373 // esdFile: is of the form opt?file_lfn
379 TString file(inFile) ;
380 if ( file.Contains("esd?") && file.Contains(".root") ) {
381 file.ReplaceAll("esd?", "") ;
382 rv = ProcessEsdFile(file.Data()) ;
384 } else if ( file.Contains("esd?") && file.Contains(".xml") ) {
385 file.ReplaceAll("esd?", "") ;
386 rv = ProcessEsdXmlCollection(file.Data()) ;
388 } else if (file.Contains("tag?") && file.Contains(".root") ) {
389 file.ReplaceAll("tag?", "") ;
390 rv = ProcessTagFile(file.Data(), runCuts, evtCuts) ;
392 } else if (file.Contains("tag?") && file.Contains(".xml") ) {
393 file.ReplaceAll("tag?", "") ;
394 rv = ProcessTagXmlCollection(file.Data(), runCuts, evtCuts) ;
397 AliError(Form("%s is not a valid file format", inFile)) ;
407 //______________________________________________________________________
408 const Bool_t AliAnalysisGoodies::ProcessChain(TChain * chain) const
415 AliError("No tasks defined") ;
419 // Make the analysis manager
420 AliAnalysisManager * mgr = new AliAnalysisManager() ;
423 // The top input must be common to all top tasks
424 TClass * classIn = fTaskInType[0] ;
425 AliAnalysisDataContainer * taskInput = mgr->CreateContainer("Input Container", classIn, AliAnalysisManager::kInputContainer) ;
427 for (index = 0; index < fnumberOfTasks; index++) {
428 AliAnalysisTask * task = fTaskList[index] ;
431 // Create containers for input/output
432 TClass * classOu = fTaskOuType[index] ;
433 AliAnalysisDataContainer * taskOutput = mgr->CreateContainer("Output Container", classOu, AliAnalysisManager::kOutputContainer) ;
434 mgr->ConnectInput (task, 0, taskInput);
435 mgr->ConnectOutput(task, 0, taskOutput);
439 taskInput->SetData(chain);
441 if (mgr->InitAnalysis()) {
450 //______________________________________________________________________
451 const Bool_t AliAnalysisGoodies::ProcessEsdFile(const char * esdFile) const
453 // process the events in a single ESD file with an Analysis Task
454 // usage ProcessLocalEsdFile(esdFile)
455 // esdFile: is the root file (local or in alien) with the ESD Tree ( ex: AliESDs.root)
459 printf("*** Process ***\n");
460 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
461 printf("*** Coll = |%s| \n",esdFile);
463 // Makes the ESD chain
464 printf("*** Getting the Chain ***\n");
465 TChain* analysisChain = new TChain(fESDTreeName) ;
466 analysisChain->AddFile(esdFile);
468 // Process the events
469 rv = ProcessChain(analysisChain) ;
474 //______________________________________________________________________
475 const Bool_t AliAnalysisGoodies::ProcessTagFile(const char * tagFile, AliRunTagCuts *runCuts, AliEventTagCuts *evtCuts) const
477 // process the events in a single Tag file with an Analysis Task
478 // usage ProcessLocalEsdFile(tagFile)
479 // tagFile: is the root file (local or in alien) with the Tag Tree (ex: Run102.Event0_100.ESD.tag.root)
483 if ( !evtCuts && !runCuts ) {
484 AliError("No Tag cuts provided") ;
488 printf("*** Process ***\n");
489 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
490 printf("*** Coll = |%s| \n",tagFile);
492 AliTagAnalysis * tagAna = new AliTagAnalysis();
493 rv = tagAna->AddTagsFile(tagFile);
497 // Query the tag file and make the analysis chain
498 TChain * analysisChain = new TChain(fESDTreeName) ;
499 analysisChain = tagAna->QueryTags(runCuts, evtCuts);
501 // Process the events
502 rv = ProcessChain(analysisChain) ;
507 //______________________________________________________________________
508 // const Bool_t AliAnalysisGoodies::ProcessTagFile(const char * tagFile, const char * runCuts, const char * evtCuts) const
510 // // process the events in a single Tag file with an Analysis Task
511 // // usage ProcessLocalEsdFile(tagFile)
512 // // tagFile: is the root file (local or in alien) with the Tag Tree (ex: Run102.Event0_100.ESD.tag.root)
514 // Bool_t rv = kTRUE ;
517 // if ( !evtCuts && !runCuts ) {
518 // AliError("No Tag cuts provided") ;
522 // printf("*** Process ***\n");
523 // printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
524 // printf("*** Coll = |%s| \n",tagFile);
526 // AliTagAnalysis * tagAna = new AliTagAnalysis();
527 // rv = tagAna->AddTagsFile(tagFile);
531 // // Query the tag file and make the analysis chain
532 // TChain * analysisChain = new TChain(fESDTreeName) ;
533 // analysisChain = tagAna->QueryTags(runCuts, evtCuts);
535 // // Process the events
536 // rv = ProcessChain(analysisChain) ;
541 //______________________________________________________________________
542 const Bool_t AliAnalysisGoodies::ProcessEsdXmlCollection(const char * xmlFile) const
544 // process the events in a xml ESD collection with an Analysis Task
545 // usage ProcessLocalEsdFile(xmlFile)
546 // xmlFile: is the local xml file with the ESD collection ( ex: esdCollection.xml)
550 printf("*** Process ***\n");
551 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
552 printf("*** Coll = |%s| \n",xmlFile);
554 TAlienCollection * collection = TAlienCollection::Open(xmlFile) ;
556 AliError(Form("%s not found", xmlFile)) ;
560 TGridResult* result = collection->GetGridResult("");
561 TList* analysisfilelist = result->GetFileInfoList();
563 // Makes the ESD chain
564 printf("*** Getting the Chain ***\n");
565 TChain* analysisChain = new TChain(fESDTreeName);
566 analysisChain->AddFileInfoList(analysisfilelist);
568 // Process the events
569 rv = ProcessChain(analysisChain) ;
574 //______________________________________________________________________
575 const Bool_t AliAnalysisGoodies::ProcessTagXmlCollection(const char * xmlFile, AliRunTagCuts *runCuts, AliEventTagCuts * evtCuts) const
577 // process the events in a xml ESD collection with an Analysis Task
578 // usage ProcessLocalEsdFile(xmlFile)
579 // xmlFile: is the local xml file with the tag collection ( ex: tagCollection.xml)
583 if ( !evtCuts && !runCuts ) {
584 AliError("No Tag cuts provided") ;
588 printf("*** Process ***\n");
589 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
590 printf("*** Coll = |%s| \n",xmlFile);
592 // check if file is local or alien
593 if ( gSystem->AccessPathName(xmlFile) )
594 TGrid::Connect("alien://");
596 TAlienCollection * collection = TAlienCollection::Open(xmlFile) ;
598 AliError(Form("%s not found", xmlFile)) ;
602 TGridResult* result = collection->GetGridResult("");
603 AliTagAnalysis * tagAna = new AliTagAnalysis();
604 tagAna->ChainGridTags(result);
606 // Query the tag file and make the analysis chain
607 TChain * analysisChain = new TChain(fESDTreeName) ;
608 analysisChain = tagAna->QueryTags(runCuts, evtCuts);
610 // Process the events
611 rv = ProcessChain(analysisChain) ;
616 //______________________________________________________________________
617 // const Bool_t AliAnalysisGoodies::ProcessTagXmlCollection(const char * xmlFile, const char * runCuts, const char * evtCuts) const
619 // // process the events in a xml ESD collection with an Analysis Task
620 // // usage ProcessLocalEsdFile(xmlFile)
621 // // xmlFile: is the local xml file with the tag collection ( ex: tagCollection.xml)
623 // Bool_t rv = kTRUE ;
625 // if ( !evtCuts && !runCuts ) {
626 // AliError("No Tag cuts provided") ;
630 // printf("*** Process ***\n");
631 // printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
632 // printf("*** Coll = |%s| \n",xmlFile);
634 // // check if file is local or alien
635 // if ( gSystem->AccessPathName(xmlFile) )
636 // TGrid::Connect("alien://");
638 // TAlienCollection * collection = TAlienCollection::Open(xmlFile) ;
639 // if (! collection) {
640 // AliError(Form("%s not found", xmlFile)) ;
644 // TGridResult* result = collection->GetGridResult("");
645 // AliTagAnalysis * tagAna = new AliTagAnalysis();
646 // tagAna->ChainGridTags(result);
648 // // Query the tag file and make the analysis chain
649 // TChain * analysisChain = new TChain(fESDTreeName) ;
650 // analysisChain = tagAna->QueryTags(runCuts, evtCuts);
652 // // Process the events
653 // rv = ProcessChain(analysisChain) ;
658 //______________________________________________________________________
659 const Bool_t AliAnalysisGoodies::Register( const char * lfndir, const char * pfndir, const char * file)
661 // register files already stored in a MSS into the AliEn catalog
662 // usage: Register(lfndir, pfndir, pfnFileName)
663 // lfndir : AliEn directory ( ex: /alice/data/2006/LHC06c/PHOS_TestBeam/ )
664 // pfndir : MSS directory ( ex: /castor/cern.ch/alice/testbeam/phos/2006 )
665 // file : text file with a list of the file names to be registered
673 AliError(Form("Cannot open file %s\n", file)) ;
677 TGrid::Connect("alien://");
679 char fileName[1024] ;
687 sprintf(lfn, "%s/%s", lfndir, fileName) ;
691 sprintf(pfn, "castor://Alice::CERN::Castor2/%s/%s", pfndir, fileName) ;
693 printf("Register %s as %s\n", pfn, lfn) ;
695 gGrid->Register(lfn, pfn) ;
704 //______________________________________________________________________
705 void AliAnalysisGoodies::SetTasks(Int_t nb, AliAnalysisTask ** taskList, TClass ** inputType, TClass ** outputType)
707 // define a task with its output and input type
711 fTaskList = taskList ;
712 fTaskInType = inputType ;
713 fTaskOuType = outputType ;