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>
34 #include <TAlienCollection.h>
37 #include <TFileMerger.h>
43 //______________________________________________________________________________
44 AliAnalysisGoodies::AliAnalysisGoodies() :
45 fESDTreeName("esdTree"),
53 TString token = gSystem->Getenv("GRID_TOKEN") ;
56 TGrid::Connect("alien://");
58 AliInfo("You are not connected to the GRID") ;
61 //______________________________________________________________________________
62 void AliAnalysisGoodies::Help() const
64 AliInfo("Analysis utilities:\n") ;
65 printf(" *** Alien2Local : copy files ESD files listed in an xml collection from AliEn catalog to local storage and creates a local xml collection \n") ;
66 printf(" usage: Alien2Local(in, out)\n") ;
67 printf(" in: a xml esd collection file name \n") ;
68 printf(" ou: the local directory where to save the esd root files \n") ;
69 printf(" *** Make : makes esd collection from tags \n") ;
70 printf(" usage: Make(tags, esds)\n") ;
71 printf(" tags: is either a tag root file or an xml tag collection \n") ;
72 printf(" esds: is an esd collection \n") ;
73 printf(" *** Merge : merges files listed in a xml collection \n") ;
74 printf(" usage Merge(collection, outputDile)\n") ;
75 printf(" collection: is a xml collection \n") ;
76 printf(" *** Process : process the events with an Analysis Task \n") ;
77 printf(" usage: Process(esdFile, tagCuts) \n") ;
78 printf(" esdFile: can be a root file with the ESD Tree ( ex: esd?AliESDs.root) \n") ;
79 printf(" or a root file with the Tag Tree ( ex: tag?Run100.Event0_100.ESD.tag.root) \n") ;
80 printf(" or a local or alien xml file with the ESD collection ( ex: esd?esdCollection.xml) \n") ;
81 printf(" or a local or alien xml file with the TAG collection ( ex: tag?tagCollection.xml) \n") ;
82 printf(" or a TChain of esd TTrees \n") ;
83 printf(" tagCuts: is the AliEventTagCuts (needed only for tag? cases \n") ;
84 printf(" *** Register: register files already stored in a MSS into the AliEn catalog\n") ;
85 printf(" usage: Register(lfndir, pfndir, pfnFileName) \n") ;
86 printf(" lfndir : AliEn directory ( ex: /alice/data/2006/LHC06c/PHOS_TestBeam/\n") ;
87 printf(" pfndir : MSS directory ( ex: /castor/cern.ch/alice/testbeam/phos/2006 \n") ;
88 printf(" file : text file with a list of the file names to be registered\n ") ;
92 //______________________________________________________________________
93 const Bool_t AliAnalysisGoodies::Alien2Local(const TString collectionNameIn, const TString localDir)
95 // copy files ESD files listed in an xml collection from AliEn catalog to local storage and creates a local xml collection
96 // usage: Alien2Local(in, out)
97 // in: a xml esd collection file name
98 // ou: the local directory where to save the esd root files
104 AliXMLCollection * collectionIn = AliXMLCollection::Open(collectionNameIn) ;
105 collectionIn->Reset() ;
107 AliXMLCollection * collectionOu = new AliXMLCollection() ;
108 TString collectionNameOu(collectionIn->GetCollectionName()) ;
109 collectionNameOu.Append("Local") ;
110 collectionOu->SetCollectionName(collectionNameOu) ;
111 collectionOu->WriteHeader() ;
115 const char* ocwd = gSystem->WorkingDirectory();
118 while ( collectionIn->Next() ) {
119 gSystem->ChangeDirectory(localDir) ;
120 TString fileTURL = collectionIn->GetTURL("") ;
122 TString tempo(fileTURL) ;
123 tempo.Remove(tempo.Last('/'), tempo.Length()) ;
124 TString evtsNumber = tempo(tempo.Last('/')+1, tempo.Length())+"/";
125 tempo.Remove(tempo.Last('/'), tempo.Length()) ;
126 TString runNumber = tempo(tempo.Last('/')+1, tempo.Length())+"/" ;
127 TString dir = localDir + runNumber ;
128 gSystem->MakeDirectory(dir) ;
129 gSystem->ChangeDirectory(dir) ;
130 dir += evtsNumber + "/";
131 gSystem->MakeDirectory(dir) ;
132 gSystem->ChangeDirectory(dir) ;
133 dir += collectionIn->GetCollectionName() ;
134 TEntryList * list = collectionIn->GetEventList("") ;
136 collectionOu->WriteBody(counter, collectionIn->GetGUID(""), collectionIn->GetLFN(""), collectionIn->GetTURL(""), list) ;
138 printf("Copying %s to %s\n", fileTURL.Data(), dir.Data()) ;
139 merger.Cp(fileTURL, dir) ;
141 collectionOu->Export() ;
142 gSystem->ChangeDirectory(ocwd) ;
150 //______________________________________________________________________
151 const Bool_t AliAnalysisGoodies::Make(AliRunTagCuts *runCuts, AliEventTagCuts *evtCuts, const char * in, const char * out) const
153 // makes esd collection from tags
154 // usage Make(tags, esds)
155 // tags: is either a tag root file or an xml tag collection
156 // esds: is an esd collection
160 if ( !evtCuts && !runCuts ) {
161 AliError("No Tag cuts provided") ;
166 if ( file.Contains(".root") )
167 rv = MakeEsdCollectionFromTagFile(runCuts, evtCuts, file.Data(), out) ;
168 else if ( file.Contains(".xml") )
169 rv = MakeEsdCollectionFromTagCollection(runCuts, evtCuts, file.Data(), out) ;
171 AliError(Form("%s is not a valid file format", in)) ;
178 //______________________________________________________________________
179 const Bool_t AliAnalysisGoodies::MakeEsdCollectionFromTagFile(AliRunTagCuts *runCuts, AliEventTagCuts *evtCuts, const char * in, const char * out) const
181 // Makes an esd collection from a root tag file
183 // Open the file collection
184 printf("*** Create Collection ***\n");
185 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
186 printf("*** file = |%s| \n",in);
188 AliTagAnalysis * tagAna = new AliTagAnalysis();
189 rv = tagAna->AddTagsFile(in);
193 tagAna->CreateXMLCollection(out, runCuts, evtCuts) ;
199 //______________________________________________________________________
200 const Bool_t AliAnalysisGoodies::MakeEsdCollectionFromTagCollection(AliRunTagCuts * runCuts, AliEventTagCuts * evtCuts, const char * in, const char * out) const
202 // Makes an esd collection from a xml tag collection
204 // Open the file collection
205 printf("*** Create Collection ***\n");
206 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
207 printf("*** Coll = |%s| \n",in);
211 TAlienCollection * collection = TAlienCollection::Open(in);
212 TGridResult* result = collection->GetGridResult("");
213 AliTagAnalysis * tagAna = new AliTagAnalysis();
214 tagAna->ChainGridTags(result);
216 tagAna->CreateXMLCollection(out, runCuts, evtCuts) ;
224 //______________________________________________________________________
225 const Bool_t AliAnalysisGoodies::MakeEsdCollectionFromTagCollection(const char * runCuts, const char * evtCuts, const char * in, const char * out) const
227 // Makes an esd collection from a xml tag collection
231 // Open the file collection
232 printf("*** Create Collection ***\n");
233 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
234 printf("*** Coll = |%s| \n",in);
238 TAlienCollection * collection = TAlienCollection::Open(in);
239 TGridResult* result = collection->GetGridResult("");
240 AliTagAnalysis * tagAna = new AliTagAnalysis();
241 tagAna->ChainGridTags(result);
243 tagAna->CreateXMLCollection(out, runCuts, evtCuts) ;
251 //______________________________________________________________________
252 const Bool_t AliAnalysisGoodies::Merge(const char * collectionFile, const char * subFile, const char * outFile)
254 // merges files listed in a xml collection
255 // usage Merge(collection, outputFile))
256 // collection: is a xml collection
260 if ( strstr(collectionFile, ".xml") == 0 ) {
261 AliError("Input collection file must be an \".xml\" file\n") ;
267 // Open the file collection
268 printf("*** Create Collection ***\n");
269 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
270 printf("*** Coll = |%s| \n",collectionFile);
274 TAlienCollection * collection = TAlienCollection::Open(collectionFile);
275 TGridResult* result = collection->GetGridResult("");
281 TString tempo(collectionFile) ;
283 tempo.ReplaceAll(".xml", subFile) ;
285 tempo.ReplaceAll(".xml", "_Merged.root") ;
286 outFile = tempo.Data() ;
288 merger.OutputFile(outFile) ;
290 while ( (turl = result->GetKey(index, "turl")) ) {
293 sprintf(file, "%s#%s", turl, subFile) ;
295 sprintf(file, "%s", turl) ;
297 printf("%s\n", file) ;
298 merger.AddFile(file) ;
305 AliInfo(Form("Files merged into %s\n", outFile)) ;
316 //______________________________________________________________________
317 const Bool_t AliAnalysisGoodies::Process(TChain * chain)
319 // process events starting from a chain of esd Trees
324 rv = ProcessChain(chain) ;
332 //______________________________________________________________________
333 const Bool_t AliAnalysisGoodies::Process(const char * inFile)
335 // process the events with an Analysis Task
336 // usage Process(esdFile)
337 // esdFile: is of the form opt?file_lfn
339 AliRunTagCuts * runCuts = 0x0 ;
340 AliEventTagCuts * evtCuts = 0x0 ;
342 rv = Process(inFile, runCuts, evtCuts) ;
347 //______________________________________________________________________
348 const Bool_t AliAnalysisGoodies::Process(const char * inFile, AliRunTagCuts *runCuts, AliEventTagCuts * evtCuts )
350 // process the events with an Analysis Task
351 // usage Process(esdFile, runtagCuts, evtTagCuts)
352 // esdFile: is of the form opt?file_lfn
358 TString file(inFile) ;
359 if ( file.Contains("esd?") && file.Contains(".root") ) {
360 file.ReplaceAll("esd?", "") ;
361 rv = ProcessEsdFile(file.Data()) ;
363 } else if ( file.Contains("esd?") && file.Contains(".xml") ) {
364 file.ReplaceAll("esd?", "") ;
365 rv = ProcessEsdXmlCollection(file.Data()) ;
367 } else if (file.Contains("tag?") && file.Contains(".root") ) {
368 file.ReplaceAll("tag?", "") ;
369 rv = ProcessTagFile(file.Data(), runCuts, evtCuts) ;
371 } else if (file.Contains("tag?") && file.Contains(".xml") ) {
372 file.ReplaceAll("tag?", "") ;
373 rv = ProcessTagXmlCollection(file.Data(), runCuts, evtCuts) ;
376 AliError(Form("%s is not a valid file format", inFile)) ;
386 //______________________________________________________________________
387 const Bool_t AliAnalysisGoodies::Process(const char * inFile, const char * runCuts, const char * evtCuts)
389 // process the events with an Analysis Task
390 // usage Process(esdFile, runtagCuts, evtTagCuts)
391 // esdFile: is of the form opt?file_lfn
397 TString file(inFile) ;
398 if ( file.Contains("esd?") && file.Contains(".root") ) {
399 file.ReplaceAll("esd?", "") ;
400 rv = ProcessEsdFile(file.Data()) ;
402 } else if ( file.Contains("esd?") && file.Contains(".xml") ) {
403 file.ReplaceAll("esd?", "") ;
404 rv = ProcessEsdXmlCollection(file.Data()) ;
406 } else if (file.Contains("tag?") && file.Contains(".root") ) {
407 file.ReplaceAll("tag?", "") ;
408 rv = ProcessTagFile(file.Data(), runCuts, evtCuts) ;
410 } else if (file.Contains("tag?") && file.Contains(".xml") ) {
411 file.ReplaceAll("tag?", "") ;
412 rv = ProcessTagXmlCollection(file.Data(), runCuts, evtCuts) ;
415 AliError(Form("%s is not a valid file format", inFile)) ;
425 //______________________________________________________________________
426 const Bool_t AliAnalysisGoodies::ProcessChain(TChain * chain) const
433 AliError("No tasks defined") ;
437 // Make the analysis manager
438 AliAnalysisManager * mgr = new AliAnalysisManager() ;
441 // The top input must be common to all top tasks
442 TClass * classIn = fTaskInType[0] ;
443 AliAnalysisDataContainer * taskInput = mgr->CreateContainer("Input Container", classIn, AliAnalysisManager::kInputContainer) ;
445 for (index = 0; index < fnumberOfTasks; index++) {
446 AliAnalysisTask * task = fTaskList[index] ;
449 // Create containers for input/output
450 TClass * classOu = fTaskOuType[index] ;
451 AliAnalysisDataContainer * taskOutput = mgr->CreateContainer("Output Container", classOu, AliAnalysisManager::kOutputContainer) ;
452 mgr->ConnectInput (task, 0, taskInput);
453 mgr->ConnectOutput(task, 0, taskOutput);
457 taskInput->SetData(chain);
459 if (mgr->InitAnalysis()) {
468 //______________________________________________________________________
469 const Bool_t AliAnalysisGoodies::ProcessEsdFile(const char * esdFile) const
471 // process the events in a single ESD file with an Analysis Task
472 // usage ProcessLocalEsdFile(esdFile)
473 // esdFile: is the root file (local or in alien) with the ESD Tree ( ex: AliESDs.root)
477 printf("*** Process ***\n");
478 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
479 printf("*** Coll = |%s| \n",esdFile);
481 // Makes the ESD chain
482 printf("*** Getting the Chain ***\n");
483 TChain* analysisChain = new TChain(fESDTreeName) ;
484 analysisChain->AddFile(esdFile);
486 // Process the events
487 rv = ProcessChain(analysisChain) ;
492 //______________________________________________________________________
493 const Bool_t AliAnalysisGoodies::ProcessTagFile(const char * tagFile, AliRunTagCuts *runCuts, AliEventTagCuts *evtCuts) const
495 // process the events in a single Tag file with an Analysis Task
496 // usage ProcessLocalEsdFile(tagFile)
497 // tagFile: is the root file (local or in alien) with the Tag Tree (ex: Run102.Event0_100.ESD.tag.root)
501 if ( !evtCuts && !runCuts ) {
502 AliError("No Tag cuts provided") ;
506 printf("*** Process ***\n");
507 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
508 printf("*** Coll = |%s| \n",tagFile);
510 AliTagAnalysis * tagAna = new AliTagAnalysis();
511 rv = tagAna->AddTagsFile(tagFile);
515 // Query the tag file and make the analysis chain
516 TChain * analysisChain = new TChain(fESDTreeName) ;
517 analysisChain = tagAna->QueryTags(runCuts, evtCuts);
519 // Process the events
520 rv = ProcessChain(analysisChain) ;
525 //______________________________________________________________________
526 const Bool_t AliAnalysisGoodies::ProcessTagFile(const char * tagFile, const char * runCuts, const char * evtCuts) const
528 // process the events in a single Tag file with an Analysis Task
529 // usage ProcessLocalEsdFile(tagFile)
530 // tagFile: is the root file (local or in alien) with the Tag Tree (ex: Run102.Event0_100.ESD.tag.root)
535 if ( !evtCuts && !runCuts ) {
536 AliError("No Tag cuts provided") ;
540 printf("*** Process ***\n");
541 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
542 printf("*** Coll = |%s| \n",tagFile);
544 AliTagAnalysis * tagAna = new AliTagAnalysis();
545 rv = tagAna->AddTagsFile(tagFile);
549 // Query the tag file and make the analysis chain
550 TChain * analysisChain = new TChain(fESDTreeName) ;
551 analysisChain = tagAna->QueryTags(runCuts, evtCuts);
553 // Process the events
554 rv = ProcessChain(analysisChain) ;
559 //______________________________________________________________________
560 const Bool_t AliAnalysisGoodies::ProcessEsdXmlCollection(const char * xmlFile) const
562 // process the events in a xml ESD collection with an Analysis Task
563 // usage ProcessLocalEsdFile(xmlFile)
564 // xmlFile: is the local xml file with the ESD collection ( ex: esdCollection.xml)
568 printf("*** Process ***\n");
569 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
570 printf("*** Coll = |%s| \n",xmlFile);
574 TAlienCollection * collection = TAlienCollection::Open(xmlFile) ;
576 AliError(Form("%s not found", xmlFile)) ;
580 TGridResult* result = collection->GetGridResult("");
581 TList* analysisfilelist = result->GetFileInfoList();
583 // Makes the ESD chain
584 printf("*** Getting the Chain ***\n");
585 TChain* analysisChain = new TChain(fESDTreeName);
586 analysisChain->AddFileInfoList(analysisfilelist);
588 // Process the events
589 rv = ProcessChain(analysisChain) ;
597 //______________________________________________________________________
598 const Bool_t AliAnalysisGoodies::ProcessTagXmlCollection(const char * xmlFile, AliRunTagCuts *runCuts, AliEventTagCuts * evtCuts) const
600 // process the events in a xml ESD collection with an Analysis Task
601 // usage ProcessLocalEsdFile(xmlFile)
602 // xmlFile: is the local xml file with the tag collection ( ex: tagCollection.xml)
606 if ( !evtCuts && !runCuts ) {
607 AliError("No Tag cuts provided") ;
611 printf("*** Process ***\n");
612 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
613 printf("*** Coll = |%s| \n",xmlFile);
615 // check if file is local or alien
616 if ( gSystem->AccessPathName(xmlFile) )
617 TGrid::Connect("alien://");
621 TAlienCollection * collection = TAlienCollection::Open(xmlFile) ;
623 AliError(Form("%s not found", xmlFile)) ;
627 TGridResult* result = collection->GetGridResult("");
628 AliTagAnalysis * tagAna = new AliTagAnalysis();
629 tagAna->ChainGridTags(result);
631 // Query the tag file and make the analysis chain
632 TChain * analysisChain = new TChain(fESDTreeName) ;
633 analysisChain = tagAna->QueryTags(runCuts, evtCuts);
635 // Process the events
636 rv = ProcessChain(analysisChain) ;
644 //______________________________________________________________________
645 const Bool_t AliAnalysisGoodies::ProcessTagXmlCollection(const char * xmlFile, const char * runCuts, const char * evtCuts) const
647 // process the events in a xml ESD collection with an Analysis Task
648 // usage ProcessLocalEsdFile(xmlFile)
649 // xmlFile: is the local xml file with the tag collection ( ex: tagCollection.xml)
653 if ( !evtCuts && !runCuts ) {
654 AliError("No Tag cuts provided") ;
658 printf("*** Process ***\n");
659 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
660 printf("*** Coll = |%s| \n",xmlFile);
664 // check if file is local or alien
665 if ( gSystem->AccessPathName(xmlFile) )
666 TGrid::Connect("alien://");
668 TAlienCollection * collection = TAlienCollection::Open(xmlFile) ;
670 AliError(Form("%s not found", xmlFile)) ;
674 TGridResult* result = collection->GetGridResult("");
675 AliTagAnalysis * tagAna = new AliTagAnalysis();
676 tagAna->ChainGridTags(result);
678 // Query the tag file and make the analysis chain
679 TChain * analysisChain = new TChain(fESDTreeName) ;
680 analysisChain = tagAna->QueryTags(runCuts, evtCuts);
682 // Process the events
683 rv = ProcessChain(analysisChain) ;
691 //______________________________________________________________________
692 const Bool_t AliAnalysisGoodies::Register( const char * lfndir, const char * pfndir, const char * file)
694 // register files already stored in a MSS into the AliEn catalog
695 // usage: Register(lfndir, pfndir, pfnFileName)
696 // lfndir : AliEn directory ( ex: /alice/data/2006/LHC06c/PHOS_TestBeam/ )
697 // pfndir : MSS directory ( ex: /castor/cern.ch/alice/testbeam/phos/2006 )
698 // file : text file with a list of the file names to be registered
706 AliError(Form("Cannot open file %s\n", file)) ;
710 TGrid::Connect("alien://");
712 char fileName[1024] ;
720 sprintf(lfn, "%s/%s", lfndir, fileName) ;
724 sprintf(pfn, "castor://Alice::CERN::Castor2/%s/%s", pfndir, fileName) ;
726 printf("Register %s as %s\n", pfn, lfn) ;
728 gGrid->Register(lfn, pfn) ;
737 //______________________________________________________________________
738 void AliAnalysisGoodies::SetTasks(Int_t nb, AliAnalysisTask ** taskList, TClass ** inputType, TClass ** outputType)
740 // define a task with its output and input type
744 fTaskList = taskList ;
745 fTaskInType = inputType ;
746 fTaskOuType = outputType ;