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