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