New more general analysis implemention for particle identification and correlation...
[u/mrichter/AliRoot.git] / PWG4 / macros / anaExampleTask.C
CommitLineData
d92b41ad 1/* $Id: anaGammaAnalysis.C 25095 2008-04-11 12:54:47Z schutz $ */
2/* $Log$
3/* Revision 1.2 2007/12/13 09:45:45 gustavo
4/* Scaling option and more comentaries added
5/*
6/* Revision 1.1 2007/12/07 14:13:02 gustavo
7/* Example macros for execution and configuration of the analysis
8/* */
9
10//---------------------------------------------------
11// Example macro to do analysis with the
12// AliAnalysisTaksSE
13// Can be executed with Root and AliRoot
14//
15// Pay attention to the options and definitions
16// set in the lines below
17//
18// Author : Gustavo Conesa Balbastre (INFN-LNF)
19//
20//-------------------------------------------------
21enum anaModes {mLocal, mLocalCAF,mPROOF,mGRID};
22//mLocal: Analyze locally files in your computer
23//mLocalCAF: Analyze locally CAF files
24//mPROOF: Analyze CAF files with PROOF
25
26//---------------------------------------------------------------------------
27//Settings to read locally several files, only for "mLocal" mode
28//The different values are default, they can be set with environmental
29//variables: INDIR, PATTERN, NEVENT, respectivelly
30char * kInDir = "/user/data";
31char * kPattern = ""; // Data are in diles /data/Run0,
32// /Data/Run1 ...
33Int_t kEvent = 1; // Number of files
34//---------------------------------------------------------------------------
35//Collection file for grid analysis
36char * kXML = "collection.xml";
37//---------------------------------------------------------------------------
38//Scale histograms from file. Change to kTRUE when xsection file exists
39//Put name of file containing xsection
40//Put number of events per ESD file
41//This is an specific case for normalization of Pythia files.
42const Bool_t kGetXSectionFromFileAndScale = kFALSE ;
43const char * kXSFileName = "pyxsec.root";
44const Int_t kNumberOfEventsPerFile = 100;
45//---------------------------------------------------------------------------
46
47const Bool_t kMC = kTRUE; //With real data kMC = kFALSE
48const TString kInputData = "ESD";
49
50void anaExampleTask(Int_t mode=mLocal)
51{
52 // Main
53
54 //--------------------------------------------------------------------
55 // Load analysis libraries
56 // Look at the method below,
57 // change whatever you need for your analysis case
58 // ------------------------------------------------------------------
59 LoadLibraries(mode) ;
60
61 //-------------------------------------------------------------------------------------------------
62 //Create chain from ESD and from cross sections files, look below for options.
63 //-------------------------------------------------------------------------------------------------
64 TChain *chain = new TChain("esdTree") ;
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
76 if(kMC){
77 AliMCEventHandler* mcHandler = new AliMCEventHandler();
78 mgr->SetMCtruthEventHandler(mcHandler);
79 }
80
81 // AOD output handler
82 AliAODHandler* aodoutHandler = new AliAODHandler();
83 aodoutHandler->SetOutputFileName("aod.root");
84 //aodoutHandler->SetCreateNonStandardAOD();
85 mgr->SetOutputEventHandler(aodoutHandler);
86
87 //input
88 if(kInputData == "ESD"){
89 // ESD handler
90 AliESDInputHandler *esdHandler = new AliESDInputHandler();
91 mgr->SetInputEventHandler(esdHandler);
92 }
93 if(kInputData == "AOD"){
94 // AOD handler
95 AliAODInputHandler *aodHandler = new AliAODInputHandler();
96 mgr->SetInputEventHandler(aodHandler);
97 }
98
99 //mgr->SetDebugLevel(10); // For debugging
100
101 //-------------------------------------------------------------------------
102 //Define task, put here any other task that you want to use.
103 //-------------------------------------------------------------------------
104 AliAnalysisTaskPHOSExample * taskex = new AliAnalysisTaskPHOSExample ("Gamma");
105 mgr->AddTask(taskex);
106
107 // Create containers for input/output
108 AliAnalysisDataContainer *cinput1 = mgr->CreateContainer("cchain",TChain::Class(),
109 AliAnalysisManager::kInputContainer);
110 AliAnalysisDataContainer *coutput1 = mgr->CreateContainer("tree", TTree::Class(),
111 AliAnalysisManager::kOutputContainer, "default");
112 AliAnalysisDataContainer *coutput2 = mgr->CreateContainer("histos", TList::Class(),
113 AliAnalysisManager::kOutputContainer, "histos.root");
114
115 mgr->ConnectInput (taskex, 0, cinput1);
116 mgr->ConnectOutput (taskex, 0, coutput1 );
117 mgr->ConnectOutput (taskex, 1, coutput2 );
118
119 //------------------------
120 //Scaling task
121 //-----------------------
122 Int_t nfiles = chainxs->GetEntries();
123 if(kGetXSectionFromFileAndScale && nfiles > 0){
124 //Get the cross section
125 Double_t xsection=0;
126 Float_t ntrials = 0;
127 GetAverageXsection(chainxs, xsection, ntrials);
128
129 AliAnaScale * scale = new AliAnaScale("scale") ;
130 scale->Set(xsection/ntrials/kNumberOfEventsPerFile/nfiles) ;
131 mgr->AddTask(scale);
132
133 AliAnalysisDataContainer *coutput3 = mgr->CreateContainer("histosscaled", TList::Class(),
134 AliAnalysisManager::kOutputContainer, "gammahistosscaled.root");
135 mgr->ConnectInput (scale, 0, coutput2);
136 mgr->ConnectOutput (scale, 0, coutput3 );
137 }
138
139 //-----------------------
140 // Run the analysis
141 //-----------------------
142 TString smode = "";
143 if (mode==mLocal || mode == mLocalCAF)
144 smode = "local";
145 else if (mode==mPROOF)
146 smode = "proof";
147 else if (mode==mGRID)
148 smode = "grid";
149
150 mgr->InitAnalysis();
151 mgr->PrintStatus();
152 mgr->StartAnalysis(smode.Data(),chain);
153 }
154 else cout << "Chain was not produced ! "<<endl;
155
156}
157
158void 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("libSTEERBase");
177 //gSystem->Load("libESD");
178 //gSystem->Load("libAOD");
179 //gSystem->Load("libANALYSIS");
180 //gSystem->Load("libANALYSISalice");
181 //gSystem->Load("libPWG4PartCorr");
182
183 //--------------------------------------------------------
184 //If you want to use root and par files from aliroot
185 //--------------------------------------------------------
186 SetupPar("STEERBase");
187 SetupPar("ESD");
188 SetupPar("AOD");
189 SetupPar("ANALYSIS");
190 SetupPar("ANALYSISalice");
191 SetupPar("PWG4PartCorr");
192
193 }
194
195 //---------------------------------------------------------
196 // <<<<<<<<<< PROOF mode >>>>>>>>>>>>
197 //---------------------------------------------------------
198 else if (mode==mPROOF) {
199 //
200 // Connect to proof
201 // Put appropriate username here
202 // TProof::Reset("proof://mgheata@lxb6046.cern.ch");
203 TProof::Open("proof://mgheata@lxb6046.cern.ch");
204
205 // gProof->ClearPackages();
206 // gProof->ClearPackage("ESD");
207 // gProof->ClearPackage("AOD");
208 // gProof->ClearPackage("ANALYSIS");
209 // gProof->ClearPackage("PWG4Gamma");
210
211 // Enable the STEERBase Package
212 gProof->UploadPackage("STEERBase.par");
213 gProof->EnablePackage("STEERBase");
214 // Enable the ESD Package
215 gProof->UploadPackage("ESD.par");
216 gProof->EnablePackage("ESD");
217 // Enable the AOD Package
218 gProof->UploadPackage("AOD.par");
219 gProof->EnablePackage("AOD");
220 // Enable the Analysis Package
221 gProof->UploadPackage("ANALYSIS.par");
222 gProof->EnablePackage("ANALYSIS");
223 // Enable gamma jet analysis
224 gProof->UploadPackage("PWG4Gamma.par");
225 gProof->EnablePackage("PWG4Gamma");
226 //
227 gProof->ShowEnabledPackages();
228 }
229
230}
231
232void SetupPar(char* pararchivename)
233{
234 //Load par files, create analysis libraries
235 //For testing, if par file already decompressed and modified
236 //classes then do not decompress.
237
238 TString cdir(Form("%s", gSystem->WorkingDirectory() )) ;
239 TString parpar(Form("%s.par", pararchivename)) ;
240 if ( gSystem->AccessPathName(parpar.Data()) ) {
241 gSystem->ChangeDirectory(gSystem->Getenv("ALICE_ROOT")) ;
242 TString processline(Form(".! make %s", parpar.Data())) ;
243 gROOT->ProcessLine(processline.Data()) ;
244 gSystem->ChangeDirectory(cdir) ;
245 processline = Form(".! mv /tmp/%s .", parpar.Data()) ;
246 gROOT->ProcessLine(processline.Data()) ;
247 }
248 if ( gSystem->AccessPathName(pararchivename) ) {
249 TString processline = Form(".! tar xvzf %s",parpar.Data()) ;
250 gROOT->ProcessLine(processline.Data());
251 }
252
253 TString ocwd = gSystem->WorkingDirectory();
254 gSystem->ChangeDirectory(pararchivename);
255
256 // check for BUILD.sh and execute
257 if (!gSystem->AccessPathName("PROOF-INF/BUILD.sh")) {
258 printf("*******************************\n");
259 printf("*** Building PAR archive ***\n");
260 cout<<pararchivename<<endl;
261 printf("*******************************\n");
262
263 if (gSystem->Exec("PROOF-INF/BUILD.sh")) {
264 Error("runProcess","Cannot Build the PAR Archive! - Abort!");
265 return -1;
266 }
267 }
268 // check for SETUP.C and execute
269 if (!gSystem->AccessPathName("PROOF-INF/SETUP.C")) {
270 printf("*******************************\n");
271 printf("*** Setup PAR archive ***\n");
272 cout<<pararchivename<<endl;
273 printf("*******************************\n");
274 gROOT->Macro("PROOF-INF/SETUP.C");
275 }
276
277 gSystem->ChangeDirectory(ocwd.Data());
278 printf("Current dir: %s\n", ocwd.Data());
279}
280
281
282
283void CreateChain(const anaModes mode, TChain * chain, TChain * chainxs){
284 //Fills chain with data
285 TString ocwd = gSystem->WorkingDirectory();
286
287 //-----------------------------------------------------------
288 //Analysis of CAF data locally and with PROOF
289 //-----------------------------------------------------------
290 if(mode ==mPROOF || mode ==mLocalCAF){
291 // Chain from CAF
292 gROOT->LoadMacro("$ALICE_ROOT/PWG0/CreateESDChain.C");
293 // The second parameter is the number of input files in the chain
294 chain = CreateESDChain("ESD12001.txt", 5);
295 }
296
297 //---------------------------------------
298 //Local files analysis
299 //---------------------------------------
300 else if(mode == mLocal){
301 //If you want to add several ESD files sitting in a common directory INDIR
302 //Specify as environmental variables the directory (INDIR), the number of files
303 //to analyze (NEVENT) and the pattern name of the directories with files (PATTERN)
304
305 if(gSystem->Getenv("INDIR"))
306 kInDir = gSystem->Getenv("INDIR") ;
307 else cout<<"INDIR not set, use default: "<<kInDir<<endl;
308
309 if(gSystem->Getenv("PATTERN"))
310 kPattern = gSystem->Getenv("PATTERN") ;
311 else cout<<"PATTERN not set, use default: "<<kPattern<<endl;
312
313 if(gSystem->Getenv("NEVENT"))
314 kEvent = atoi(gSystem->Getenv("NEVENT")) ;
315 else cout<<"NEVENT not set, use default: "<<kEvent<<endl;
316
317 //Check if env variables are set and are correct
318 if ( kInDir && kEvent) {
319 printf("Get %d files from directory %s\n",kEvent,kInDir);
320 if ( ! gSystem->cd(kInDir) ) {//check if ESDs directory exist
321 printf("%s does not exist\n", kInDir) ;
322 return ;
323 }
324 cout<<"INDIR : "<<kInDir<<endl;
325 cout<<"NEVENT : "<<kEvent<<endl;
326 cout<<"PATTERN: " <<kPattern<<endl;
327
328 //Loop on ESD files, add them to chain
329 Int_t event =0;
330 Int_t skipped=0 ;
331 char file[120] ;
332 char filexs[120] ;
333
334 for (event = 0 ; event < kEvent ; event++) {
335 sprintf(file, "%s/%s%d/AliESDs.root", kInDir,kPattern,event) ;
336 sprintf(filexs, "%s/%s%d/%s", kInDir,kPattern,event,kXSFileName) ;
337 TFile * fESD = 0 ;
338 //Check if file exists and add it, if not skip it
339 if ( fESD = TFile::Open(file)) {
340 if ( fESD->Get("esdTree") ) {
341 printf("++++ Adding %s\n", file) ;
342 chain->AddFile(file);
343 chainxs->Add(filexs) ;
344 }
345 }
346 else {
347 printf("---- Skipping %s\n", file) ;
348 skipped++ ;
349 }
350 }
351 printf("number of entries # %lld, skipped %d\n", chain->GetEntries(), skipped*100) ;
352 }
353 else {
354 TString input = "AliESDs.root" ;
355 cout<<">>>>>> No list added, take a single file <<<<<<<<< "<<input<<endl;
356 chain->AddFile(input);
357 }
358
359 }// local files analysis
360
361 //------------------------------
362 //GRID xml files
363 //-----------------------------
364 else if(mode == mGRID){
365 //Get colection file. It is specified by the environmental
366 //variable XML
367
368 if(gSystem->Getenv("XML") )
369 kXML = gSystem->Getenv("XML");
370 else
371 sprintf(kXML, "collection.xml") ;
372
373 if (!TFile::Open(kXML)) {
374 printf("No collection file with name -- %s -- was found\n",kXML);
375 return ;
376 }
377 else cout<<"XML file "<<kXML<<endl;
378
379 //Load necessary libraries and connect to the GRID
380 gSystem->Load("libNetx.so") ;
381 gSystem->Load("libRAliEn.so");
382 TGrid::Connect("alien://") ;
383
384 //Feed Grid with collection file
385 //TGridCollection * collection = (TGridCollection*)gROOT->ProcessLine(Form("TAlienCollection::Open(\"%s\", 0)", kXML));
386 TGridCollection * collection = (TGridCollection*) TAlienCollection::Open(kXML);
387 if (! collection) {
388 AliError(Form("%s not found", kXML)) ;
389 return kFALSE ;
390 }
391 TGridResult* result = collection->GetGridResult("",0 ,0);
392
393 // Makes the ESD chain
394 printf("*** Getting the Chain ***\n");
395 for (Int_t index = 0; index < result->GetEntries(); index++) {
396 TString alienURL = result->GetKey(index, "turl") ;
397 cout << "================== " << alienURL << endl ;
398 chain->Add(alienURL) ;
399 alienURL.ReplaceAll("AliESDs.root",kXSFileName);
400 chainxs->Add(alienURL) ;
401 }
402 }// xml analysis
403
404 gSystem->ChangeDirectory(ocwd.Data());
405}
406
407//________________________________________________
408void GetAverageXsection(TTree * tree, Double_t & xs, Float_t & ntr)
409{
410 // Read the PYTHIA statistics from the file pyxsec.root created by
411 // the function WriteXsection():
412 // integrated cross section (xsection) and
413 // the number of Pyevent() calls (ntrials)
414 // and calculate the weight per one event xsection/ntrials
415 // The spectrum calculated by a user should be
416 // multiplied by this weight, something like this:
417 // TH1F *userSpectrum ... // book and fill the spectrum
418 // userSpectrum->Scale(weight)
419 //
420 // Yuri Kharlov 19 June 2007
421 // Gustavo Conesa 15 April 2008
422 Double_t xsection = 0;
423 UInt_t ntrials = 0;
424 xs = 0;
425 ntr = 0;
426
427 Int_t nfiles = tree->GetEntries() ;
428 if (tree && nfiles > 0) {
429 tree->SetBranchAddress("xsection",&xsection);
430 tree->SetBranchAddress("ntrials",&ntrials);
431 for(Int_t i = 0; i < nfiles; i++){
432 tree->GetEntry(i);
433 xs += xsection ;
434 ntr += ntrials ;
435 cout << "xsection " <<xsection<<" ntrials "<<ntrials<<endl;
436 }
437
438 xs = xs / nfiles;
439 ntr = ntr / nfiles;
440 cout << "-----------------------------------------------------------------"<<endl;
441 cout << "Average of "<< nfiles<<" files: xsection " <<xs<<" ntrials "<<ntr<<endl;
442 cout << "-----------------------------------------------------------------"<<endl;
443 }
444 else cout << " >>>> Empty tree !!!! <<<<< "<<endl;
445
446}
447
448
449