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>
35 #include <TGridResult.h>
36 #include <TFileMerger.h>
42 #include <TEntryList.h>
44 //______________________________________________________________________________
45 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 void AliAnalysisGoodies::Help() const
65 AliInfo("Analysis utilities:\n") ;
66 printf(" *** Alien2Local : copy files ESD files listed in an xml collection from AliEn catalog to local storage and creates a local xml collection \n") ;
67 printf(" usage: Alien2Local(in, out)\n") ;
68 printf(" in: a xml esd collection file name \n") ;
69 printf(" ou: the local directory where to save the esd root files \n") ;
70 printf(" *** Make : makes esd collection from tags \n") ;
71 printf(" usage: Make(tags, esds)\n") ;
72 printf(" tags: is either a tag root file or an xml tag collection \n") ;
73 printf(" esds: is an esd collection \n") ;
74 printf(" *** Merge : merges files listed in a xml collection \n") ;
75 printf(" usage Merge(collection, outputDile)\n") ;
76 printf(" collection: is a xml collection \n") ;
77 printf(" *** Process : process the events with an Analysis Task \n") ;
78 printf(" usage: Process(esdFile, tagCuts) \n") ;
79 printf(" esdFile: can be a root file with the ESD Tree ( ex: esd?AliESDs.root) \n") ;
80 printf(" or a root file with the Tag Tree ( ex: tag?Run100.Event0_100.ESD.tag.root) \n") ;
81 printf(" or a local or alien xml file with the ESD collection ( ex: esd?esdCollection.xml) \n") ;
82 printf(" or a local or alien xml file with the TAG collection ( ex: tag?tagCollection.xml) \n") ;
83 printf(" or a TChain of esd TTrees \n") ;
84 printf(" tagCuts: is the AliEventTagCuts (needed only for tag? cases \n") ;
85 printf(" *** Register: register files already stored in a MSS into the AliEn catalog\n") ;
86 printf(" usage: Register(lfndir, pfndir, pfnFileName) \n") ;
87 printf(" lfndir : AliEn directory ( ex: /alice/data/2006/LHC06c/PHOS_TestBeam/\n") ;
88 printf(" pfndir : MSS directory ( ex: /castor/cern.ch/alice/testbeam/phos/2006 \n") ;
89 printf(" file : text file with a list of the file names to be registered\n ") ;
93 //______________________________________________________________________
94 Bool_t AliAnalysisGoodies::Alien2Local(const TString collectionNameIn, const TString localDir)
96 // copy files ESD files listed in an xml collection from AliEn catalog to local storage and creates a local xml collection
97 // usage: Alien2Local(in, out)
98 // in: a xml esd collection file name
99 // ou: the local directory where to save the esd root files
106 AliXMLCollection * collectionIn = AliXMLCollection::Open(collectionNameIn) ;
107 collectionIn->Reset() ;
109 AliXMLCollection * collectionOu = new AliXMLCollection() ;
110 TString collectionNameOu(collectionIn->GetCollectionName()) ;
111 collectionNameOu.Append("Local") ;
112 collectionOu->SetCollectionName(collectionNameOu) ;
113 collectionOu->WriteHeader() ;
117 const char* ocwd = gSystem->WorkingDirectory();
120 while ( collectionIn->Next() ) {
121 gSystem->ChangeDirectory(localDir) ;
122 TString fileTURL = collectionIn->GetTURL("") ;
124 TString tempo(fileTURL) ;
125 tempo.Remove(tempo.Last('/'), tempo.Length()) ;
126 TString evtsNumber = tempo(tempo.Last('/')+1, tempo.Length())+"/";
127 tempo.Remove(tempo.Last('/'), tempo.Length()) ;
128 TString runNumber = tempo(tempo.Last('/')+1, tempo.Length())+"/" ;
129 TString dir = localDir + runNumber ;
132 sprintf(line, ".! mkdir -p %s", dir.Data()) ;
133 gROOT->ProcessLine(line) ;
134 printf("***************************%s\n", line) ;
135 TEntryList * list = collectionIn->GetEventList("") ;
137 list = new TEntryList() ;
139 TString filename = tempo(tempo.Last('/')+1, tempo.Length()) ;
141 AliInfo(Form("Copying %s to %s\n", fileTURL.Data(), dir.Data())) ;
142 collectionOu->WriteBody(counter, collectionIn->GetGUID(""), collectionIn->GetLFN(""), dir, list) ;
144 merger.Cp(fileTURL, dir) ;
146 collectionOu->Export() ;
147 gSystem->ChangeDirectory(ocwd) ;
158 //______________________________________________________________________
159 Bool_t AliAnalysisGoodies::Make(AliRunTagCuts *runCuts, AliEventTagCuts *evtCuts, const char * in, const char * out) const
161 // makes esd collection from tags
162 // usage Make(tags, esds)
163 // tags: is either a tag root file or an xml tag collection
164 // esds: is an esd collection
168 if ( !evtCuts && !runCuts ) {
169 AliError("No Tag cuts provided") ;
174 if ( file.Contains(".root") )
175 rv = MakeEsdCollectionFromTagFile(runCuts, evtCuts, file.Data(), out) ;
176 else if ( file.Contains(".xml") )
177 rv = MakeEsdCollectionFromTagCollection(runCuts, evtCuts, file.Data(), out) ;
179 AliError(Form("%s is not a valid file format", in)) ;
186 //______________________________________________________________________
187 Bool_t AliAnalysisGoodies::MakeEsdCollectionFromTagFile(AliRunTagCuts *runCuts, AliEventTagCuts *evtCuts, const char * in, const char * out) const
189 // Makes an esd collection from a root tag file
191 // Open the file collection
192 printf("*** Create Collection ***\n");
193 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
194 printf("*** file = |%s| \n",in);
196 AliTagAnalysis * tagAna = new AliTagAnalysis();
197 rv = tagAna->AddTagsFile(in);
201 tagAna->CreateXMLCollection(out, runCuts, evtCuts) ;
207 //______________________________________________________________________
208 Bool_t AliAnalysisGoodies::MakeEsdCollectionFromTagCollection(AliRunTagCuts * runCuts, AliEventTagCuts * evtCuts, const char * in, const char * out) const
210 // Makes an esd collection from a xml tag collection
212 // Open the file collection
213 printf("*** Create Collection ***\n");
214 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
215 printf("*** Coll = |%s| \n",in);
219 TGridCollection * collection = TAlienCollection::Open(in);
220 TGridResult* result = collection->GetGridResult("");
221 AliTagAnalysis * tagAna = new AliTagAnalysis();
222 tagAna->ChainGridTags(result);
224 tagAna->CreateXMLCollection(out, runCuts, evtCuts) ;
232 //______________________________________________________________________
233 Bool_t AliAnalysisGoodies::MakeEsdCollectionFromTagCollection(const char * runCuts, const char * evtCuts, const char * in, const char * out) const
235 // Makes an esd collection from a xml tag collection
239 // Open the file collection
240 printf("*** Create Collection ***\n");
241 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
242 printf("*** Coll = |%s| \n",in);
246 TGridCollection * collection = TAlienCollection::Open(in);
247 TGridResult* result = collection->GetGridResult("");
248 AliTagAnalysis * tagAna = new AliTagAnalysis();
249 tagAna->ChainGridTags(result);
251 tagAna->CreateXMLCollection(out, runCuts, evtCuts) ;
259 //______________________________________________________________________
260 Bool_t AliAnalysisGoodies::Merge(const char * collectionFile, const char * subFile, const char * outFile)
262 // merges files listed in a xml collection
263 // usage Merge(collection, outputFile))
264 // collection: is a xml collection
268 if ( strstr(collectionFile, ".xml") == 0 ) {
269 AliError("Input collection file must be an \".xml\" file\n") ;
275 // Open the file collection
276 printf("*** Create Collection ***\n");
277 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
278 printf("*** Coll = |%s| \n",collectionFile);
282 TGridCollection * collection = TAlienCollection::Open(collectionFile);
283 TGridResult* result = collection->GetGridResult("");
289 TString tempo(collectionFile) ;
291 tempo.ReplaceAll(".xml", subFile) ;
293 tempo.ReplaceAll(".xml", "_Merged.root") ;
294 outFile = tempo.Data() ;
296 merger.OutputFile(outFile) ;
298 while ( (turl = result->GetKey(index, "turl")) ) {
301 sprintf(file, "%s#%s", turl, subFile) ;
303 sprintf(file, "%s", turl) ;
305 printf("%s\n", file) ;
306 merger.AddFile(file) ;
313 AliInfo(Form("Files merged into %s\n", outFile)) ;
324 //______________________________________________________________________
325 Bool_t AliAnalysisGoodies::Process(TChain * chain)
327 // process events starting from a chain of esd Trees
331 rv = ProcessChain(chain) ;
339 //______________________________________________________________________
340 Bool_t AliAnalysisGoodies::Process(const char * inFile)
342 // process the events with an Analysis Task
343 // usage Process(esdFile)
344 // esdFile: is of the form opt?file_lfn
346 AliRunTagCuts * runCuts = 0x0 ;
347 AliEventTagCuts * evtCuts = 0x0 ;
349 rv = Process(inFile, runCuts, evtCuts) ;
354 //______________________________________________________________________
355 Bool_t AliAnalysisGoodies::Process(const char * inFile, AliRunTagCuts *runCuts, AliEventTagCuts * evtCuts )
357 // process the events with an Analysis Task
358 // usage Process(esdFile, runtagCuts, evtTagCuts)
359 // esdFile: is of the form opt?file_lfn
365 TString file(inFile) ;
366 if ( file.Contains("esd?") && file.Contains(".root") ) {
367 file.ReplaceAll("esd?", "") ;
368 rv = ProcessEsdFile(file.Data()) ;
370 } else if ( file.Contains("esd?") && file.Contains(".xml") ) {
371 file.ReplaceAll("esd?", "") ;
372 rv = ProcessEsdXmlCollection(file.Data()) ;
374 } else if (file.Contains("tag?") && file.Contains(".root") ) {
375 file.ReplaceAll("tag?", "") ;
376 rv = ProcessTagFile(file.Data(), runCuts, evtCuts) ;
378 } else if (file.Contains("tag?") && file.Contains(".xml") ) {
379 file.ReplaceAll("tag?", "") ;
380 rv = ProcessTagXmlCollection(file.Data(), runCuts, evtCuts) ;
383 AliError(Form("%s is not a valid file format", inFile)) ;
393 //______________________________________________________________________
394 Bool_t AliAnalysisGoodies::Process(const char * inFile, const char * runCuts, const char * evtCuts)
396 // process the events with an Analysis Task
397 // usage Process(esdFile, runtagCuts, evtTagCuts)
398 // esdFile: is of the form opt?file_lfn
404 TString file(inFile) ;
405 if ( file.Contains("esd?") && file.Contains(".root") ) {
406 file.ReplaceAll("esd?", "") ;
407 rv = ProcessEsdFile(file.Data()) ;
409 } else if ( file.Contains("esd?") && file.Contains(".xml") ) {
410 file.ReplaceAll("esd?", "") ;
411 rv = ProcessEsdXmlCollection(file.Data()) ;
413 } else if (file.Contains("tag?") && file.Contains(".root") ) {
414 file.ReplaceAll("tag?", "") ;
415 rv = ProcessTagFile(file.Data(), runCuts, evtCuts) ;
417 } else if (file.Contains("tag?") && file.Contains(".xml") ) {
418 file.ReplaceAll("tag?", "") ;
419 rv = ProcessTagXmlCollection(file.Data(), runCuts, evtCuts) ;
422 AliError(Form("%s is not a valid file format", inFile)) ;
432 //______________________________________________________________________
433 Bool_t AliAnalysisGoodies::ProcessChain(TChain * chain) const
440 AliError("No tasks defined") ;
444 // Make the analysis manager
445 AliAnalysisManager * mgr = new AliAnalysisManager("Goodies Manager", "Analysis manager created by AliAnalysisGoodies") ;
448 // The top input must be common to all top tasks
449 TClass * classIn = fTaskInType[0] ;
450 AliAnalysisDataContainer * taskInput = mgr->CreateContainer("InputContainer", classIn, AliAnalysisManager::kInputContainer) ;
452 for (index = 0; index < fnumberOfTasks; index++) {
453 AliAnalysisTask * task = fTaskList[index] ;
456 // Create containers for input/output
457 TClass * classOu = fTaskOuType[index] ;
458 AliAnalysisDataContainer * taskOutput = mgr->CreateContainer(Form("OutputContainer%d",index), classOu, AliAnalysisManager::kOutputContainer,
459 Form("%s.root",task->GetName())) ;
460 mgr->ConnectInput (task, 0, taskInput);
461 mgr->ConnectOutput(task, 0, taskOutput);
465 // taskInput->SetData(chain);
467 if (mgr->InitAnalysis()) {
469 // chain->Process(mgr);
470 mgr->StartAnalysis("local",chain);
477 //______________________________________________________________________
478 Bool_t AliAnalysisGoodies::ProcessEsdFile(const char * esdFile) const
480 // process the events in a single ESD file with an Analysis Task
481 // usage ProcessLocalEsdFile(esdFile)
482 // esdFile: is the root file (local or in alien) with the ESD Tree ( ex: AliESDs.root)
486 printf("*** Process ***\n");
487 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
488 printf("*** Coll = |%s| \n",esdFile);
490 // Makes the ESD chain
491 printf("*** Getting the Chain ***\n");
492 TChain* analysisChain = new TChain(fESDTreeName) ;
493 analysisChain->AddFile(esdFile);
495 // Process the events
496 rv = ProcessChain(analysisChain) ;
501 //______________________________________________________________________
502 Bool_t AliAnalysisGoodies::ProcessTagFile(const char * tagFile, AliRunTagCuts *runCuts, AliEventTagCuts *evtCuts) const
504 // process the events in a single Tag file with an Analysis Task
505 // usage ProcessLocalEsdFile(tagFile)
506 // tagFile: is the root file (local or in alien) with the Tag Tree (ex: Run102.Event0_100.ESD.tag.root)
510 if ( !evtCuts && !runCuts ) {
511 AliError("No Tag cuts provided") ;
515 printf("*** Process ***\n");
516 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
517 printf("*** Coll = |%s| \n",tagFile);
519 AliTagAnalysis * tagAna = new AliTagAnalysis();
520 rv = tagAna->AddTagsFile(tagFile);
524 // Query the tag file and make the analysis chain
525 TChain * analysisChain = new TChain(fESDTreeName) ;
526 analysisChain = tagAna->QueryTags(runCuts, evtCuts);
528 // Process the events
529 rv = ProcessChain(analysisChain) ;
534 //______________________________________________________________________
535 Bool_t AliAnalysisGoodies::ProcessTagFile(const char * tagFile, const char * runCuts, const char * evtCuts) const
537 // process the events in a single Tag file with an Analysis Task
538 // usage ProcessLocalEsdFile(tagFile)
539 // tagFile: is the root file (local or in alien) with the Tag Tree (ex: Run102.Event0_100.ESD.tag.root)
544 if ( !evtCuts && !runCuts ) {
545 AliError("No Tag cuts provided") ;
549 printf("*** Process ***\n");
550 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
551 printf("*** Coll = |%s| \n",tagFile);
553 AliTagAnalysis * tagAna = new AliTagAnalysis();
554 rv = tagAna->AddTagsFile(tagFile);
558 // Query the tag file and make the analysis chain
559 TChain * analysisChain = new TChain(fESDTreeName) ;
560 analysisChain = tagAna->QueryTags(runCuts, evtCuts);
562 // Process the events
563 rv = ProcessChain(analysisChain) ;
568 //______________________________________________________________________
569 Bool_t AliAnalysisGoodies::ProcessEsdXmlCollection(const char * xmlFile) const
571 // process the events in a xml ESD collection with an Analysis Task
572 // usage ProcessLocalEsdFile(xmlFile)
573 // xmlFile: is the local xml file with the ESD collection ( ex: esdCollection.xml)
577 printf("*** Process ***\n");
578 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
579 printf("*** Coll = |%s| \n",xmlFile);
583 TGridCollection * collection = TAlienCollection::Open(xmlFile) ;
585 AliError(Form("%s not found", xmlFile)) ;
589 TGridResult* result = collection->GetGridResult("");
590 TList* analysisfilelist = result->GetFileInfoList();
592 // Makes the ESD chain
593 printf("*** Getting the Chain ***\n");
594 TChain* analysisChain = new TChain(fESDTreeName);
595 analysisChain->AddFileInfoList(analysisfilelist);
597 // Process the events
598 rv = ProcessChain(analysisChain) ;
606 //______________________________________________________________________
607 Bool_t AliAnalysisGoodies::ProcessTagXmlCollection(const char * xmlFile, AliRunTagCuts *runCuts, AliEventTagCuts * evtCuts) const
609 // process the events in a xml ESD collection with an Analysis Task
610 // usage ProcessLocalEsdFile(xmlFile)
611 // xmlFile: is the local xml file with the tag collection ( ex: tagCollection.xml)
615 if ( !evtCuts && !runCuts ) {
616 AliError("No Tag cuts provided") ;
620 printf("*** Process ***\n");
621 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
622 printf("*** Coll = |%s| \n",xmlFile);
624 // check if file is local or alien
625 if ( gSystem->AccessPathName(xmlFile) )
626 TGrid::Connect("alien://");
630 TGridCollection * collection = TAlienCollection::Open(xmlFile) ;
632 AliError(Form("%s not found", xmlFile)) ;
636 TGridResult* result = collection->GetGridResult("");
637 AliTagAnalysis * tagAna = new AliTagAnalysis();
638 tagAna->ChainGridTags(result);
640 // Query the tag file and make the analysis chain
641 TChain * analysisChain = new TChain(fESDTreeName) ;
642 analysisChain = tagAna->QueryTags(runCuts, evtCuts);
644 // Process the events
645 rv = ProcessChain(analysisChain) ;
653 //______________________________________________________________________
654 Bool_t AliAnalysisGoodies::ProcessTagXmlCollection(const char * xmlFile, const char * runCuts, const char * evtCuts) const
656 // process the events in a xml ESD collection with an Analysis Task
657 // usage ProcessLocalEsdFile(xmlFile)
658 // xmlFile: is the local xml file with the tag collection ( ex: tagCollection.xml)
662 if ( !evtCuts && !runCuts ) {
663 AliError("No Tag cuts provided") ;
667 printf("*** Process ***\n");
668 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
669 printf("*** Coll = |%s| \n",xmlFile);
673 // check if file is local or alien
674 if ( gSystem->AccessPathName(xmlFile) )
675 TGrid::Connect("alien://");
677 TGridCollection * collection = TAlienCollection::Open(xmlFile) ;
679 AliError(Form("%s not found", xmlFile)) ;
683 TGridResult* result = collection->GetGridResult("");
684 AliTagAnalysis * tagAna = new AliTagAnalysis();
685 tagAna->ChainGridTags(result);
687 // Query the tag file and make the analysis chain
688 TChain * analysisChain = new TChain(fESDTreeName) ;
689 analysisChain = tagAna->QueryTags(runCuts, evtCuts);
691 // Process the events
692 rv = ProcessChain(analysisChain) ;
700 //______________________________________________________________________
701 Bool_t AliAnalysisGoodies::Register( const char * lfndir, const char * pfndir, const char * file)
703 // register files already stored in a MSS into the AliEn catalog
704 // usage: Register(lfndir, pfndir, pfnFileName)
705 // lfndir : AliEn directory ( ex: /alice/data/2006/LHC06c/PHOS_TestBeam/ )
706 // pfndir : MSS directory ( ex: /castor/cern.ch/alice/testbeam/phos/2006 )
707 // file : text file with a list of the file names to be registered
715 AliError(Form("Cannot open file %s\n", file)) ;
719 TGrid::Connect("alien://");
721 char fileName[1024] ;
729 sprintf(lfn, "%s/%s", lfndir, fileName) ;
733 sprintf(pfn, "castor://Alice::CERN::Castor2/%s/%s", pfndir, fileName) ;
735 printf("Register %s as %s\n", pfn, lfn) ;
737 gGrid->Register(lfn, pfn) ;
746 //______________________________________________________________________
747 void AliAnalysisGoodies::SetTasks(Int_t nb, AliAnalysisTask ** taskList, TClass ** inputType, TClass ** outputType)
749 // define a task with its output and input type
753 fTaskList = taskList ;
754 fTaskInType = inputType ;
755 fTaskOuType = outputType ;