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