]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG4/macros/anaM.C
Updates from Eulogio:
[u/mrichter/AliRoot.git] / PWG4 / macros / anaM.C
1 /* $Id$ */
2 //--------------------------------------------------
3 // Example macro to do analysis with the 
4 // analysis classes in PWG4PartCorr
5 // Can be executed with Root and AliRoot
6 //
7 // Pay attention to the options and definitions
8 // set in the lines below
9 //
10 //  Author : Gustavo Conesa Balbastre (INFN-LNF)
11 //
12 //-------------------------------------------------
13 enum anaModes {mLocal, mLocalCAF,mPROOF,mGRID};
14 //mLocal: Analyze locally files in your computer
15 //mLocalCAF: Analyze locally CAF files
16 //mPROOF: Analyze CAF files with PROOF
17
18 //---------------------------------------------------------------------------
19 //Settings to read locally several files, only for "mLocal" mode
20 //The different values are default, they can be set with environmental 
21 //variables: INDIR, PATTERN, NFILES, respectivelly
22 //char * kInDir = "/Users/Gustavo/Work/mixed/data"; 
23 char * kPattern = ""; // Data are in files kInDir/kPattern+i 
24 Int_t kFile = 1; // Number of files
25 //---------------------------------------------------------------------------
26 //Collection file for grid analysis
27 char * kXML = "collection.xml";
28 //---------------------------------------------------------------------------
29 //Scale histograms from file. Change to kTRUE when xsection file exists
30 //Put name of file containing xsection 
31 //Put number of events per ESD file
32 //This is an specific case for normalization of Pythia files.
33 const Bool_t kGetXSectionFromFileAndScale = kFALSE ;
34 const char * kXSFileName = "pyxsec.root";
35 //---------------------------------------------------------------------------
36
37 const Bool_t kMC = kFALSE; //With real data kMC = kFALSE
38 const TString kInputData = "AOD"; //ESD, AOD, MC
39 TString kTreeName ;
40
41 void anaM(Int_t mode=mLocal)
42 {
43   // Main
44
45   //--------------------------------------------------------------------
46   // Load analysis libraries
47   // Look at the method below, 
48   // change whatever you need for your analysis case
49   // ------------------------------------------------------------------
50   LoadLibraries(mode) ;
51   
52   //-------------------------------------------------------------------------------------------------
53   //Create chain from ESD and from cross sections files, look below for options.
54   //-------------------------------------------------------------------------------------------------
55   if(kInputData == "ESD") kTreeName = "esdTree" ;
56   else if(kInputData == "AOD") kTreeName = "aodTree" ;
57   else if (kInputData == "MC") kTreeName = "TE" ;
58   else {
59     cout<<"Wrong  data type "<<kInputData<<endl;
60     break;
61   }
62
63   TChain *chain       = new TChain(kTreeName) ;
64   TChain * chainxs = new TChain("Xsection") ;
65   CreateChain(mode, chain, chainxs);  
66
67   if(chain){
68     AliLog::SetGlobalLogLevel(AliLog::kError);//Minimum prints on screen
69     
70     //--------------------------------------
71     // Make the analysis manager
72     //-------------------------------------
73     AliAnalysisManager *mgr  = new AliAnalysisManager("Manager", "Manager");
74     // MC handler
75     if((kMC || kInputData == "MC") && kInputData!="AOD"){
76       AliMCEventHandler* mcHandler = new AliMCEventHandler();
77       mcHandler->SetReadTR(kFALSE);//Do not search TrackRef file
78       mgr->SetMCtruthEventHandler(mcHandler);
79           if( kInputData == "MC") mgr->SetInputEventHandler(NULL);
80     }
81
82     // AOD output handler
83 //      AliAODHandler* aodoutHandler   = new AliAODHandler();
84 //     aodoutHandler->SetOutputFileName("aod.root");
85 //     ////aodoutHandler->SetCreateNonStandardAOD();
86 //     mgr->SetOutputEventHandler(aodoutHandler);
87     
88     //input
89     Int_t maxiterations = 1;
90     AliEventPoolLoop* pool = new AliEventPoolLoop(maxiterations);
91     pool->SetChain(chain);
92     Int_t eventsInPool = 2;
93     AliMultiEventInputHandler *inpHandler = NULL ; 
94     if(kInputData == "ESD"){
95       // ESD handler
96       printf("ESD MultiInput \n");
97       inpHandler = new AliMultiEventInputHandler(eventsInPool, 0);
98     }
99     if(kInputData == "AOD"){
100       // AOD handler
101       inpHandler = new AliMultiEventInputHandler(eventsInPool, 1);                
102     }
103     mgr->SetInputEventHandler(inpHandler);
104     cout<<"Input handler "<<mgr->GetInputEventHandler()<<endl;
105     mgr->SetEventPool(pool);
106     inpHandler->SetEventPool(pool);
107
108       //mgr->SetDebugLevel(10); // For debugging, do not uncomment if you want no messages.
109
110     //-------------------------------------------------------------------------
111     //Define task, put here any other task that you want to use.
112     //-------------------------------------------------------------------------
113     //    AliAnalysisDataContainer *cinput1 = mgr->GetCommonInputContainer();
114     // AliAnalysisDataContainer *coutput1 = mgr->GetCommonOutputContainer();
115
116     //gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskESDFilter.C");
117     //AliAnalysisTaskESDfilter *taskesdfilter = AddTaskESDFilter(kTRUE);
118
119     gROOT->LoadMacro("AddTaskPartCorrM.C");
120    
121     AliAnalysisTaskParticleCorrelationM *taskEMCAL = AddTaskPartCorrM(kInputData,"EMCAL", kFALSE,kFALSE,kFALSE,kTRUE);  
122     mgr->AddTask(taskEMCAL);
123     AliAnalysisTaskParticleCorrelationM *taskPHOS  = AddTaskPartCorrM(kInputData,"PHOS", kFALSE, kFALSE);
124     mgr->AddTask(taskPHOS);
125
126     //gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskCalorimeterQA.C");
127     //gROOT->LoadMacro("AddTaskCalorimeterQA.C");
128     //AliAnalysisTaskParticleCorrelation *taskQA = AddTaskCalorimeterQA(kInputData,kFALSE,kTRUE);
129
130     //gROOT->LoadMacro("$ALICE_ROOT/PWG4/macros/AddTaskomega3pi.C");
131     //AliAnalysisTaskOmegaPi0PiPi * taskomega = AddTaskomega3pi();
132     //mgr->AddTask(taskomega);
133
134
135
136     //-----------------------
137     // Run the analysis
138     //-----------------------    
139     TString smode = "";
140     if (mode==mLocal || mode == mLocalCAF) 
141       smode = "mix";
142     else if (mode==mPROOF) 
143       smode = "proof";
144     else if (mode==mGRID) 
145       smode = "mix";
146     
147     mgr->InitAnalysis();
148     mgr->PrintStatus();
149     mgr->StartAnalysis(smode.Data(),chain,1000);
150
151 cout <<" Analysis ended sucessfully "<< endl ;
152
153   }
154   else cout << "Chain was not produced ! "<<endl;
155   
156 }
157
158 void  LoadLibraries(const anaModes mode) {
159   
160   //--------------------------------------
161   // Load the needed libraries most of them already loaded by aliroot
162   //--------------------------------------
163   gSystem->Load("libTree.so");
164   gSystem->Load("libGeom.so");
165   gSystem->Load("libVMC.so");
166   gSystem->Load("libXMLIO.so");
167   
168   //----------------------------------------------------------
169   // >>>>>>>>>>> Local mode <<<<<<<<<<<<<< 
170   //----------------------------------------------------------
171   if (mode==mLocal || mode == mLocalCAF || mode == mGRID) {
172     //--------------------------------------------------------
173     // If you want to use already compiled libraries 
174     // in the aliroot distribution
175     //--------------------------------------------------------
176     //gSystem->Load("/Users/Gustavo/Work/analysis/STEERBase/libSTEERBase.so");
177     //gSystem->Load("/Users/Gustavo/Work/analysis/ESD/libESD.so");
178     //gSystem->Load("/Users/Gustavo/Work/analysis/AOD/libAOD.so");
179     //gSystem->Load("/Users/Gustavo/Work/analysis/ANALYSIS/libANALYSIS.so");
180     //gSystem->Load("/Users/Gustavo/Work/analysis/ANALYSISalice/libANALYSISalice.so");
181     //gSystem->Load("libPHOSUtils");
182     //gSystem->Load("/Users/Gustavo/Work/analysis/PWG4PartCorrBase/libPWG4PartCorrBase.so");
183     //gSystem->Load("/Users/Gustavo/Work/analysis/PWG4PartCorrDep/libPWG4PartCorrDep.so");
184         
185     gSystem->Load("libSTEERBase.so");
186     gSystem->Load("libESD.so");
187     gSystem->Load("libAOD.so");
188     gSystem->Load("libANALYSIS.so");
189     gSystem->Load("libANALYSISalice.so");
190     gSystem->Load("libPHOSUtils");
191     gSystem->Load("libEMCALUtils");
192     gSystem->Load("libPWG4PartCorrBase.so");
193     gSystem->Load("libPWG4PartCorrDep.so");
194 //     gSystem->Load("libPWG4omega3pi.so");
195 //     gSystem->Load("libCORRFW.so");
196 //     gSystem->Load("libPWG3base.so");
197 //     gSystem->Load("libPWG3muon.so");
198  
199     //--------------------------------------------------------
200     //If you want to use root and par files from aliroot
201     //--------------------------------------------------------  
202 //     SetupPar("STEERBase");
203 //     SetupPar("ESD");
204 //     SetupPar("AOD");
205 //     SetupPar("ANALYSIS");
206 //     SetupPar("ANALYSISalice");
207 //     //If your analysis needs PHOS geometry uncomment following lines
208 //     SetupPar("PHOSUtils");
209 //     SetupPar("EMCALUtils");
210 //     //       //Create Geometry
211 //     //    TGeoManager::Import("geometry.root") ; //need file "geometry.root" in local dir!!!!
212 //     SetupPar("PWG4PartCorrBase");
213 //     SetupPar("PWG4PartCorrDep");
214 //     //SetupPar("PWG4omega3pi");
215   }
216
217   //---------------------------------------------------------
218   // <<<<<<<<<< PROOF mode >>>>>>>>>>>>
219   //---------------------------------------------------------
220   else if (mode==mPROOF) {
221     //
222     // Connect to proof
223     // Put appropriate username here
224     // TProof::Reset("proof://mgheata@lxb6046.cern.ch"); 
225     TProof::Open("proof://mgheata@lxb6046.cern.ch");
226     
227     //    gProof->ClearPackages();
228     //    gProof->ClearPackage("ESD");
229     //    gProof->ClearPackage("AOD");
230     //    gProof->ClearPackage("ANALYSIS");   
231     //    gProof->ClearPackage("PWG4PartCorrBase");
232     //    gProof->ClearPackage("PWG4PartCorrDep");
233     
234     // Enable the STEERBase Package
235     gProof->UploadPackage("STEERBase.par");
236     gProof->EnablePackage("STEERBase");
237     // Enable the ESD Package
238     gProof->UploadPackage("ESD.par");
239     gProof->EnablePackage("ESD");
240     // Enable the AOD Package
241     gProof->UploadPackage("AOD.par");
242     gProof->EnablePackage("AOD");
243     // Enable the Analysis Package
244     gProof->UploadPackage("ANALYSIS.par");
245     gProof->EnablePackage("ANALYSIS");
246     // Enable the PHOS geometry Package
247     //gProof->UploadPackage("PHOSUtils.par");
248     //gProof->EnablePackage("PHOSUtils");
249     // Enable PartCorr analysis
250     gProof->UploadPackage("PWG4PartCorrBase.par");
251     gProof->EnablePackage("PWG4PartCorrBase");
252     gProof->UploadPackage("PWG4PartCorrDep.par");
253     gProof->EnablePackage("PWG4PartCorrDep");    
254     gProof->ShowEnabledPackages();
255   }  
256   
257 }
258
259 void SetupPar(char* pararchivename)
260 {
261   //Load par files, create analysis libraries
262   //For testing, if par file already decompressed and modified
263   //classes then do not decompress.
264  
265   TString cdir(Form("%s", gSystem->WorkingDirectory() )) ; 
266   TString parpar(Form("%s.par", pararchivename)) ; 
267   if ( gSystem->AccessPathName(parpar.Data()) ) {
268     gSystem->ChangeDirectory(gSystem->Getenv("ALICE_ROOT")) ;
269     TString processline(Form(".! make %s", parpar.Data())) ; 
270     gROOT->ProcessLine(processline.Data()) ;
271     gSystem->ChangeDirectory(cdir) ; 
272     processline = Form(".! mv $ALICE_ROOT/%s .", parpar.Data()) ;
273     gROOT->ProcessLine(processline.Data()) ;
274   } 
275   if ( gSystem->AccessPathName(pararchivename) ) {  
276     TString processline = Form(".! tar xvzf %s",parpar.Data()) ;
277     gROOT->ProcessLine(processline.Data());
278   }
279   
280   TString ocwd = gSystem->WorkingDirectory();
281   gSystem->ChangeDirectory(pararchivename);
282   
283   // check for BUILD.sh and execute
284   if (!gSystem->AccessPathName("PROOF-INF/BUILD.sh")) {
285     printf("*******************************\n");
286     printf("*** Building PAR archive    ***\n");
287     cout<<pararchivename<<endl;
288     printf("*******************************\n");
289     
290     if (gSystem->Exec("PROOF-INF/BUILD.sh")) {
291       Error("runProcess","Cannot Build the PAR Archive! - Abort!");
292       return -1;
293     }
294   }
295   // check for SETUP.C and execute
296   if (!gSystem->AccessPathName("PROOF-INF/SETUP.C")) {
297     printf("*******************************\n");
298     printf("*** Setup PAR archive       ***\n");
299     cout<<pararchivename<<endl;
300     printf("*******************************\n");
301     gROOT->Macro("PROOF-INF/SETUP.C");
302   }
303   
304   gSystem->ChangeDirectory(ocwd.Data());
305   printf("Current dir: %s\n", ocwd.Data());
306 }
307
308
309
310 void CreateChain(const anaModes mode, TChain * chain, TChain * chainxs){
311   //Fills chain with data
312   TString ocwd = gSystem->WorkingDirectory();
313   
314   //-----------------------------------------------------------
315   //Analysis of CAF data locally and with PROOF
316   //-----------------------------------------------------------
317   if(mode ==mPROOF || mode ==mLocalCAF){
318     // Chain from CAF
319     gROOT->LoadMacro("$ALICE_ROOT/PWG0/CreateESDChain.C");
320     // The second parameter is the number of input files in the chain
321     chain = CreateESDChain("ESD12001.txt", 5);  
322   }
323   
324   //---------------------------------------
325   //Local files analysis
326   //---------------------------------------
327   else if(mode == mLocal){    
328     //If you want to add several ESD files sitting in a common directory INDIR
329     //Specify as environmental variables the directory (INDIR), the number of files 
330     //to analyze (NFILES) and the pattern name of the directories with files (PATTERN)
331
332     if(gSystem->Getenv("INDIR"))  
333       kInDir = gSystem->Getenv("INDIR") ; 
334     else cout<<"INDIR not set, use default: "<<kInDir<<endl;    
335     
336     if(gSystem->Getenv("PATTERN"))   
337       kPattern = gSystem->Getenv("PATTERN") ; 
338     else  cout<<"PATTERN not set, use default: "<<kPattern<<endl;
339     
340     if(gSystem->Getenv("NFILES"))
341       kFile = atoi(gSystem->Getenv("NFILES")) ;
342     else cout<<"NFILES not set, use default: "<<kFile<<endl;
343     
344     //Check if env variables are set and are correct
345     if ( kInDir  && kFile) {
346       printf("Get %d files from directory %s\n",kFile,kInDir);
347       if ( ! gSystem->cd(kInDir) ) {//check if ESDs directory exist
348         printf("%s does not exist\n", kInDir) ;
349         return ;
350       }
351
352       //if(gSystem->Getenv("XSFILE"))  
353       //kXSFileName = gSystem->Getenv("XSFILE") ; 
354       //else cout<<" XS file name not set, use default: "<<kXSFileName<<endl;   
355       char * kGener = gSystem->Getenv("GENER");
356       if(kGener) {
357         cout<<"GENER "<<kGener<<endl;
358         if(!strcmp(kGener,"PYTHIA")) kXSFileName = "pyxsec.root";
359         else if(!strcmp(kGener,"HERWIG")) kXSFileName = "hexsec.root";
360         else cout<<" UNKNOWN GENER, use default: "<<kXSFileName<<endl;
361       }
362       else cout<<" GENER not set, use default xs file name: "<<kXSFileName<<endl;
363
364       cout<<"INDIR   : "<<kInDir<<endl;
365       cout<<"NFILES  : "<<kFile<<endl;
366       cout<<"PATTERN : " <<kPattern<<endl;
367       cout<<"XSFILE  : "<<kXSFileName<<endl;
368
369       TString datafile="";
370       if(kInputData == "ESD") datafile = "AliESDs.root" ;
371       else if(kInputData == "AOD") datafile = "AliAOD.root" ;
372       else if(kInputData == "MC")  datafile = "galice.root" ;
373       
374       //Loop on ESD files, add them to chain
375       Int_t event =0;
376       Int_t skipped=0 ; 
377       char file[120] ;
378       char filexs[120] ;
379       
380       for (event = 0 ; event < kFile ; event++) {
381         sprintf(file, "%s/%s%d/%s", kInDir,kPattern,event,datafile.Data()) ; 
382         sprintf(filexs, "%s/%s%d/%s", kInDir,kPattern,event,kXSFileName) ; 
383         TFile * fESD = 0 ; 
384         //Check if file exists and add it, if not skip it
385         if ( fESD = TFile::Open(file)) {
386           if ( fESD->Get(kTreeName) ) { 
387             printf("++++ Adding %s\n", file) ;
388             chain->AddFile(file);
389             chainxs->Add(filexs) ; 
390           }
391         }
392         else { 
393           printf("---- Skipping %s\n", file) ;
394           skipped++ ;
395         }
396       }
397       printf("number of entries # %lld, skipped %d\n", chain->GetEntries(), skipped*100) ;      
398     }
399     else {
400       TString input = "AliESDs.root" ;
401       cout<<">>>>>> No list added, take a single file <<<<<<<<< "<<input<<endl;
402       chain->AddFile(input);
403     }
404     
405   }// local files analysis
406   
407   //------------------------------
408   //GRID xml files
409   //-----------------------------
410   else if(mode == mGRID){
411     //Get colection file. It is specified by the environmental
412     //variable XML
413
414     if(gSystem->Getenv("XML") )
415       kXML = gSystem->Getenv("XML");
416     else
417       sprintf(kXML, "collection.xml") ; 
418     
419     if (!TFile::Open(kXML)) {
420       printf("No collection file with name -- %s -- was found\n",kXML);
421       return ;
422     }
423     else cout<<"XML file "<<kXML<<endl;
424
425     //Load necessary libraries and connect to the GRID
426     gSystem->Load("libNetx.so") ; 
427     gSystem->Load("libRAliEn.so"); 
428     TGrid::Connect("alien://") ;
429
430     //Feed Grid with collection file
431     //TGridCollection * collection =  (TGridCollection*)gROOT->ProcessLine(Form("TAlienCollection::Open(\"%s\", 0)", kXML));
432     TGridCollection * collection = (TGridCollection*) TAlienCollection::Open(kXML);
433     if (! collection) {
434       AliError(Form("%s not found", kXML)) ; 
435       return kFALSE ; 
436     }
437     TGridResult* result = collection->GetGridResult("",0 ,0);
438    
439     // Makes the ESD chain 
440     printf("*** Getting the Chain       ***\n");
441     for (Int_t index = 0; index < result->GetEntries(); index++) {
442       TString alienURL = result->GetKey(index, "turl") ; 
443       cout << "================== " << alienURL << endl ; 
444       chain->Add(alienURL) ; 
445       alienURL.ReplaceAll("AliESDs.root",kXSFileName);
446       chainxs->Add(alienURL) ; 
447     }
448   }// xml analysis
449   
450   gSystem->ChangeDirectory(ocwd.Data());
451 }
452
453 //________________________________________________
454 void GetAverageXsection(TTree * tree, Double_t & xs, Float_t & ntr)
455 {
456   // Read the PYTHIA statistics from the file pyxsec.root created by
457   // the function WriteXsection():
458   // integrated cross section (xsection) and
459   // the  number of Pyevent() calls (ntrials)
460   // and calculate the weight per one event xsection/ntrials
461   // The spectrum calculated by a user should be
462   // multiplied by this weight, something like this:
463   // TH1F *userSpectrum ... // book and fill the spectrum
464   // userSpectrum->Scale(weight)
465   //
466   // Yuri Kharlov 19 June 2007
467   // Gustavo Conesa 15 April 2008
468   Double_t xsection = 0;
469   UInt_t    ntrials = 0;
470   xs = 0;
471   ntr = 0;
472   
473   Int_t nfiles =  tree->GetEntries()  ;
474   if (tree && nfiles > 0) {
475     tree->SetBranchAddress("xsection",&xsection);
476     tree->SetBranchAddress("ntrials",&ntrials);
477     for(Int_t i = 0; i < nfiles; i++){
478       tree->GetEntry(i);
479       xs += xsection ;
480       ntr += ntrials ;
481       cout << "xsection " <<xsection<<" ntrials "<<ntrials<<endl; 
482     }
483     
484     xs =   xs /  nfiles;
485     ntr =  ntr / nfiles;
486     cout << "-----------------------------------------------------------------"<<endl;
487     cout << "Average of "<< nfiles<<" files: xsection " <<xs<<" ntrials "<<ntr<<endl; 
488     cout << "-----------------------------------------------------------------"<<endl;
489   } 
490   else cout << " >>>> Empty tree !!!! <<<<< "<<endl;
491   
492 }
493
494
495