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