]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG4/macros/anaM.C
Add line in makefile to allow compilation of PWG4PartCorrDep.par, add libPhysics...
[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   gSystem->Load("libMatrix.so");
168   gSystem->Load("libPhysics.so");
169
170   //----------------------------------------------------------
171   // >>>>>>>>>>> Local mode <<<<<<<<<<<<<< 
172   //----------------------------------------------------------
173   if (mode==mLocal || mode == mLocalCAF || mode == mGRID) {
174     //--------------------------------------------------------
175     // If you want to use already compiled libraries 
176     // in the aliroot distribution
177     //--------------------------------------------------------
178     //gSystem->Load("/Users/Gustavo/Work/analysis/STEERBase/libSTEERBase.so");
179     //gSystem->Load("/Users/Gustavo/Work/analysis/ESD/libESD.so");
180     //gSystem->Load("/Users/Gustavo/Work/analysis/AOD/libAOD.so");
181     //gSystem->Load("/Users/Gustavo/Work/analysis/ANALYSIS/libANALYSIS.so");
182     //gSystem->Load("/Users/Gustavo/Work/analysis/ANALYSISalice/libANALYSISalice.so");
183     //gSystem->Load("libPHOSUtils");
184     //gSystem->Load("/Users/Gustavo/Work/analysis/PWG4PartCorrBase/libPWG4PartCorrBase.so");
185     //gSystem->Load("/Users/Gustavo/Work/analysis/PWG4PartCorrDep/libPWG4PartCorrDep.so");
186         
187     gSystem->Load("libSTEERBase.so");
188     gSystem->Load("libESD.so");
189     gSystem->Load("libAOD.so");
190     gSystem->Load("libANALYSIS.so");
191     gSystem->Load("libANALYSISalice.so");
192     gSystem->Load("libPHOSUtils");
193     gSystem->Load("libEMCALUtils");
194     gSystem->Load("libPWG4PartCorrBase.so");
195     gSystem->Load("libPWG4PartCorrDep.so");
196 //     gSystem->Load("libPWG4omega3pi.so");
197 //     gSystem->Load("libCORRFW.so");
198 //     gSystem->Load("libPWG3base.so");
199 //     gSystem->Load("libPWG3muon.so");
200  
201     //--------------------------------------------------------
202     //If you want to use root and par files from aliroot
203     //--------------------------------------------------------  
204 //     SetupPar("STEERBase");
205 //     SetupPar("ESD");
206 //     SetupPar("AOD");
207 //     SetupPar("ANALYSIS");
208 //     SetupPar("ANALYSISalice");
209 //     //If your analysis needs PHOS geometry uncomment following lines
210 //     SetupPar("PHOSUtils");
211 //     SetupPar("EMCALUtils");
212 //     //       //Create Geometry
213 //     //    TGeoManager::Import("geometry.root") ; //need file "geometry.root" in local dir!!!!
214 //     SetupPar("PWG4PartCorrBase");
215 //     SetupPar("PWG4PartCorrDep");
216 //     //SetupPar("PWG4omega3pi");
217   }
218
219   //---------------------------------------------------------
220   // <<<<<<<<<< PROOF mode >>>>>>>>>>>>
221   //---------------------------------------------------------
222   else if (mode==mPROOF) {
223     //
224     // Connect to proof
225     // Put appropriate username here
226     // TProof::Reset("proof://mgheata@lxb6046.cern.ch"); 
227     TProof::Open("proof://mgheata@lxb6046.cern.ch");
228     
229     //    gProof->ClearPackages();
230     //    gProof->ClearPackage("ESD");
231     //    gProof->ClearPackage("AOD");
232     //    gProof->ClearPackage("ANALYSIS");   
233     //    gProof->ClearPackage("PWG4PartCorrBase");
234     //    gProof->ClearPackage("PWG4PartCorrDep");
235     
236     // Enable the STEERBase Package
237     gProof->UploadPackage("STEERBase.par");
238     gProof->EnablePackage("STEERBase");
239     // Enable the ESD Package
240     gProof->UploadPackage("ESD.par");
241     gProof->EnablePackage("ESD");
242     // Enable the AOD Package
243     gProof->UploadPackage("AOD.par");
244     gProof->EnablePackage("AOD");
245     // Enable the Analysis Package
246     gProof->UploadPackage("ANALYSIS.par");
247     gProof->EnablePackage("ANALYSIS");
248     // Enable the PHOS geometry Package
249     //gProof->UploadPackage("PHOSUtils.par");
250     //gProof->EnablePackage("PHOSUtils");
251     // Enable PartCorr analysis
252     gProof->UploadPackage("PWG4PartCorrBase.par");
253     gProof->EnablePackage("PWG4PartCorrBase");
254     gProof->UploadPackage("PWG4PartCorrDep.par");
255     gProof->EnablePackage("PWG4PartCorrDep");    
256     gProof->ShowEnabledPackages();
257   }  
258   
259 }
260
261 void SetupPar(char* pararchivename)
262 {
263   //Load par files, create analysis libraries
264   //For testing, if par file already decompressed and modified
265   //classes then do not decompress.
266  
267   TString cdir(Form("%s", gSystem->WorkingDirectory() )) ; 
268   TString parpar(Form("%s.par", pararchivename)) ; 
269   if ( gSystem->AccessPathName(parpar.Data()) ) {
270     gSystem->ChangeDirectory(gSystem->Getenv("ALICE_ROOT")) ;
271     TString processline(Form(".! make %s", parpar.Data())) ; 
272     gROOT->ProcessLine(processline.Data()) ;
273     gSystem->ChangeDirectory(cdir) ; 
274     processline = Form(".! mv $ALICE_ROOT/%s .", parpar.Data()) ;
275     gROOT->ProcessLine(processline.Data()) ;
276   } 
277   if ( gSystem->AccessPathName(pararchivename) ) {  
278     TString processline = Form(".! tar xvzf %s",parpar.Data()) ;
279     gROOT->ProcessLine(processline.Data());
280   }
281   
282   TString ocwd = gSystem->WorkingDirectory();
283   gSystem->ChangeDirectory(pararchivename);
284   
285   // check for BUILD.sh and execute
286   if (!gSystem->AccessPathName("PROOF-INF/BUILD.sh")) {
287     printf("*******************************\n");
288     printf("*** Building PAR archive    ***\n");
289     cout<<pararchivename<<endl;
290     printf("*******************************\n");
291     
292     if (gSystem->Exec("PROOF-INF/BUILD.sh")) {
293       Error("runProcess","Cannot Build the PAR Archive! - Abort!");
294       return -1;
295     }
296   }
297   // check for SETUP.C and execute
298   if (!gSystem->AccessPathName("PROOF-INF/SETUP.C")) {
299     printf("*******************************\n");
300     printf("*** Setup PAR archive       ***\n");
301     cout<<pararchivename<<endl;
302     printf("*******************************\n");
303     gROOT->Macro("PROOF-INF/SETUP.C");
304   }
305   
306   gSystem->ChangeDirectory(ocwd.Data());
307   printf("Current dir: %s\n", ocwd.Data());
308 }
309
310
311
312 void CreateChain(const anaModes mode, TChain * chain, TChain * chainxs){
313   //Fills chain with data
314   TString ocwd = gSystem->WorkingDirectory();
315   
316   //-----------------------------------------------------------
317   //Analysis of CAF data locally and with PROOF
318   //-----------------------------------------------------------
319   if(mode ==mPROOF || mode ==mLocalCAF){
320     // Chain from CAF
321     gROOT->LoadMacro("$ALICE_ROOT/PWG0/CreateESDChain.C");
322     // The second parameter is the number of input files in the chain
323     chain = CreateESDChain("ESD12001.txt", 5);  
324   }
325   
326   //---------------------------------------
327   //Local files analysis
328   //---------------------------------------
329   else if(mode == mLocal){    
330     //If you want to add several ESD files sitting in a common directory INDIR
331     //Specify as environmental variables the directory (INDIR), the number of files 
332     //to analyze (NFILES) and the pattern name of the directories with files (PATTERN)
333
334     if(gSystem->Getenv("INDIR"))  
335       kInDir = gSystem->Getenv("INDIR") ; 
336     else cout<<"INDIR not set, use default: "<<kInDir<<endl;    
337     
338     if(gSystem->Getenv("PATTERN"))   
339       kPattern = gSystem->Getenv("PATTERN") ; 
340     else  cout<<"PATTERN not set, use default: "<<kPattern<<endl;
341     
342     if(gSystem->Getenv("NFILES"))
343       kFile = atoi(gSystem->Getenv("NFILES")) ;
344     else cout<<"NFILES not set, use default: "<<kFile<<endl;
345     
346     //Check if env variables are set and are correct
347     if ( kInDir  && kFile) {
348       printf("Get %d files from directory %s\n",kFile,kInDir);
349       if ( ! gSystem->cd(kInDir) ) {//check if ESDs directory exist
350         printf("%s does not exist\n", kInDir) ;
351         return ;
352       }
353
354       //if(gSystem->Getenv("XSFILE"))  
355       //kXSFileName = gSystem->Getenv("XSFILE") ; 
356       //else cout<<" XS file name not set, use default: "<<kXSFileName<<endl;   
357       char * kGener = gSystem->Getenv("GENER");
358       if(kGener) {
359         cout<<"GENER "<<kGener<<endl;
360         if(!strcmp(kGener,"PYTHIA")) kXSFileName = "pyxsec.root";
361         else if(!strcmp(kGener,"HERWIG")) kXSFileName = "hexsec.root";
362         else cout<<" UNKNOWN GENER, use default: "<<kXSFileName<<endl;
363       }
364       else cout<<" GENER not set, use default xs file name: "<<kXSFileName<<endl;
365
366       cout<<"INDIR   : "<<kInDir<<endl;
367       cout<<"NFILES  : "<<kFile<<endl;
368       cout<<"PATTERN : " <<kPattern<<endl;
369       cout<<"XSFILE  : "<<kXSFileName<<endl;
370
371       TString datafile="";
372       if(kInputData == "ESD") datafile = "AliESDs.root" ;
373       else if(kInputData == "AOD") datafile = "AliAOD.root" ;
374       else if(kInputData == "MC")  datafile = "galice.root" ;
375       
376       //Loop on ESD files, add them to chain
377       Int_t event =0;
378       Int_t skipped=0 ; 
379       char file[120] ;
380       char filexs[120] ;
381       
382       for (event = 0 ; event < kFile ; event++) {
383         sprintf(file, "%s/%s%d/%s", kInDir,kPattern,event,datafile.Data()) ; 
384         sprintf(filexs, "%s/%s%d/%s", kInDir,kPattern,event,kXSFileName) ; 
385         TFile * fESD = 0 ; 
386         //Check if file exists and add it, if not skip it
387         if ( fESD = TFile::Open(file)) {
388           if ( fESD->Get(kTreeName) ) { 
389             printf("++++ Adding %s\n", file) ;
390             chain->AddFile(file);
391             chainxs->Add(filexs) ; 
392           }
393         }
394         else { 
395           printf("---- Skipping %s\n", file) ;
396           skipped++ ;
397         }
398       }
399       printf("number of entries # %lld, skipped %d\n", chain->GetEntries(), skipped*100) ;      
400     }
401     else {
402       TString input = "AliESDs.root" ;
403       cout<<">>>>>> No list added, take a single file <<<<<<<<< "<<input<<endl;
404       chain->AddFile(input);
405     }
406     
407   }// local files analysis
408   
409   //------------------------------
410   //GRID xml files
411   //-----------------------------
412   else if(mode == mGRID){
413     //Get colection file. It is specified by the environmental
414     //variable XML
415
416     if(gSystem->Getenv("XML") )
417       kXML = gSystem->Getenv("XML");
418     else
419       sprintf(kXML, "collection.xml") ; 
420     
421     if (!TFile::Open(kXML)) {
422       printf("No collection file with name -- %s -- was found\n",kXML);
423       return ;
424     }
425     else cout<<"XML file "<<kXML<<endl;
426
427     //Load necessary libraries and connect to the GRID
428     gSystem->Load("libNetx.so") ; 
429     gSystem->Load("libRAliEn.so"); 
430     TGrid::Connect("alien://") ;
431
432     //Feed Grid with collection file
433     //TGridCollection * collection =  (TGridCollection*)gROOT->ProcessLine(Form("TAlienCollection::Open(\"%s\", 0)", kXML));
434     TGridCollection * collection = (TGridCollection*) TAlienCollection::Open(kXML);
435     if (! collection) {
436       AliError(Form("%s not found", kXML)) ; 
437       return kFALSE ; 
438     }
439     TGridResult* result = collection->GetGridResult("",0 ,0);
440    
441     // Makes the ESD chain 
442     printf("*** Getting the Chain       ***\n");
443     for (Int_t index = 0; index < result->GetEntries(); index++) {
444       TString alienURL = result->GetKey(index, "turl") ; 
445       cout << "================== " << alienURL << endl ; 
446       chain->Add(alienURL) ; 
447       alienURL.ReplaceAll("AliESDs.root",kXSFileName);
448       chainxs->Add(alienURL) ; 
449     }
450   }// xml analysis
451   
452   gSystem->ChangeDirectory(ocwd.Data());
453 }
454
455 //________________________________________________
456 void GetAverageXsection(TTree * tree, Double_t & xs, Float_t & ntr)
457 {
458   // Read the PYTHIA statistics from the file pyxsec.root created by
459   // the function WriteXsection():
460   // integrated cross section (xsection) and
461   // the  number of Pyevent() calls (ntrials)
462   // and calculate the weight per one event xsection/ntrials
463   // The spectrum calculated by a user should be
464   // multiplied by this weight, something like this:
465   // TH1F *userSpectrum ... // book and fill the spectrum
466   // userSpectrum->Scale(weight)
467   //
468   // Yuri Kharlov 19 June 2007
469   // Gustavo Conesa 15 April 2008
470   Double_t xsection = 0;
471   UInt_t    ntrials = 0;
472   xs = 0;
473   ntr = 0;
474   
475   Int_t nfiles =  tree->GetEntries()  ;
476   if (tree && nfiles > 0) {
477     tree->SetBranchAddress("xsection",&xsection);
478     tree->SetBranchAddress("ntrials",&ntrials);
479     for(Int_t i = 0; i < nfiles; i++){
480       tree->GetEntry(i);
481       xs += xsection ;
482       ntr += ntrials ;
483       cout << "xsection " <<xsection<<" ntrials "<<ntrials<<endl; 
484     }
485     
486     xs =   xs /  nfiles;
487     ntr =  ntr / nfiles;
488     cout << "-----------------------------------------------------------------"<<endl;
489     cout << "Average of "<< nfiles<<" files: xsection " <<xs<<" ntrials "<<ntr<<endl; 
490     cout << "-----------------------------------------------------------------"<<endl;
491   } 
492   else cout << " >>>> Empty tree !!!! <<<<< "<<endl;
493   
494 }
495
496
497