]> git.uio.no Git - u/mrichter/AliRoot.git/blame - PWG4/macros/anaExampleTask.C
possiblity to read ESD friends (Jacek)
[u/mrichter/AliRoot.git] / PWG4 / macros / anaExampleTask.C
CommitLineData
1e1befe8 1/* $Id: $ */
2//--------------------------------------------------
d92b41ad 3// Example macro to do analysis with the
4// AliAnalysisTaksSE
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
1e1befe8 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
1e1befe8 39const TString kInputData = "ESD";//ESD, AOD, MC
40TString kTreeName = "esdTree";
d92b41ad 41
42void anaExampleTask(Int_t mode=mLocal)
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 //-------------------------------------------------------------------------------------------------
1e1befe8 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
1e1befe8 76 if(kMC || kInputData == "MC"){
d92b41ad 77 AliMCEventHandler* mcHandler = new AliMCEventHandler();
377108c5 78 mcHandler->SetReadTR(kFALSE);//Do not search TrackRef file
d92b41ad 79 mgr->SetMCtruthEventHandler(mcHandler);
1e1befe8 80 if( kInputData == "MC") mgr->SetInputEventHandler(NULL);
d92b41ad 81 }
82
83 // AOD output handler
84 AliAODHandler* aodoutHandler = new AliAODHandler();
85 aodoutHandler->SetOutputFileName("aod.root");
86 //aodoutHandler->SetCreateNonStandardAOD();
87 mgr->SetOutputEventHandler(aodoutHandler);
88
89 //input
90 if(kInputData == "ESD"){
91 // ESD handler
92 AliESDInputHandler *esdHandler = new AliESDInputHandler();
93 mgr->SetInputEventHandler(esdHandler);
94 }
95 if(kInputData == "AOD"){
96 // AOD handler
97 AliAODInputHandler *aodHandler = new AliAODInputHandler();
98 mgr->SetInputEventHandler(aodHandler);
99 }
100
1e1befe8 101 //mgr->SetDebugLevel(-1); // For debugging
d92b41ad 102
103 //-------------------------------------------------------------------------
104 //Define task, put here any other task that you want to use.
105 //-------------------------------------------------------------------------
106 AliAnalysisTaskPHOSExample * taskex = new AliAnalysisTaskPHOSExample ("Gamma");
107 mgr->AddTask(taskex);
108
109 // Create containers for input/output
8a546c82 110 AliAnalysisDataContainer *cinput1 = mgr->GetCommonInputContainer();
111 AliAnalysisDataContainer *coutput1 = mgr->GetCommonOutputContainer();
d92b41ad 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) ;
1e1befe8 131 scale->MakeSumw2(kFALSE);//If you want histograms with error bars set to kTRUE
132 //scale->SetDebugLevel(2);
d92b41ad 133 mgr->AddTask(scale);
1e1befe8 134
d92b41ad 135 AliAnalysisDataContainer *coutput3 = mgr->CreateContainer("histosscaled", TList::Class(),
136 AliAnalysisManager::kOutputContainer, "gammahistosscaled.root");
137 mgr->ConnectInput (scale, 0, coutput2);
138 mgr->ConnectOutput (scale, 0, coutput3 );
139 }
140
141 //-----------------------
142 // Run the analysis
143 //-----------------------
144 TString smode = "";
145 if (mode==mLocal || mode == mLocalCAF)
146 smode = "local";
147 else if (mode==mPROOF)
148 smode = "proof";
149 else if (mode==mGRID)
29d1ef1d 150 smode = "local";
d92b41ad 151
152 mgr->InitAnalysis();
153 mgr->PrintStatus();
154 mgr->StartAnalysis(smode.Data(),chain);
1e1befe8 155
156 cout <<" Analysis ended sucessfully "<< endl ;
157
d92b41ad 158 }
159 else cout << "Chain was not produced ! "<<endl;
160
161}
162
163void LoadLibraries(const anaModes mode) {
164
165 //--------------------------------------
166 // Load the needed libraries most of them already loaded by aliroot
167 //--------------------------------------
168 gSystem->Load("libTree.so");
169 gSystem->Load("libGeom.so");
170 gSystem->Load("libVMC.so");
171 gSystem->Load("libXMLIO.so");
172
173 //----------------------------------------------------------
174 // >>>>>>>>>>> Local mode <<<<<<<<<<<<<<
175 //----------------------------------------------------------
176 if (mode==mLocal || mode == mLocalCAF || mode == mGRID) {
177 //--------------------------------------------------------
178 // If you want to use already compiled libraries
179 // in the aliroot distribution
180 //--------------------------------------------------------
181 //gSystem->Load("libSTEERBase");
182 //gSystem->Load("libESD");
183 //gSystem->Load("libAOD");
184 //gSystem->Load("libANALYSIS");
185 //gSystem->Load("libANALYSISalice");
3e0577a2 186 //gSystem->Load("libPWG4PartCorrBase");
1e1befe8 187 //gSystem->Load("libPWG4PartCorrDep");
3e0577a2 188
d92b41ad 189 //--------------------------------------------------------
190 //If you want to use root and par files from aliroot
191 //--------------------------------------------------------
192 SetupPar("STEERBase");
193 SetupPar("ESD");
194 SetupPar("AOD");
195 SetupPar("ANALYSIS");
196 SetupPar("ANALYSISalice");
3e0577a2 197 SetupPar("PWG4PartCorrBase");
198 SetupPar("PWG4PartCorrDep");
d92b41ad 199 }
200
201 //---------------------------------------------------------
202 // <<<<<<<<<< PROOF mode >>>>>>>>>>>>
203 //---------------------------------------------------------
204 else if (mode==mPROOF) {
205 //
206 // Connect to proof
207 // Put appropriate username here
208 // TProof::Reset("proof://mgheata@lxb6046.cern.ch");
209 TProof::Open("proof://mgheata@lxb6046.cern.ch");
210
d92b41ad 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");
3e0577a2 223 // Enable PartCorr analysis
224 gProof->UploadPackage("PWG4PartCorrBase.par");
225 gProof->EnablePackage("PWG4PartCorrBase");
226 gProof->UploadPackage("PWG4PartCorrDep.par");
227 gProof->EnablePackage("PWG4PartCorrDep");
d92b41ad 228 //
229 gProof->ShowEnabledPackages();
230 }
231
232}
233
234void SetupPar(char* pararchivename)
235{
236 //Load par files, create analysis libraries
237 //For testing, if par file already decompressed and modified
238 //classes then do not decompress.
239
240 TString cdir(Form("%s", gSystem->WorkingDirectory() )) ;
241 TString parpar(Form("%s.par", pararchivename)) ;
242 if ( gSystem->AccessPathName(parpar.Data()) ) {
243 gSystem->ChangeDirectory(gSystem->Getenv("ALICE_ROOT")) ;
244 TString processline(Form(".! make %s", parpar.Data())) ;
245 gROOT->ProcessLine(processline.Data()) ;
246 gSystem->ChangeDirectory(cdir) ;
1e1befe8 247 //processline = Form(".! mv /tmp/%s .", parpar.Data()) ;
248 processline = Form(".! mv $ALICE_ROOT/%s .", parpar.Data()) ;
d92b41ad 249 gROOT->ProcessLine(processline.Data()) ;
250 }
251 if ( gSystem->AccessPathName(pararchivename) ) {
252 TString processline = Form(".! tar xvzf %s",parpar.Data()) ;
253 gROOT->ProcessLine(processline.Data());
254 }
255
256 TString ocwd = gSystem->WorkingDirectory();
257 gSystem->ChangeDirectory(pararchivename);
258
259 // check for BUILD.sh and execute
260 if (!gSystem->AccessPathName("PROOF-INF/BUILD.sh")) {
261 printf("*******************************\n");
262 printf("*** Building PAR archive ***\n");
263 cout<<pararchivename<<endl;
264 printf("*******************************\n");
265
266 if (gSystem->Exec("PROOF-INF/BUILD.sh")) {
267 Error("runProcess","Cannot Build the PAR Archive! - Abort!");
268 return -1;
269 }
270 }
271 // check for SETUP.C and execute
272 if (!gSystem->AccessPathName("PROOF-INF/SETUP.C")) {
273 printf("*******************************\n");
274 printf("*** Setup PAR archive ***\n");
275 cout<<pararchivename<<endl;
276 printf("*******************************\n");
277 gROOT->Macro("PROOF-INF/SETUP.C");
278 }
279
280 gSystem->ChangeDirectory(ocwd.Data());
281 printf("Current dir: %s\n", ocwd.Data());
282}
283
284
285
286void CreateChain(const anaModes mode, TChain * chain, TChain * chainxs){
287 //Fills chain with data
288 TString ocwd = gSystem->WorkingDirectory();
289
290 //-----------------------------------------------------------
291 //Analysis of CAF data locally and with PROOF
292 //-----------------------------------------------------------
293 if(mode ==mPROOF || mode ==mLocalCAF){
294 // Chain from CAF
295 gROOT->LoadMacro("$ALICE_ROOT/PWG0/CreateESDChain.C");
296 // The second parameter is the number of input files in the chain
297 chain = CreateESDChain("ESD12001.txt", 5);
298 }
299
300 //---------------------------------------
301 //Local files analysis
302 //---------------------------------------
303 else if(mode == mLocal){
304 //If you want to add several ESD files sitting in a common directory INDIR
305 //Specify as environmental variables the directory (INDIR), the number of files
306 //to analyze (NEVENT) and the pattern name of the directories with files (PATTERN)
307
308 if(gSystem->Getenv("INDIR"))
309 kInDir = gSystem->Getenv("INDIR") ;
310 else cout<<"INDIR not set, use default: "<<kInDir<<endl;
311
312 if(gSystem->Getenv("PATTERN"))
313 kPattern = gSystem->Getenv("PATTERN") ;
314 else cout<<"PATTERN not set, use default: "<<kPattern<<endl;
315
316 if(gSystem->Getenv("NEVENT"))
317 kEvent = atoi(gSystem->Getenv("NEVENT")) ;
318 else cout<<"NEVENT not set, use default: "<<kEvent<<endl;
319
320 //Check if env variables are set and are correct
321 if ( kInDir && kEvent) {
322 printf("Get %d files from directory %s\n",kEvent,kInDir);
323 if ( ! gSystem->cd(kInDir) ) {//check if ESDs directory exist
324 printf("%s does not exist\n", kInDir) ;
325 return ;
326 }
327 cout<<"INDIR : "<<kInDir<<endl;
328 cout<<"NEVENT : "<<kEvent<<endl;
329 cout<<"PATTERN: " <<kPattern<<endl;
1e1befe8 330
331 TString datafile="";
332 if(kInputData == "ESD") datafile = "AliESDs.root" ;
333 else if(kInputData == "AOD") datafile = "aod.root" ;
334 else if(kInputData == "MC") datafile = "galice.root" ;
335
d92b41ad 336 //Loop on ESD files, add them to chain
337 Int_t event =0;
338 Int_t skipped=0 ;
339 char file[120] ;
340 char filexs[120] ;
341
342 for (event = 0 ; event < kEvent ; event++) {
1e1befe8 343 sprintf(file, "%s/%s%d/%s", kInDir,kPattern,event,datafile.Data()) ;
d92b41ad 344 sprintf(filexs, "%s/%s%d/%s", kInDir,kPattern,event,kXSFileName) ;
345 TFile * fESD = 0 ;
346 //Check if file exists and add it, if not skip it
347 if ( fESD = TFile::Open(file)) {
1e1befe8 348 if ( fESD->Get(kTreeName) ) {
d92b41ad 349 printf("++++ Adding %s\n", file) ;
350 chain->AddFile(file);
351 chainxs->Add(filexs) ;
352 }
353 }
354 else {
355 printf("---- Skipping %s\n", file) ;
356 skipped++ ;
357 }
358 }
1e1befe8 359 printf("number of entries # %lld, skipped %d\n", chain->GetEntries(), skipped) ;
d92b41ad 360 }
361 else {
362 TString input = "AliESDs.root" ;
363 cout<<">>>>>> No list added, take a single file <<<<<<<<< "<<input<<endl;
364 chain->AddFile(input);
365 }
366
367 }// local files analysis
368
369 //------------------------------
370 //GRID xml files
371 //-----------------------------
372 else if(mode == mGRID){
373 //Get colection file. It is specified by the environmental
374 //variable XML
375
376 if(gSystem->Getenv("XML") )
377 kXML = gSystem->Getenv("XML");
378 else
379 sprintf(kXML, "collection.xml") ;
380
381 if (!TFile::Open(kXML)) {
382 printf("No collection file with name -- %s -- was found\n",kXML);
383 return ;
384 }
385 else cout<<"XML file "<<kXML<<endl;
386
387 //Load necessary libraries and connect to the GRID
388 gSystem->Load("libNetx.so") ;
389 gSystem->Load("libRAliEn.so");
390 TGrid::Connect("alien://") ;
391
392 //Feed Grid with collection file
393 //TGridCollection * collection = (TGridCollection*)gROOT->ProcessLine(Form("TAlienCollection::Open(\"%s\", 0)", kXML));
394 TGridCollection * collection = (TGridCollection*) TAlienCollection::Open(kXML);
395 if (! collection) {
396 AliError(Form("%s not found", kXML)) ;
397 return kFALSE ;
398 }
399 TGridResult* result = collection->GetGridResult("",0 ,0);
400
401 // Makes the ESD chain
402 printf("*** Getting the Chain ***\n");
403 for (Int_t index = 0; index < result->GetEntries(); index++) {
404 TString alienURL = result->GetKey(index, "turl") ;
405 cout << "================== " << alienURL << endl ;
406 chain->Add(alienURL) ;
407 alienURL.ReplaceAll("AliESDs.root",kXSFileName);
408 chainxs->Add(alienURL) ;
409 }
410 }// xml analysis
411
412 gSystem->ChangeDirectory(ocwd.Data());
413}
414
415//________________________________________________
416void GetAverageXsection(TTree * tree, Double_t & xs, Float_t & ntr)
417{
418 // Read the PYTHIA statistics from the file pyxsec.root created by
419 // the function WriteXsection():
420 // integrated cross section (xsection) and
421 // the number of Pyevent() calls (ntrials)
422 // and calculate the weight per one event xsection/ntrials
423 // The spectrum calculated by a user should be
424 // multiplied by this weight, something like this:
425 // TH1F *userSpectrum ... // book and fill the spectrum
426 // userSpectrum->Scale(weight)
427 //
428 // Yuri Kharlov 19 June 2007
429 // Gustavo Conesa 15 April 2008
430 Double_t xsection = 0;
431 UInt_t ntrials = 0;
432 xs = 0;
433 ntr = 0;
434
435 Int_t nfiles = tree->GetEntries() ;
436 if (tree && nfiles > 0) {
437 tree->SetBranchAddress("xsection",&xsection);
438 tree->SetBranchAddress("ntrials",&ntrials);
439 for(Int_t i = 0; i < nfiles; i++){
440 tree->GetEntry(i);
441 xs += xsection ;
442 ntr += ntrials ;
443 cout << "xsection " <<xsection<<" ntrials "<<ntrials<<endl;
444 }
445
446 xs = xs / nfiles;
447 ntr = ntr / nfiles;
448 cout << "-----------------------------------------------------------------"<<endl;
449 cout << "Average of "<< nfiles<<" files: xsection " <<xs<<" ntrials "<<ntr<<endl;
450 cout << "-----------------------------------------------------------------"<<endl;
451 }
452 else cout << " >>>> Empty tree !!!! <<<<< "<<endl;
453
454}
455
456
457