]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGGA/CaloTrackCorrelations/macros/anaGenKine.C
move prints to AliDebug; fix coverity 24174
[u/mrichter/AliRoot.git] / PWGGA / CaloTrackCorrelations / macros / anaGenKine.C
1 /* $Id:  $ */
2 //--------------------------------------------------
3 // Example macro to do analysis with the 
4 // analysis classes in CaloTrackCorrelations
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 //-------------------------------------------------
13 enum anaModes {mLocal=0, mPROOF=1, mPlugin=2, mGRID=3};
14 //mLocal    = 0: Analyze locally files in your computer
15 //mPROOF    = 1: Analyze files on GRID with Plugin
16 //mPlugin   = 2: Analyze files on GRID with Plugin
17 //mGRID     = 3: Analyze files on GRID, jobs launched from aliensh
18
19 //---------------------------------------------------------------------------
20 // Settings to read locally several files, only for "mLocal" mode
21 // The different values are default, they can be set with environmental 
22 // variables: INDIR, PATTERN, NFILES, respectivelly
23
24 char * kInDir   = "/user/data/files/"; 
25 char * kPattern = ""; // Data are in files kInDir/kPattern+i 
26 Int_t  kFile    = 6; 
27
28 //---------------------------------------------------------------------------
29 // Dataset for proof analysis, mode=mPROOF
30 // char * kDataset = "/alice/vernet/PbPb_LHC10h_ESD";
31
32 char *  kDatasetPROOF     = "/alice/vernet/LHC11b_149646";
33 Int_t   kDatasetNMaxFiles = 20;
34 TString ccin2p3UserName   = "arbor" ;
35 TString alienUserName     = "narbor" ;
36
37 //---------------------------------------------------------------------------
38 // Collection file for grid analysis
39
40 char * kXML = "collection.xml";
41
42 //---------------------------------------------------------------------------
43 //Scale histograms from file. Change to kTRUE when xsection file exists
44 //Put name of file containing xsection 
45 //Put number of events per ESD file
46 //This is an specific case for normalization of Pythia files.
47 const char * kXSFileName = "pyxsec.root";
48
49 //---------------------------------------------------------------------------
50
51 //Set some default values, but used values are set in the code!
52
53 Bool_t  kMC        = kFALSE; //With real data kMC = kFALSE
54 TString kInputData = "ESD"; //ESD, AOD, MC, deltaAOD
55 Int_t   kYear      = 2011;
56 TString kCollision = "pp";
57 Bool_t  outAOD     = kFALSE; //Some tasks doesnt need it.
58 TString kTreeName;
59 TString kPass      = "";
60 char    kTrigger[1024];
61 Int_t   kRun       = 0;
62
63 //________________________
64 void anaGenKine(Int_t mode=mGRID)
65 {
66   // Main
67   
68   //--------------------------------------------------------------------
69   // Load analysis libraries
70   // Look at the method below, 
71   // change whatever you need for your analysis case
72   // ------------------------------------------------------------------
73   
74   LoadLibraries(mode) ;
75   //gSystem->ListLibraries();
76   
77   //-------------------------------------------------------------------------------------------------
78   //Create chain from ESD and from cross sections files, look below for options.
79   //-------------------------------------------------------------------------------------------------
80   
81   // Set kInputData and kTreeName looking to the kINDIR
82   
83   //CheckInputData(mode);
84   
85   // Check global analysis settings  
86   
87   //CheckEnvironmentVariables();
88   
89   //printf("*********************************************\n");
90   //printf("*** Input data < %s >, pass %s, tree < %s >, MC?  < %d > ***\n",kInputData.Data(),kPass.Data(),kTreeName.Data(),kMC);
91   
92   //printf("*********************************************\n");
93   kTreeName  = "TE";
94   kInputData = "MC";
95   TChain * chain   = new TChain(kTreeName) ;
96   TChain * chainxs = new TChain("Xsection") ;
97   
98   chain  ->AddFile("galice.root");
99   chainxs->AddFile("pyxsec.root");
100
101   //CreateChain(mode, chain, chainxs); 
102   
103   Double_t scale = -1;
104   printf("===== kMC %d, chainxs %p\n",kMC,chainxs);
105   if(chainxs && chainxs->GetEntries() > 0)
106   {
107     Int_t nfiles = chainxs->GetEntries();
108     
109     //Get the cross section
110     Double_t xsection = 0; 
111     Float_t ntrials   = 0;
112     
113     GetAverageXsection(chainxs, xsection, ntrials);
114     
115     Int_t    nEventsPerFile = chain->GetEntries() / nfiles;
116     
117     Double_t trials = ntrials / nEventsPerFile ;
118     
119     scale = xsection/trials;
120     
121     printf("Get Cross section : nfiles  %d, nevents %d, nevents per file %d \n",nfiles, chain->GetEntries(),nEventsPerFile);     
122     printf("                    ntrials %d, trials %2.2f, xs %2.2e, scale factor %2.2e\n", ntrials,trials,xsection,scale);
123     
124   } 
125   
126   printf("*********************************************\n");
127   printf("number of entries # %lld, skipped %d\n", chain->GetEntries()) ;       
128   printf("*********************************************\n");
129   
130   if(!chain)
131   { 
132     printf("STOP, no chain available\n"); 
133     return;
134   }
135   
136   AliLog::SetGlobalLogLevel(AliLog::kError);//Minimum prints on screen
137   
138   //------------------------------------------
139   //  Alien handler part
140   //------------------------------------------
141   AliAnalysisGrid *alienHandler=0x0;
142   if(mode==mPlugin)
143   {
144     // Create and configure the alien handler plugin
145     gROOT->LoadMacro("CreateAlienHandler.C");
146     alienHandler = CreateAlienHandler();
147     if (!alienHandler) return;
148   }  
149   
150   //--------------------------------------
151   // Make the analysis manager
152   //-------------------------------------
153   AliAnalysisManager *mgr  = new AliAnalysisManager("Manager", "Manager");
154   //AliAnalysisManager::SetUseProgressBar(kTRUE);
155   //mgr->SetSkipTerminate(kTRUE);
156   //mgr->SetNSysInfo(1);
157   
158 //  if(mode==mPlugin)
159 //  {
160 //    // Connect plugin to the analysis manager
161 //    mgr->SetGridHandler(alienHandler);
162 //  }
163   
164   // MC handler
165   if((kMC || kInputData == "MC") && !kInputData.Contains("AOD"))
166   {
167     AliMCEventHandler* mcHandler = new AliMCEventHandler();
168     mcHandler->SetReadTR(kFALSE);//Do not search TrackRef file
169     mgr->SetMCtruthEventHandler(mcHandler);
170     if( kInputData == "MC") 
171     {
172       cout<<"MC INPUT EVENT HANDLER"<<endl;
173       mgr->SetInputEventHandler(NULL);
174     }
175   }
176   
177   
178 //  // AOD output handler
179 //  if(kInputData!="deltaAOD" && outAOD)
180 //  {
181 //    cout<<"Init output handler"<<endl;
182 //    AliAODHandler* aodoutHandler   = new AliAODHandler();
183 //    aodoutHandler->SetOutputFileName("aod.root");
184 //    ////aodoutHandler->SetCreateNonStandardAOD();
185 //    mgr->SetOutputEventHandler(aodoutHandler);
186 //  }
187 //  
188 //  //input
189 //  
190 //  if(kInputData == "ESD")
191 //  {
192 //    // ESD handler
193 //    AliESDInputHandler *esdHandler = new AliESDInputHandler();
194 //    esdHandler->SetReadFriends(kFALSE);
195 //    mgr->SetInputEventHandler(esdHandler);
196 //    cout<<"ESD handler "<<mgr->GetInputEventHandler()<<endl;
197 //  }
198 //  else if(kInputData.Contains("AOD"))
199 //  {
200 //    // AOD handler
201 //    AliAODInputHandler *aodHandler = new AliAODInputHandler();
202 //    mgr->SetInputEventHandler(aodHandler);
203 //    if(kInputData == "deltaAOD") aodHandler->AddFriend("deltaAODCaloTrackCorr.root");
204 //    cout<<"AOD handler "<<mgr->GetInputEventHandler()<<endl;
205 //  }
206   
207   //mgr->RegisterExternalFile("deltaAODCaloTrackCorr.root");
208   mgr->SetDebugLevel(1); // For debugging, do not uncomment if you want no messages.
209   
210   TString outputFile = AliAnalysisManager::GetCommonFileName(); 
211    
212   gROOT->LoadMacro("AddTaskGenKine.C");  
213   
214  
215   AliAnalysisTaskCaloTrackCorrelation *ana   = AddTaskGenKine(outputFile, scale);
216     
217   
218   //-----------------------
219   // Run the analysis
220   //-----------------------    
221   mgr->InitAnalysis();
222   mgr->PrintStatus();
223   
224   if      (mode == mPlugin) mgr->StartAnalysis("grid");
225   else if (mode == mPROOF ) mgr->StartAnalysis("proof",chain);
226   else                      mgr->StartAnalysis("local",chain);
227   
228   cout <<" Analysis ended sucessfully "<< endl ;
229   
230 }
231
232 //_____________________________
233 void  LoadLibraries(Int_t mode)
234 {
235   
236   if (mode == mPROOF) {
237     //TProof::Mgr("ccalpmaster")->SetROOTVersion("ALICE_v5-27-06b");
238     gROOT->LoadMacro("/afs/in2p3.fr/group/alice/laf/EnableAliRootForLAF.C");
239     TProof* proof = EnableAliRootForLAF("ccaplmaster",nPROOFWorkers.Data(),ccin2p3UserName.Data(),alienUserName.Data(),"",kFALSE,kTRUE,kTRUE,"OADB:ANALYSIS:ANALYSISalice:AOD:ESD:CORRFW:STEERBase:EMCALUtils:PHOSUtils:PWGCaloTrackCorrBase:PWGGACaloTrackCorrelations");
240     
241     //  TProof* proof = TProof::Open("ccaplmaster",Form("workers=%s",nPROOFWorkers.Data()));
242     
243     //     //proof->ClearPackages();
244     //     proof->UploadPackage("STEERBase");
245     //     proof->UploadPackage("ESD");
246     //     proof->UploadPackage("AOD");
247     //     proof->UploadPackage("ANALYSIS");
248     //     proof->UploadPackage("OADB");
249     //     proof->UploadPackage("ANALYSISalice");
250     //     proof->UploadPackage("CORRFW");
251     //     //proof->UploadPackage("JETAN");
252     //     proof->UploadPackage("PHOSUtils");
253     //     proof->UploadPackage("EMCALUtils");
254     //     proof->UploadPackage("PWGCaloTrackCorrBase");
255     //     proof->UploadPackage("PWGGACaloTrackCorrelations");
256
257     //     proof->EnablePackage("STEERBase");
258     //     proof->EnablePackage("ESD");
259     //     proof->EnablePackage("AOD");
260     //     proof->EnablePackage("ANALYSIS");
261     //     proof->EnablePackage("OADB");
262     //     proof->EnablePackage("ANALYSISalice");
263     //     proof->EnablePackage("CORRFW");
264     //     //proof->EnablePackage("JETAN");
265     //     proof->EnablePackage("PHOSUtils");
266     //     proof->EnablePackage("EMCALUtils");
267     //     proof->EnablePackage("PWGCaloTrackCorrBase");
268     //     proof->EnablePackage("PWGGACaloTrackCorrelations");
269
270     return;
271   }  
272   
273   //--------------------------------------
274   // Load the needed libraries most of them already loaded by aliroot
275   //--------------------------------------
276   gSystem->Load("libTree.so");
277   gSystem->Load("libGeom.so");
278   gSystem->Load("libVMC.so");
279   gSystem->Load("libXMLIO.so");
280   gSystem->Load("libMatrix.so");
281   gSystem->Load("libPhysics.so");
282   gSystem->Load("libMinuit.so"); // Root + libraries to if reclusterization is done
283   
284   gSystem->Load("libSTEERBase.so");
285   gSystem->Load("libGui.so"); // Root + libraries to if reclusterization is done
286   gSystem->Load("libCDB.so"); // Root + libraries to if reclusterization is done
287   gSystem->Load("libESD.so"); // Root + libraries to if reclusterization is done
288   gSystem->Load("libAOD.so");
289   gSystem->Load("libRAWDatabase.so"); // Root + libraries to if reclusterization is done
290   gSystem->Load("libProof.so"); 
291   gSystem->Load("libOADB");
292   gSystem->Load("libANALYSIS.so");
293   gSystem->Load("libSTEER.so"); // Root + libraries to if reclusterization is done
294   
295   gSystem->Load("libRAWDatarec.so"); // Root + libraries to if reclusterization is done
296   gSystem->Load("libRAWDatasim.so"); // Root + libraries to if reclusterization is done
297   gSystem->Load("libVZERObase.so");  // Root + libraries to if reclusterization is done
298   gSystem->Load("libVZEROrec.so");   // Root + libraries to if reclusterization is done
299   
300   gSystem->Load("libEMCALUtils");
301   //SetupPar("EMCALUtils");
302   gSystem->Load("libEMCALraw");  // Root + libraries to if reclusterization is done
303   gSystem->Load("libEMCALbase"); // Root + libraries to if reclusterization is done
304   gSystem->Load("libEMCALsim");  // Root + libraries to if reclusterization is done
305   gSystem->Load("libEMCALrec");  // Root + libraries to if reclusterization is done
306   //SetupPar("EMCALraw");
307   //SetupPar("EMCALbase");
308   //SetupPar("EMCALsim");
309   //SetupPar("EMCALrec");
310   
311   gSystem->Load("libANALYSISalice.so");
312   //gSystem->Load("libTENDER.so"); 
313   //gSystem->Load("libTENDERSupplies.so");
314   
315   gSystem->Load("libPHOSUtils");
316   gSystem->Load("libEMCALUtils");
317   gSystem->Load("libPWGCaloTrackCorrBase");
318   gSystem->Load("libPWGGACaloTrackCorrelations");
319   //SetupPar("PWGCaloTrackCorrBase");
320   //SetupPar("PWGGACaloTrackCorrelations");
321   
322  
323   //gSystem->Load("libJETAN");
324   //gSystem->Load("FASTJETAN");
325   //gSystem->Load("PWGJE");
326
327   //gSystem->Load("libCORRFW.so");
328   //gSystem->Load("libPWGGAGammaConv.so"); 
329   //SetupPar("PWGGAGammaConv"); 
330   
331   // needed for plugin?
332   gSystem->AddIncludePath("-I$ALICE_ROOT");
333   gSystem->AddIncludePath("-I./");     
334   
335 }
336
337 //_________________________________
338 void SetupPar(char* pararchivename)
339 {
340   //Load par files, create analysis libraries
341   //For testing, if par file already decompressed and modified
342   //classes then do not decompress.
343   
344   TString cdir(Form("%s", gSystem->WorkingDirectory() )) ; 
345   TString parpar(Form("%s.par", pararchivename)) ; 
346   
347   if ( gSystem->AccessPathName(pararchivename) ) {  
348     TString processline = Form(".! tar xvzf %s",parpar.Data()) ;
349     gROOT->ProcessLine(processline.Data());
350   }
351   
352   TString ocwd = gSystem->WorkingDirectory();
353   gSystem->ChangeDirectory(pararchivename);
354   
355   // check for BUILD.sh and execute
356   if (!gSystem->AccessPathName("PROOF-INF/BUILD.sh")) {
357     printf("*******************************\n");
358     printf("*** Building PAR archive    ***\n");
359     cout<<pararchivename<<endl;
360     printf("*******************************\n");
361     
362     if (gSystem->Exec("PROOF-INF/BUILD.sh")) {
363       Error("runProcess","Cannot Build the PAR Archive! - Abort!");
364       return -1;
365     }
366   }
367   // check for SETUP.C and execute
368   if (!gSystem->AccessPathName("PROOF-INF/SETUP.C")) {
369     printf("*******************************\n");
370     printf("*** Setup PAR archive       ***\n");
371     cout<<pararchivename<<endl;
372     printf("*******************************\n");
373     gROOT->Macro("PROOF-INF/SETUP.C");
374   }
375   
376   gSystem->ChangeDirectory(ocwd.Data());
377   printf("Current dir: %s\n", ocwd.Data());
378 }
379
380 //______________________________________
381 void CheckInputData(const anaModes mode)
382 {
383   //Sets input data and tree
384   
385   TString ocwd = gSystem->WorkingDirectory();
386   
387   //---------------------------------------
388   //Local files analysis
389   //---------------------------------------
390   if(mode == mLocal){    
391     //If you want to add several ESD files sitting in a common directory INDIR
392     //Specify as environmental variables the directory (INDIR), the number of files 
393     //to analyze (NFILES) and the pattern name of the directories with files (PATTERN)
394     
395     if(gSystem->Getenv("INDIR"))  
396       kInDir = gSystem->Getenv("INDIR") ; 
397     else cout<<"INDIR not set, use default: "<<kInDir<<endl;    
398     
399     TString sindir(kInDir);
400     if     (sindir.Contains("pass1")) kPass = "pass1";
401     else if(sindir.Contains("pass2")) kPass = "pass2";
402     else if(sindir.Contains("pass3")) kPass = "pass3";
403     
404     if(gSystem->Getenv("PATTERN"))   
405       kPattern = gSystem->Getenv("PATTERN") ; 
406     else  cout<<"PATTERN not set, use default: "<<kPattern<<endl;
407     
408     cout<<"INDIR   : "<<kInDir<<endl;
409     cout<<"NFILES  : "<<kFile<<endl;
410     
411     char fileE[120] ;   
412     char fileA[120] ;   
413     char fileG[120] ;
414     char fileEm[120] ;   
415     for (Int_t event = 0 ; event < kFile ; event++) {
416       sprintf(fileE,  "%s/%s%d/AliESDs.root",    kInDir,kPattern,event) ; 
417       sprintf(fileA,  "%s/%s%d/AliAOD.root",     kInDir,kPattern,event) ; 
418       sprintf(fileG,  "%s/%s%d/galice.root",     kInDir,kPattern,event) ; 
419       sprintf(fileEm, "%s/%s%d/embededAOD.root", kInDir,kPattern,event) ; 
420       
421       TFile * fESD = TFile::Open(fileE) ; 
422       TFile * fAOD = TFile::Open(fileA) ; 
423       
424       //Check if file exists and add it, if not skip it
425       if (fESD) 
426       {
427         kTreeName  = "esdTree";
428         kInputData = "ESD";
429         TFile * fG = TFile::Open(fileG);
430         if(fG) { kMC = kTRUE; fG->Close();}
431         else     kMC = kFALSE;
432         
433         // Get run number
434         TTree* esdTree = (TTree*)fESD->Get("esdTree");
435         AliESDEvent* esd = new AliESDEvent();
436         esd->ReadFromTree(esdTree);
437         esdTree->GetEvent(0);
438         kRun = esd->GetRunNumber();
439         
440         return;
441       }
442       else if(fAOD)
443       {
444         kTreeName  = "aodTree";
445         kInputData = "AOD";
446         if(((TTree*) fAOD->Get("aodTree"))->GetBranch("mcparticles")) kMC=kTRUE;
447         else kMC = kFALSE;
448         
449         // Get run number
450         TTree* aodTree = (TTree*)fAOD->Get("aodTree");
451         AliAODEvent* aod = new AliAODEvent();
452         aod->ReadFromTree(aodTree);
453         aodTree->GetEvent(0);
454         kRun = aod->GetRunNumber();
455         return;
456       }
457       else if(TFile::Open(fileEm))
458       {
459         kTreeName  = "aodTree";
460         kInputData = "AOD";
461         kMC        = kTRUE;
462         
463         return;
464       }
465       else if(TFile::Open(fileG))
466       {
467         kTreeName  = "TE";
468         kInputData = "MC";
469         kMC        = kTRUE;
470         return;
471       }
472     }
473     
474     if(fESD) fESD->Close();
475     if(fAOD) fAOD->Close();
476     
477   }// local files analysis
478   
479   //------------------------------
480   //GRID xml files
481   //-----------------------------
482   else if(mode == mGRID){
483     //Get colection file. It is specified by the environmental
484     //variable XML
485     
486     if(gSystem->Getenv("XML") )
487       kXML = gSystem->Getenv("XML");
488     else
489       sprintf(kXML, "collection.xml") ; 
490     
491     if (!TFile::Open(kXML)) {
492       printf("No collection file with name -- %s -- was found\n",kXML);
493       return ;
494     }
495     else cout<<"XML file "<<kXML<<endl;
496     
497     //Load necessary libraries and connect to the GRID
498     gSystem->Load("libNetx.so") ; 
499     gSystem->Load("libRAliEn.so"); 
500     TGrid::Connect("alien://") ;
501     
502     //Feed Grid with collection file
503     TGridCollection * collection = (TGridCollection*) TAlienCollection::Open(kXML);
504     if (! collection) {
505       AliError(Form("%s not found", kXML)) ; 
506       return kFALSE ; 
507     }
508     TGridResult* result = collection->GetGridResult("",0 ,0);
509     
510     for (Int_t index = 0; index < result->GetEntries(); index++) {
511       TString alienURL = result->GetKey(index, "turl") ; 
512       cout << "================== " << alienURL << endl ; 
513       
514       if     (alienURL.Contains("pass1")) kPass = "pass1";
515       else if(alienURL.Contains("pass2")) kPass = "pass2";
516       else if(alienURL.Contains("pass3")) kPass = "pass3";
517       
518       kRun = AliAnalysisManager::GetRunFromAlienPath(alienURL.Data());
519       printf("Run number from alien path = %d\n",kRun);
520       
521       TFile * fAOD = 0 ; 
522       //Check if file exists and add it, if not skip it
523       if (alienURL.Contains("AliESDs.root"))  
524       {
525         kTreeName  = "esdTree";
526         kInputData = "ESD";
527         alienURL.ReplaceAll("AliESDs.root","galice.root");
528         if(TFile::Open(alienURL)) kMC=kTRUE;
529         else kMC = kFALSE;
530         return;
531       }
532       else if(alienURL.Contains("AliAOD.root"))
533       {
534         kTreeName  = "aodTree";
535         kInputData = "AOD";
536         fAOD = TFile::Open(alienURL);
537         if(((TTree*) fAOD->Get("aodTree"))->GetBranch("mcparticles")) kMC=kTRUE;
538         else kMC = kFALSE;
539         return;
540       }
541       else if(alienURL.Contains("embededAOD.root"))
542       {
543         kTreeName  = "aodTree";
544         kInputData = "AOD";
545         kMC=kTRUE;
546         return;
547       }
548       else if(alienURL.Contains("galice.root"))
549       {
550         kTreeName  = "TE";
551         kInputData = "MC";
552         kMC=kTRUE;
553         return;
554       } 
555     }
556   }// xml analysis
557   //------------------------------
558   //PROOF files
559   //-----------------------------
560   else if(mode == mPROOF){
561     
562     TFileCollection* coll  = gProof->GetDataSet(kDatasetPROOF)->GetStagedSubset();
563     
564     TIter iter(coll->GetList());
565     
566     TFileInfo* fileInfo = 0;
567     while ((fileInfo = dynamic_cast<TFileInfo*> (iter())))
568     {
569       if (fileInfo->GetFirstUrl()) {
570         TString ProofURL = fileInfo->GetFirstUrl()->GetUrl();
571         cout << "================== " << ProofURL << endl ; 
572         
573         if     (ProofURL.Contains("pass1")) kPass = "pass1";
574         else if(ProofURL.Contains("pass2")) kPass = "pass2";
575         else if(ProofURL.Contains("pass3")) kPass = "pass3";
576         
577         kRun = AliAnalysisManager::GetRunFromAlienPath(ProofURL.Data());
578         printf("Run number from alien path = %d\n",kRun);
579         
580         TFile * fAOD = 0 ; 
581         //Check if file exists and add it, if not skip it
582         if (ProofURL.Contains("AliESDs.root"))  
583         {
584           kTreeName  = "esdTree";
585           kInputData = "ESD";
586           alienURL.ReplaceAll("AliESDs.root","galice.root");
587           if(TFile::Open(ProofURL)) kMC=kTRUE;
588           else kMC = kFALSE;
589           
590           return;
591         }
592         else if(ProofURL.Contains("AliAOD.root"))
593         {
594           kTreeName  = "aodTree";
595           kInputData = "AOD";
596           fAOD = TFile::Open(ProofURL);
597           if(((TTree*) fAOD->Get("aodTree"))->GetBranch("mcparticles")) kMC=kTRUE;
598           else kMC = kFALSE;
599           return;
600         }
601         else if(ProofURL.Contains("embededAOD.root"))
602         {
603           kTreeName  = "aodTree";
604           kInputData = "AOD";
605           kMC=kTRUE;
606           return;
607         }
608         else if(ProofURL.Contains("galice.root"))
609         {
610           kTreeName  = "TE";
611           kInputData = "MC";
612           kMC=kTRUE;
613           return;
614         } 
615       }
616     }
617   }// proof analysis
618   
619   gSystem->ChangeDirectory(ocwd.Data());
620   
621 }
622
623 //_____________________________________________________________________
624 void CreateChain(const anaModes mode, TChain * chain, TChain * chainxs)
625 {
626   //Fills chain with data
627   TString ocwd = gSystem->WorkingDirectory();
628   
629   //---------------------------------------
630   // Local files analysis
631   //---------------------------------------
632   if(mode == mLocal){    
633     //If you want to add several ESD files sitting in a common directory INDIR
634     //Specify as environmental variables the directory (INDIR), the number of files 
635     //to analyze (NFILES) and the pattern name of the directories with files (PATTERN)
636     
637     if(gSystem->Getenv("INDIR"))  
638       kInDir = gSystem->Getenv("INDIR") ; 
639     else cout<<"INDIR not set, use default: "<<kInDir<<endl;    
640     
641     if(gSystem->Getenv("PATTERN"))   
642       kPattern = gSystem->Getenv("PATTERN") ; 
643     else  cout<<"PATTERN not set, use default: "<<kPattern<<endl;
644     
645     if(gSystem->Getenv("NFILES"))
646       kFile = atoi(gSystem->Getenv("NFILES")) ;
647     else cout<<"NFILES not set, use default: "<<kFile<<endl;
648     
649     //Check if env variables are set and are correct
650     if ( kInDir  && kFile) {
651       printf("Get %d files from directory %s\n",kFile,kInDir);
652       if ( ! gSystem->cd(kInDir) ) {//check if ESDs directory exist
653         printf("%s does not exist\n", kInDir) ;
654         return ;
655       }
656       
657       //if(gSystem->Getenv("XSFILE"))  
658       //kXSFileName = gSystem->Getenv("XSFILE") ; 
659       //else cout<<" XS file name not set, use default: "<<kXSFileName<<endl;   
660       char * kGener = gSystem->Getenv("GENER");
661       if(kGener) {
662         cout<<"GENER "<<kGener<<endl;
663         if     (!strcmp(kGener,"PYTHIA")) kXSFileName = "pyxsec.root";
664         else if(!strcmp(kGener,"HERWIG")) kXSFileName = "hexsec.root";
665         else cout<<" UNKNOWN GENER, use default: "<<kXSFileName<<endl;
666       }
667       else cout<<" GENER not set, use default xs file name: "<<kXSFileName<<endl;
668       
669       cout<<"INDIR   : "<<kInDir     <<endl;
670       cout<<"NFILES  : "<<kFile      <<endl;
671       cout<<"PATTERN : "<<kPattern   <<endl;
672       cout<<"XSFILE  : "<<kXSFileName<<endl;
673       
674       TString datafile="";
675       if     (kInputData == "ESD")        datafile = "AliESDs.root" ;
676       else if(kInputData.Contains("AOD")) datafile = "AliAOD.root"  ;
677       else if(kInputData == "MC")         datafile = "galice.root"  ;
678       
679       //Loop on ESD/AOD/MC files, add them to chain
680       Int_t event =0;
681       Int_t skipped=0 ; 
682       char file[120] ;
683       char filexs[120] ;
684       
685       for (event = 0 ; event < kFile ; event++) {
686         sprintf(file,   "%s/%s%d/%s", kInDir,kPattern,event,datafile.Data()) ; 
687         sprintf(filexs, "%s/%s%d/%s", kInDir,kPattern,event,kXSFileName) ; 
688         TFile * fData = 0 ; 
689         //Check if file exists and add it, if not skip it
690         if ( fData = TFile::Open(file)) {
691           if ( fData->Get(kTreeName) ) { 
692             printf("++++ Adding %s\n", file) ;
693             chain->AddFile(file);
694             chainxs->Add(filexs) ; 
695           }
696         }
697         else { 
698           printf("---- Skipping %s\n", file) ;
699           skipped++ ;
700         }
701       }
702     }
703     else {
704       TString input = "AliESDs.root" ;
705       cout<<">>>>>> No list added, take a single file <<<<<<<<< "<<input<<endl;
706       chain->AddFile(input);
707     }
708     
709   }// local files analysis
710   
711   //------------------------------
712   // GRID xml files
713   //------------------------------
714   else if(mode == mGRID){
715     //Get colection file. It is specified by the environmental
716     //variable XML
717     
718     //Feed Grid with collection file
719     TGridCollection * collection = (TGridCollection*) TAlienCollection::Open(kXML);
720     if (! collection) {
721       AliError(Form("%s not found", kXML)) ; 
722       return kFALSE ; 
723     }
724     
725     TGridResult* result = collection->GetGridResult("",0 ,0);
726     
727     // Makes the ESD chain 
728     printf("*** Getting the Chain       ***\n");
729     for (Int_t index = 0; index < result->GetEntries(); index++) {
730       TString alienURL = result->GetKey(index, "turl") ; 
731       cout << "================== " << alienURL << endl ; 
732       chain->Add(alienURL) ; 
733       alienURL.ReplaceAll("AliESDs.root",kXSFileName);
734       chainxs->Add(alienURL) ; 
735     }
736   }// xml analysis
737   
738   //------------------------------
739   // PROOF
740   //------------------------------
741   else if (mode == mPROOF) {
742     
743     TFileCollection* ds= gProof->GetDataSet(kDatasetPROOF)->GetStagedSubset();
744     
745     gROOT->LoadMacro("/afs/in2p3.fr/group/alice/laf/dataset_management/CreateChainFromDataSet.C");
746     chain = CreateChainFromDataSet(ds, kTreeName , kDatasetNMaxFiles);
747     printf("chain has %d entries\n",chain->GetEntries());
748   }
749   
750   gSystem->ChangeDirectory(ocwd.Data());
751   
752 }
753
754 //______________________________
755 void CheckEnvironmentVariables()
756 {
757   
758   sprintf(kTrigger,"");
759   
760   Bool_t bRecalibrate = kFALSE;
761   Bool_t bBadChannel = kFALSE;
762   
763   for (int i=0; i< gApplication->Argc();i++){
764     
765 #ifdef VERBOSEARGS
766     
767     printf("Arg  %d:  %s\n",i,gApplication->Argv(i));
768     
769 #endif
770     
771     TString sRun = "";
772     
773     if (!(strcmp(gApplication->Argv(i),"--trigger")))
774       sprintf(trigger,gApplication->Argv(i+1));
775     
776     if (!(strcmp(gApplication->Argv(i),"--recalibrate")))
777       bRecalibrate = atoi(gApplication->Argv(i+1));
778     
779     if (!(strcmp(gApplication->Argv(i),"--badchannel")))
780       bBadChannel = atoi(gApplication->Argv(i+1));
781     
782     if (!(strcmp(gApplication->Argv(i),"--run"))){
783       sRun = gApplication->Argv(i+1);
784       if(sRun.Contains("LHC10")) {
785         kYear = 2010;
786       }
787       else {
788         if(kRun <=0){
789           kRun = atoi(gApplication->Argv(i+1));
790         }
791         else printf("** Run number already set  to %d, do not set to %d\n",kRun,atoi(gApplication->Argv(i+1)));
792       }//numeric run
793     }//--run available
794     
795   }// args loop
796   
797   if(!sRun.Contains("LHC10")){
798     if     ( kRun < 140000) 
799     {
800       kYear = 2010;
801       if( kRun >= 136851 ) kCollision = "PbPb";
802     }
803     else if( kRun < 170600)
804     {
805       kYear = 2011;
806       if( kRun >= 166500 ) kCollision = "PbPb";
807     }
808     else 
809     {
810       kYear = 2012;
811
812     }
813   }
814   
815   if(kMC) sprintf(kTrigger,"");
816   
817   printf("*********************************************\n");
818   //printf("*** Settings trigger %s, recalibrate %d, remove bad channels %d, year %d, collision %s, run %d ***\n",
819   //       kTrigger,bRecalibrate,bBadChannel, kYear,kCollision.Data(), kRun);
820   printf("*** Settings year %d, collision %s, run %d ***\n",kYear,kCollision.Data(), kRun);
821   printf("*********************************************\n");
822   
823 }
824
825
826 //_________________________________________________________________
827 void GetAverageXsection(TTree * tree, Double_t & xs, Float_t & ntr)
828 {
829   // Read the PYTHIA statistics from the file pyxsec.root created by
830   // the function WriteXsection():
831   // integrated cross section (xsection) and
832   // the  number of Pyevent() calls (ntrials)
833   // and calculate the weight per one event xsection/ntrials
834   // The spectrum calculated by a user should be
835   // multiplied by this weight, something like this:
836   // TH1F *userSpectrum ... // book and fill the spectrum
837   // userSpectrum->Scale(weight)
838   //
839   // Yuri Kharlov 19 June 2007
840   // Gustavo Conesa 15 April 2008
841   Double_t xsection = 0;
842   UInt_t    ntrials = 0;
843   xs = 0;
844   ntr = 0;
845   
846   Int_t nfiles =  tree->GetEntries()  ;
847   if (tree && nfiles > 0) {
848     tree->SetBranchAddress("xsection",&xsection);
849     tree->SetBranchAddress("ntrials" ,&ntrials );
850     for(Int_t i = 0; i < nfiles; i++){
851       tree->GetEntry(i);
852       xs  += xsection ;
853       ntr += ntrials ;
854       cout << "xsection " <<xsection<<" ntrials "<<ntrials<<endl; 
855     }
856     
857     xs =   xs /  nfiles;
858     ntr =  ntr / nfiles;
859     cout << "-----------------------------------------------------------------"<<endl;
860     cout << "Average of "<< nfiles<<" files: xsection " <<xs<<" ntrials "<<ntr<<endl; 
861     cout << "-----------------------------------------------------------------"<<endl;
862   } 
863   else cout << " >>>> Empty tree !!!! <<<<< "<<endl;
864   
865 }
866
867
868