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
102 AliXMLCollection collectionIn(collectionNameIn) ;
104 AliXMLCollection * collectionOu = new AliXMLCollection() ;
105 TString collectionNameOu(collectionIn.GetCollectionName()) ;
106 collectionNameOu.Append("Local") ;
107 collectionOu->SetCollectionName(collectionNameOu) ;
108 collectionOu->WriteHeader() ;
112 const char* ocwd = gSystem->WorkingDirectory();
114 while ( collectionIn.Next() ) {
115 gSystem->ChangeDirectory(localDir) ;
116 TString fileTURL = collectionIn.GetTURL() ;
118 TString tempo(fileTURL) ;
119 tempo.Remove(tempo.Last('/'), tempo.Length()) ;
120 TString evtsNumber = tempo(tempo.Last('/')+1, tempo.Length())+"/";
121 tempo.Remove(tempo.Last('/'), tempo.Length()) ;
122 TString runNumber = tempo(tempo.Last('/')+1, tempo.Length())+"/" ;
123 TString dir = localDir + runNumber ;
124 gSystem->MakeDirectory(dir) ;
125 gSystem->ChangeDirectory(dir) ;
126 dir += evtsNumber + "/";
127 gSystem->MakeDirectory(dir) ;
128 gSystem->ChangeDirectory(dir) ;
129 dir += collectionIn.GetFileName() ;
130 TEventList * list = collectionIn.GetEventList() ;
132 collectionOu->WriteBody(collectionIn.GetCurrentEvent(), collectionIn.GetGUID(), collectionIn.GetLFN(), collectionIn.GetTURL(), list) ;
134 printf("Copying %s to %s\n", fileTURL.Data(), dir.Data()) ;
135 merger.Cp(fileTURL, dir) ;
137 collectionOu->Export() ;
138 gSystem->ChangeDirectory(ocwd) ;
146 //______________________________________________________________________
147 const Bool_t AliAnalysisGoodies::Make(AliRunTagCuts *runCuts, AliEventTagCuts *evtCuts, const char * in, const char * out) const
149 // makes esd collection from tags
150 // usage Make(tags, esds)
151 // tags: is either a tag root file or an xml tag collection
152 // esds: is an esd collection
156 if ( !evtCuts && !runCuts ) {
157 AliError("No Tag cuts provided") ;
162 if ( file.Contains(".root") )
163 rv = MakeEsdCollectionFromTagFile(runCuts, evtCuts, file.Data(), out) ;
164 else if ( file.Contains(".xml") )
165 rv = MakeEsdCollectionFromTagCollection(runCuts, evtCuts, file.Data(), out) ;
167 AliError(Form("%s is not a valid file format", in)) ;
174 //______________________________________________________________________
175 const Bool_t AliAnalysisGoodies::MakeEsdCollectionFromTagFile(AliRunTagCuts *runCuts, AliEventTagCuts *evtCuts, const char * in, const char * out) const
177 // Makes an esd collection from a root tag file
179 // Open the file collection
180 printf("*** Create Collection ***\n");
181 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
182 printf("*** file = |%s| \n",in);
184 AliTagAnalysis * tagAna = new AliTagAnalysis();
185 rv = tagAna->AddTagsFile(in);
189 tagAna->CreateXMLCollection(out, runCuts, evtCuts) ;
195 //______________________________________________________________________
196 const Bool_t AliAnalysisGoodies::MakeEsdCollectionFromTagCollection(AliRunTagCuts * runCuts, AliEventTagCuts * evtCuts, const char * in, const char * out) const
198 // Makes an esd collection from a xml tag collection
200 // Open the file collection
201 printf("*** Create Collection ***\n");
202 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
203 printf("*** Coll = |%s| \n",in);
205 TAlienCollection * collection = TAlienCollection::Open(in);
206 TGridResult* result = collection->GetGridResult("");
207 AliTagAnalysis * tagAna = new AliTagAnalysis();
208 tagAna->ChainGridTags(result);
210 tagAna->CreateXMLCollection(out, runCuts, evtCuts) ;
215 //______________________________________________________________________
216 const Bool_t AliAnalysisGoodies::MakeEsdCollectionFromTagCollection(const char * runCuts, const char * evtCuts, const char * in, const char * out) const
218 // Makes an esd collection from a xml tag collection
220 // Open the file collection
221 printf("*** Create Collection ***\n");
222 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
223 printf("*** Coll = |%s| \n",in);
225 TAlienCollection * collection = TAlienCollection::Open(in);
226 TGridResult* result = collection->GetGridResult("");
227 AliTagAnalysis * tagAna = new AliTagAnalysis();
228 tagAna->ChainGridTags(result);
230 tagAna->CreateXMLCollection(out, runCuts, evtCuts) ;
235 //______________________________________________________________________
236 const Bool_t AliAnalysisGoodies::Merge(const char * collectionFile, const char * subFile, const char * outFile)
238 // merges files listed in a xml collection
239 // usage Merge(collection, outputFile))
240 // collection: is a xml collection
244 if ( strstr(collectionFile, ".xml") == 0 ) {
245 AliError("Input collection file must be an \".xml\" file\n") ;
251 // Open the file collection
252 printf("*** Create Collection ***\n");
253 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
254 printf("*** Coll = |%s| \n",collectionFile);
256 TAlienCollection * collection = TAlienCollection::Open(collectionFile);
257 TGridResult* result = collection->GetGridResult("");
263 TString tempo(collectionFile) ;
265 tempo.ReplaceAll(".xml", subFile) ;
267 tempo.ReplaceAll(".xml", "_Merged.root") ;
268 outFile = tempo.Data() ;
270 merger.OutputFile(outFile) ;
272 while ( (turl = result->GetKey(index, "turl")) ) {
275 sprintf(file, "%s#%s", turl, subFile) ;
277 sprintf(file, "%s", turl) ;
279 printf("%s\n", file) ;
280 merger.AddFile(file) ;
287 AliInfo(Form("Files merged into %s\n", outFile)) ;
295 //______________________________________________________________________
296 const Bool_t AliAnalysisGoodies::Process(TChain * chain)
298 // process events starting from a chain of esd Trees
303 rv = ProcessChain(chain) ;
311 //______________________________________________________________________
312 const Bool_t AliAnalysisGoodies::Process(const char * inFile)
314 // process the events with an Analysis Task
315 // usage Process(esdFile)
316 // esdFile: is of the form opt?file_lfn
318 AliRunTagCuts * runCuts = 0x0 ;
319 AliEventTagCuts * evtCuts = 0x0 ;
321 rv = Process(inFile, runCuts, evtCuts) ;
326 //______________________________________________________________________
327 const Bool_t AliAnalysisGoodies::Process(const char * inFile, AliRunTagCuts *runCuts, AliEventTagCuts * evtCuts )
329 // process the events with an Analysis Task
330 // usage Process(esdFile, runtagCuts, evtTagCuts)
331 // esdFile: is of the form opt?file_lfn
337 TString file(inFile) ;
338 if ( file.Contains("esd?") && file.Contains(".root") ) {
339 file.ReplaceAll("esd?", "") ;
340 rv = ProcessEsdFile(file.Data()) ;
342 } else if ( file.Contains("esd?") && file.Contains(".xml") ) {
343 file.ReplaceAll("esd?", "") ;
344 rv = ProcessEsdXmlCollection(file.Data()) ;
346 } else if (file.Contains("tag?") && file.Contains(".root") ) {
347 file.ReplaceAll("tag?", "") ;
348 rv = ProcessTagFile(file.Data(), runCuts, evtCuts) ;
350 } else if (file.Contains("tag?") && file.Contains(".xml") ) {
351 file.ReplaceAll("tag?", "") ;
352 rv = ProcessTagXmlCollection(file.Data(), runCuts, evtCuts) ;
355 AliError(Form("%s is not a valid file format", inFile)) ;
365 //______________________________________________________________________
366 const Bool_t AliAnalysisGoodies::Process(const char * inFile, const char * runCuts, const char * evtCuts)
368 // process the events with an Analysis Task
369 // usage Process(esdFile, runtagCuts, evtTagCuts)
370 // esdFile: is of the form opt?file_lfn
376 TString file(inFile) ;
377 if ( file.Contains("esd?") && file.Contains(".root") ) {
378 file.ReplaceAll("esd?", "") ;
379 rv = ProcessEsdFile(file.Data()) ;
381 } else if ( file.Contains("esd?") && file.Contains(".xml") ) {
382 file.ReplaceAll("esd?", "") ;
383 rv = ProcessEsdXmlCollection(file.Data()) ;
385 } else if (file.Contains("tag?") && file.Contains(".root") ) {
386 file.ReplaceAll("tag?", "") ;
387 rv = ProcessTagFile(file.Data(), runCuts, evtCuts) ;
389 } else if (file.Contains("tag?") && file.Contains(".xml") ) {
390 file.ReplaceAll("tag?", "") ;
391 rv = ProcessTagXmlCollection(file.Data(), runCuts, evtCuts) ;
394 AliError(Form("%s is not a valid file format", inFile)) ;
404 //______________________________________________________________________
405 const Bool_t AliAnalysisGoodies::ProcessChain(TChain * chain) const
412 AliError("No tasks defined") ;
416 // Make the analysis manager
417 AliAnalysisManager * mgr = new AliAnalysisManager() ;
420 // The top input must be common to all top tasks
421 TClass * classIn = fTaskInType[0] ;
422 AliAnalysisDataContainer * taskInput = mgr->CreateContainer("Input Container", classIn, AliAnalysisManager::kInputContainer) ;
424 for (index = 0; index < fnumberOfTasks; index++) {
425 AliAnalysisTask * task = fTaskList[index] ;
428 // Create containers for input/output
429 TClass * classOu = fTaskOuType[index] ;
430 AliAnalysisDataContainer * taskOutput = mgr->CreateContainer("Output Container", classOu, AliAnalysisManager::kOutputContainer) ;
431 mgr->ConnectInput (task, 0, taskInput);
432 mgr->ConnectOutput(task, 0, taskOutput);
436 taskInput->SetData(chain);
438 if (mgr->InitAnalysis()) {
447 //______________________________________________________________________
448 const Bool_t AliAnalysisGoodies::ProcessEsdFile(const char * esdFile) const
450 // process the events in a single ESD file with an Analysis Task
451 // usage ProcessLocalEsdFile(esdFile)
452 // esdFile: is the root file (local or in alien) with the ESD Tree ( ex: AliESDs.root)
456 printf("*** Process ***\n");
457 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
458 printf("*** Coll = |%s| \n",esdFile);
460 // Makes the ESD chain
461 printf("*** Getting the Chain ***\n");
462 TChain* analysisChain = new TChain(fESDTreeName) ;
463 analysisChain->AddFile(esdFile);
465 // Process the events
466 rv = ProcessChain(analysisChain) ;
471 //______________________________________________________________________
472 const Bool_t AliAnalysisGoodies::ProcessTagFile(const char * tagFile, AliRunTagCuts *runCuts, AliEventTagCuts *evtCuts) const
474 // process the events in a single Tag file with an Analysis Task
475 // usage ProcessLocalEsdFile(tagFile)
476 // tagFile: is the root file (local or in alien) with the Tag Tree (ex: Run102.Event0_100.ESD.tag.root)
480 if ( !evtCuts && !runCuts ) {
481 AliError("No Tag cuts provided") ;
485 printf("*** Process ***\n");
486 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
487 printf("*** Coll = |%s| \n",tagFile);
489 AliTagAnalysis * tagAna = new AliTagAnalysis();
490 rv = tagAna->AddTagsFile(tagFile);
494 // Query the tag file and make the analysis chain
495 TChain * analysisChain = new TChain(fESDTreeName) ;
496 analysisChain = tagAna->QueryTags(runCuts, evtCuts);
498 // Process the events
499 rv = ProcessChain(analysisChain) ;
504 //______________________________________________________________________
505 const Bool_t AliAnalysisGoodies::ProcessTagFile(const char * tagFile, const char * runCuts, const char * evtCuts) const
507 // process the events in a single Tag file with an Analysis Task
508 // usage ProcessLocalEsdFile(tagFile)
509 // tagFile: is the root file (local or in alien) with the Tag Tree (ex: Run102.Event0_100.ESD.tag.root)
513 if ( !evtCuts && !runCuts ) {
514 AliError("No Tag cuts provided") ;
518 printf("*** Process ***\n");
519 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
520 printf("*** Coll = |%s| \n",tagFile);
522 AliTagAnalysis * tagAna = new AliTagAnalysis();
523 rv = tagAna->AddTagsFile(tagFile);
527 // Query the tag file and make the analysis chain
528 TChain * analysisChain = new TChain(fESDTreeName) ;
529 analysisChain = tagAna->QueryTags(runCuts, evtCuts);
531 // Process the events
532 rv = ProcessChain(analysisChain) ;
537 //______________________________________________________________________
538 const Bool_t AliAnalysisGoodies::ProcessEsdXmlCollection(const char * xmlFile) const
540 // process the events in a xml ESD collection with an Analysis Task
541 // usage ProcessLocalEsdFile(xmlFile)
542 // xmlFile: is the local xml file with the ESD collection ( ex: esdCollection.xml)
546 printf("*** Process ***\n");
547 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
548 printf("*** Coll = |%s| \n",xmlFile);
550 TAlienCollection * collection = TAlienCollection::Open(xmlFile) ;
552 AliError(Form("%s not found", xmlFile)) ;
556 TGridResult* result = collection->GetGridResult("");
557 TList* analysisfilelist = result->GetFileInfoList();
559 // Makes the ESD chain
560 printf("*** Getting the Chain ***\n");
561 TChain* analysisChain = new TChain(fESDTreeName);
562 analysisChain->AddFileInfoList(analysisfilelist);
564 // Process the events
565 rv = ProcessChain(analysisChain) ;
570 //______________________________________________________________________
571 const Bool_t AliAnalysisGoodies::ProcessTagXmlCollection(const char * xmlFile, AliRunTagCuts *runCuts, AliEventTagCuts * evtCuts) const
573 // process the events in a xml ESD collection with an Analysis Task
574 // usage ProcessLocalEsdFile(xmlFile)
575 // xmlFile: is the local xml file with the tag collection ( ex: tagCollection.xml)
579 if ( !evtCuts && !runCuts ) {
580 AliError("No Tag cuts provided") ;
584 printf("*** Process ***\n");
585 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
586 printf("*** Coll = |%s| \n",xmlFile);
588 // check if file is local or alien
589 if ( gSystem->AccessPathName(xmlFile) )
590 TGrid::Connect("alien://");
592 TAlienCollection * collection = TAlienCollection::Open(xmlFile) ;
594 AliError(Form("%s not found", xmlFile)) ;
598 TGridResult* result = collection->GetGridResult("");
599 AliTagAnalysis * tagAna = new AliTagAnalysis();
600 tagAna->ChainGridTags(result);
602 // Query the tag file and make the analysis chain
603 TChain * analysisChain = new TChain(fESDTreeName) ;
604 analysisChain = tagAna->QueryTags(runCuts, evtCuts);
606 // Process the events
607 rv = ProcessChain(analysisChain) ;
612 //______________________________________________________________________
613 const Bool_t AliAnalysisGoodies::ProcessTagXmlCollection(const char * xmlFile, const char * runCuts, const char * evtCuts) const
615 // process the events in a xml ESD collection with an Analysis Task
616 // usage ProcessLocalEsdFile(xmlFile)
617 // xmlFile: is the local xml file with the tag collection ( ex: tagCollection.xml)
621 if ( !evtCuts && !runCuts ) {
622 AliError("No Tag cuts provided") ;
626 printf("*** Process ***\n");
627 printf("*** Wk-Dir = |%s| \n",gSystem->WorkingDirectory());
628 printf("*** Coll = |%s| \n",xmlFile);
630 // check if file is local or alien
631 if ( gSystem->AccessPathName(xmlFile) )
632 TGrid::Connect("alien://");
634 TAlienCollection * collection = TAlienCollection::Open(xmlFile) ;
636 AliError(Form("%s not found", xmlFile)) ;
640 TGridResult* result = collection->GetGridResult("");
641 AliTagAnalysis * tagAna = new AliTagAnalysis();
642 tagAna->ChainGridTags(result);
644 // Query the tag file and make the analysis chain
645 TChain * analysisChain = new TChain(fESDTreeName) ;
646 analysisChain = tagAna->QueryTags(runCuts, evtCuts);
648 // Process the events
649 rv = ProcessChain(analysisChain) ;
654 //______________________________________________________________________
655 const Bool_t AliAnalysisGoodies::Register( const char * lfndir, const char * pfndir, const char * file)
657 // register files already stored in a MSS into the AliEn catalog
658 // usage: Register(lfndir, pfndir, pfnFileName)
659 // lfndir : AliEn directory ( ex: /alice/data/2006/LHC06c/PHOS_TestBeam/ )
660 // pfndir : MSS directory ( ex: /castor/cern.ch/alice/testbeam/phos/2006 )
661 // file : text file with a list of the file names to be registered
669 AliError(Form("Cannot open file %s\n", file)) ;
673 TGrid::Connect("alien://");
675 char fileName[1024] ;
683 sprintf(lfn, "%s/%s", lfndir, fileName) ;
687 sprintf(pfn, "castor://Alice::CERN::Castor2/%s/%s", pfndir, fileName) ;
689 printf("Register %s as %s\n", pfn, lfn) ;
691 gGrid->Register(lfn, pfn) ;
700 //______________________________________________________________________
701 void AliAnalysisGoodies::SetTasks(Int_t nb, AliAnalysisTask ** taskList, TClass ** inputType, TClass ** outputType)
703 // define a task with its output and input type
707 fTaskList = taskList ;
708 fTaskInType = inputType ;
709 fTaskOuType = outputType ;