]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGGA/CaloTasks/macros/anaCaloFilter.C
Merge branch 'feature-movesplit'
[u/mrichter/AliRoot.git] / PWGGA / CaloTasks / macros / anaCaloFilter.C
1 /* $Id:  $ */
2 //--------------------------------------------------
3 // Example macro to do Calorimeters filtering
4 // copy ESDs into AODs
5 //
6 // Pay attention to the options and definitions
7 // set in the lines below
8 //
9 //  Author : Gustavo Conesa Balbastre (INFN-LNF)
10 //
11 //-------------------------------------------------
12 enum anaModes {mLocal, mLocalCAF,mPROOF,mGRID};
13 //mLocal: Analyze locally files in your computer
14 //mLocalCAF: Analyze locally CAF files
15 //mPROOF: Analyze CAF files with PROOF
16
17 //---------------------------------------------------------------------------
18 //Settings to read locally several files, only for "mLocal" mode
19 //The different values are default, they can be set with environmental 
20 //variables: INDIR, PATTERN, NFILES, respectivelly
21 char * kInDir = "/user/data/files/"; 
22 char * kPattern = ""; // Data are in files kInDir/kPattern+i 
23 Int_t kFile = 1; // Number of files
24 //---------------------------------------------------------------------------
25 //Collection file for grid analysis
26 char * kXML = "collection.xml";
27 //---------------------------------------------------------------------------
28
29 const TString kInputData = "ESD"; //ESD, AOD, MC
30 TString kTreeName = "esdTree";
31 Bool_t kUsePhysSel = kTRUE;
32
33 void anaCaloFilter(Int_t mode=mLocal)
34 {
35   // Main
36   char cmd[200] ; 
37   sprintf(cmd, ".! rm -rf AliAOD.root") ; 
38   gROOT->ProcessLine(cmd) ; 
39   //--------------------------------------------------------------------
40   // Load analysis libraries
41   // Look at the method below, 
42   // change whatever you need for your analysis case
43   // ------------------------------------------------------------------
44   LoadLibraries(mode) ;
45   
46   //-------------------------------------------------------------------------------------------------
47   //Create chain from ESD and from cross sections files, look below for options.
48   //-------------------------------------------------------------------------------------------------
49   if(kInputData == "ESD") kTreeName = "esdTree" ;
50   else if(kInputData == "AOD") kTreeName = "aodTree" ;
51   else {
52     cout<<"Wrong  data type "<<kInputData<<endl;
53     break;
54   }
55   
56   TChain *chain       = new TChain(kTreeName) ;
57   CreateChain(mode, chain);  
58   
59   if(chain){
60     AliLog::SetGlobalLogLevel(AliLog::kError);//Minimum prints on screen
61     
62     //--------------------------------------
63     // Make the analysis manager
64     //-------------------------------------
65     AliAnalysisManager *mgr  = new AliAnalysisManager("Manager", "Manager");
66     
67     // AOD output handler
68     AliAODHandler* aodoutHandler   = new AliAODHandler();
69     aodoutHandler->SetOutputFileName("AliAOD.root");
70     ////aodoutHandler->SetCreateNonStandardAOD();
71     mgr->SetOutputEventHandler(aodoutHandler);
72     
73     //input
74     if(kInputData == "ESD")
75     {
76       // ESD handler
77       AliESDInputHandler *esdHandler = new AliESDInputHandler();
78       mgr->SetInputEventHandler(esdHandler);
79       esdHandler->SetReadFriends(kFALSE);
80       cout<<"ESD handler "<<mgr->GetInputEventHandler()<<endl;
81     }
82     if(kInputData == "AOD")
83     {
84       // AOD handler
85       AliAODInputHandler *aodHandler = new AliAODInputHandler();
86       mgr->SetInputEventHandler(aodHandler);
87       cout<<"AOD handler "<<mgr->GetInputEventHandler()<<endl;
88       
89     }
90     
91     // mgr->SetDebugLevel(1);
92     
93     //-------------------------------------------------------------------------
94     //Define task, put here any other task that you want to use.
95     //-------------------------------------------------------------------------    
96     // ESD physics selection task
97     if(kInputData == "ESD" && kUsePhysSel)
98     {
99       gROOT->LoadMacro("$ALICE_ROOT/OADB/macros/AddTaskPhysicsSelection.C");
100       AliPhysicsSelectionTask* physSelTask = AddTaskPhysicsSelection();
101     }
102     
103     gROOT->LoadMacro("AddTaskCaloFilter.C");
104     AliAnalysisTaskCaloFilter* filter = AddTaskCaloFilter();
105             
106     //AliAnalysisTaskCaloFilter * filter = new AliAnalysisTaskCaloFilter();
107     //filter->SetConfigFileName("ConfigCaloFilter.C");
108     //mgr->AddTask(filter);
109       
110     // Create containers for input/output
111     AliAnalysisDataContainer *cinput1    = mgr->GetCommonInputContainer();
112     AliAnalysisDataContainer *coutput1   = mgr->GetCommonOutputContainer();
113     AliAnalysisDataContainer *coutntuple = mgr->CreateContainer("EventNtuple", TNtuple::Class(), 
114                                                                 AliAnalysisManager::kOutputContainer, "eventselection.root");
115     
116     mgr->ConnectInput  (filter, 0, cinput1);
117     mgr->ConnectOutput (filter, 0, coutput1 );
118     mgr->ConnectOutput (filter, 1, coutntuple );
119     
120     //-----------------------
121     // Run the analysis
122     //-----------------------    
123     TString smode = "";
124     if (mode==mLocal || mode == mLocalCAF) 
125       smode = "local";
126     else if (mode==mPROOF) 
127       smode = "proof";
128     else if (mode==mGRID) 
129       smode = "local";
130     
131     mgr->InitAnalysis();
132     mgr->PrintStatus();
133     mgr->StartAnalysis(smode.Data(),chain);
134     
135     cout <<" Analysis ended sucessfully "<< endl ;
136     
137   }
138   else cout << "Chain was not produced ! "<<endl;
139   
140   //sprintf(cmd, ".! rm -rf CorrectionFiles") ;
141   
142 }
143
144 void  LoadLibraries(const anaModes mode) {
145   
146   //--------------------------------------
147   // Load the needed libraries most of them already loaded by aliroot
148   //--------------------------------------
149   gSystem->Load("libTree");
150   gSystem->Load("libGeom");
151   gSystem->Load("libVMC");
152   gSystem->Load("libXMLIO");
153   gSystem->Load("libMatrix");
154   gSystem->Load("libPhysics");
155
156   //----------------------------------------------------------
157   // >>>>>>>>>>> Local mode <<<<<<<<<<<<<< 
158   //----------------------------------------------------------
159   if (mode==mLocal || mode == mLocalCAF || mode == mGRID) {
160     //--------------------------------------------------------
161     // If you want to use already compiled libraries 
162     // in the aliroot distribution
163     //--------------------------------------------------------
164
165     gSystem->Load("libSTEERBase");
166     gSystem->Load("libESD");
167     gSystem->Load("libAOD");
168     gSystem->Load("libANALYSIS");
169     gSystem->Load("libANALYSISalice");
170     gSystem->Load("libANALYSISalice");
171     gSystem->Load("libCORRFW");
172     gSystem->Load("libPHOSUtils");
173     gSystem->Load("libEMCALUtils");
174     gSystem->Load("libPWGGACaloTasks");
175        
176     //--------------------------------------------------------
177     //If you want to use root and par files from aliroot
178     //--------------------------------------------------------  
179     /*     
180            SetupPar("STEERBase");
181            SetupPar("ESD");
182            SetupPar("AOD");
183            SetupPar("ANALYSIS");
184            SetupPar("ANALYSISalice");  
185            SetupPar("PHOSUtils");
186            SetupPar("EMCALUtils");
187            //Create Geometry
188            TGeoManager::Import("geometry.root") ; //need file "geometry.root" in local dir!!!!
189            SetupPar("PWGGACaloTasks");
190 */
191   }
192
193   //---------------------------------------------------------
194   // <<<<<<<<<< PROOF mode >>>>>>>>>>>>
195   //---------------------------------------------------------
196   else if (mode==mPROOF) {
197     //
198     // Connect to proof
199     // Put appropriate username here
200     // TProof::Reset("proof://mgheata@lxb6046.cern.ch"); 
201     TProof::Open("proof://mgheata@lxb6046.cern.ch");
202     
203     //    gProof->ClearPackages();
204     //    gProof->ClearPackage("ESD");
205     //    gProof->ClearPackage("AOD");
206     //    gProof->ClearPackage("ANALYSIS");   
207      
208     // Enable the STEERBase Package
209     gProof->UploadPackage("STEERBase.par");
210     gProof->EnablePackage("STEERBase");
211     // Enable the ESD Package
212     gProof->UploadPackage("ESD.par");
213     gProof->EnablePackage("ESD");
214     // Enable the AOD Package
215     gProof->UploadPackage("AOD.par");
216     gProof->EnablePackage("AOD");
217     // Enable the Analysis Package
218     gProof->UploadPackage("ANALYSIS.par");
219     gProof->EnablePackage("ANALYSIS");
220     // Enable the PHOS geometry Package
221     //gProof->UploadPackage("PHOSUtils.par");
222     //gProof->EnablePackage("PHOSUtils");
223     gProof->ShowEnabledPackages();
224   }  
225   
226 }
227
228 void SetupPar(char* pararchivename)
229 {
230   //Load par files, create analysis libraries
231   //For testing, if par file already decompressed and modified
232   //classes then do not decompress.
233  
234   TString cdir(Form("%s", gSystem->WorkingDirectory() )) ; 
235   TString parpar(Form("%s.par", pararchivename)) ; 
236   if ( gSystem->AccessPathName(parpar.Data()) ) {
237     gSystem->ChangeDirectory(gSystem->Getenv("ALICE_ROOT")) ;
238     TString processline(Form(".! make %s", parpar.Data())) ; 
239     gROOT->ProcessLine(processline.Data()) ;
240     gSystem->ChangeDirectory(cdir) ; 
241     processline = Form(".! mv $ALICE_ROOT/%s .", parpar.Data()) ;
242     gROOT->ProcessLine(processline.Data()) ;
243   } 
244   if ( gSystem->AccessPathName(pararchivename) ) {  
245     TString processline = Form(".! tar xvzf %s",parpar.Data()) ;
246     gROOT->ProcessLine(processline.Data());
247   }
248   
249   TString ocwd = gSystem->WorkingDirectory();
250   gSystem->ChangeDirectory(pararchivename);
251   
252   // check for BUILD.sh and execute
253   if (!gSystem->AccessPathName("PROOF-INF/BUILD.sh")) {
254     printf("*******************************\n");
255     printf("*** Building PAR archive    ***\n");
256     cout<<pararchivename<<endl;
257     printf("*******************************\n");
258     
259     if (gSystem->Exec("PROOF-INF/BUILD.sh")) {
260       Error("runProcess","Cannot Build the PAR Archive! - Abort!");
261       return -1;
262     }
263   }
264   // check for SETUP.C and execute
265   if (!gSystem->AccessPathName("PROOF-INF/SETUP.C")) {
266     printf("*******************************\n");
267     printf("*** Setup PAR archive       ***\n");
268     cout<<pararchivename<<endl;
269     printf("*******************************\n");
270     gROOT->Macro("PROOF-INF/SETUP.C");
271   }
272   
273   gSystem->ChangeDirectory(ocwd.Data());
274   printf("Current dir: %s\n", ocwd.Data());
275 }
276
277
278
279 void CreateChain(const anaModes mode, TChain * chain){
280   //Fills chain with data
281   TString ocwd = gSystem->WorkingDirectory();
282   
283   //-----------------------------------------------------------
284   //Analysis of CAF data locally and with PROOF
285   //-----------------------------------------------------------
286   if(mode ==mPROOF || mode ==mLocalCAF){
287     // Chain from CAF
288     gROOT->LoadMacro("$ALICE_ROOT/PWG0/CreateESDChain.C");
289     // The second parameter is the number of input files in the chain
290     chain = CreateESDChain("ESD12001.txt", 5);  
291   }
292   
293   //---------------------------------------
294   //Local files analysis
295   //---------------------------------------
296   else if(mode == mLocal){    
297     //If you want to add several ESD files sitting in a common directory INDIR
298     //Specify as environmental variables the directory (INDIR), the number of files 
299     //to analyze (NFILES) and the pattern name of the directories with files (PATTERN)
300
301     if(gSystem->Getenv("INDIR"))  
302       kInDir = gSystem->Getenv("INDIR") ; 
303     else cout<<"INDIR not set, use default: "<<kInDir<<endl;    
304     
305     if(gSystem->Getenv("PATTERN"))   
306       kPattern = gSystem->Getenv("PATTERN") ; 
307     else  cout<<"PATTERN not set, use default: "<<kPattern<<endl;
308     
309     if(gSystem->Getenv("NFILES"))
310       kFile = atoi(gSystem->Getenv("NFILES")) ;
311     else cout<<"NFILES not set, use default: "<<kFile<<endl;
312     
313     //Check if env variables are set and are correct
314     if ( kInDir  && kFile) {
315       printf("Get %d files from directory %s\n",kFile,kInDir);
316       if ( ! gSystem->cd(kInDir) ) {//check if ESDs directory exist
317         printf("%s does not exist\n", kInDir) ;
318         return ;
319       }
320
321   
322       cout<<"INDIR   : "<<kInDir<<endl;
323       cout<<"NFILES  : "<<kFile<<endl;
324       cout<<"PATTERN : " <<kPattern<<endl;
325
326       TString datafile="";
327       if(kInputData == "ESD") datafile = "AliESDs.root" ;
328       else if(kInputData == "AOD") datafile = "AliAOD.root" ;
329       else if(kInputData == "MC")  datafile = "galice.root" ;
330       
331       //Loop on ESD files, add them to chain
332       Int_t event =0;
333       Int_t skipped=0 ; 
334       char file[120] ;
335       
336       for (event = 0 ; event < kFile ; event++) {
337         sprintf(file, "%s/%s%d/%s", kInDir,kPattern,event,datafile.Data()) ; 
338         TFile * fESD = 0 ; 
339         //Check if file exists and add it, if not skip it
340         if ( fESD = TFile::Open(file)) {
341           if ( fESD->Get(kTreeName) ) { 
342             printf("++++ Adding %s\n", file) ;
343             chain->AddFile(file);
344           }
345         }
346         else { 
347           printf("---- Skipping %s\n", file) ;
348           skipped++ ;
349         }
350       }
351       printf("number of entries # %lld, skipped %d\n", chain->GetEntries(), skipped*100) ;      
352     }
353     else {
354       TString input = "AliESDs.root" ;
355       cout<<">>>>>> No list added, take a single file <<<<<<<<< "<<input<<endl;
356       chain->AddFile(input);
357     }
358     
359   }// local files analysis
360   
361   //------------------------------
362   //GRID xml files
363   //-----------------------------
364   else if(mode == mGRID){
365     //Get colection file. It is specified by the environmental
366     //variable XML
367
368     if(gSystem->Getenv("XML") )
369       kXML = gSystem->Getenv("XML");
370     else
371       sprintf(kXML, "collection.xml") ; 
372     
373     if (!TFile::Open(kXML)) {
374       printf("No collection file with name -- %s -- was found\n",kXML);
375       return ;
376     }
377     else cout<<"XML file "<<kXML<<endl;
378
379     //Load necessary libraries and connect to the GRID
380     gSystem->Load("libNetx") ;
381     gSystem->Load("libRAliEn");
382     TGrid::Connect("alien://") ;
383
384     //Feed Grid with collection file
385     //TGridCollection * collection =  (TGridCollection*)gROOT->ProcessLine(Form("TAlienCollection::Open(\"%s\", 0)", kXML));
386     TGridCollection * collection = (TGridCollection*) TAlienCollection::Open(kXML);
387     if (! collection) {
388       AliError(Form("%s not found", kXML)) ; 
389       return kFALSE ; 
390     }
391     TGridResult* result = collection->GetGridResult("",0 ,0);
392    
393     // Makes the ESD chain 
394     printf("*** Getting the Chain       ***\n");
395     for (Int_t index = 0; index < result->GetEntries(); index++) {
396       TString alienURL = result->GetKey(index, "turl") ; 
397       cout << "================== " << alienURL << endl ; 
398       chain->Add(alienURL) ; 
399     }
400   }// xml analysis
401   
402   gSystem->ChangeDirectory(ocwd.Data());
403 }
404