]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGGA/CaloTrackCorrelations/macros/QA/ana.C
TENDER becomes Tender, removing .so
[u/mrichter/AliRoot.git] / PWGGA / CaloTrackCorrelations / macros / QA / ana.C
1 /* $Id:  $ */
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 //-------------------------------------------------
13 enum 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, NFILES, respectivelly
22 char * kInDir = "/user/data/files/"; 
23 char * kPattern = ""; // Data are in files kInDir/kPattern+i 
24 Int_t kFile = 1; // Number of files
25 //---------------------------------------------------------------------------
26 //Collection file for grid analysis
27 char * kXML = "collection.xml";
28
29 const Bool_t kMC = kFALSE; //With real data kMC = kFALSE
30 const TString kInputData = "ESD"; //ESD, AOD, MC
31 TString kTreeName = "esdTree";
32
33 void ana(Int_t mode=mLocal)
34 {
35   // Main
36
37   //--------------------------------------------------------------------
38   // Load analysis libraries
39   // Look at the method below, 
40   // change whatever you need for your analysis case
41   // ------------------------------------------------------------------
42   LoadLibraries(mode) ;
43   //    TGeoManager::Import("geometry.root") ; //need file "geometry.root" in local dir!!!!
44
45   //-------------------------------------------------------------------------------------------------
46   //Create chain from ESD and from cross sections files, look below for options.
47   //-------------------------------------------------------------------------------------------------
48   if(kInputData == "ESD") kTreeName = "esdTree" ;
49   else if(kInputData == "AOD") kTreeName = "aodTree" ;
50   else if (kInputData == "MC") kTreeName = "TE" ;
51   else {
52     cout<<"Wrong  data type "<<kInputData<<endl;
53     break;
54   }
55
56   TChain *chain       = new TChain(kTreeName) ;
57   CreateChain(mode, chain);  
58
59   if(chain){
60     AliLog::SetGlobalLogLevel(AliLog::kError);//Minimum prints on screen
61     
62     //--------------------------------------
63     // Make the analysis manager
64     //-------------------------------------
65     AliAnalysisManager *mgr  = new AliAnalysisManager("Manager", "Manager");
66     // MC handler
67     if((kMC || kInputData == "MC") && kInputData!="AOD"){
68       AliMCEventHandler* mcHandler = new AliMCEventHandler();
69       mcHandler->SetReadTR(kFALSE);//Do not search TrackRef file
70       mgr->SetMCtruthEventHandler(mcHandler);
71       if( kInputData == "MC") mgr->SetInputEventHandler(NULL);
72     }
73     
74     //input
75     if(kInputData == "ESD")
76       {
77        // ESD handler
78        AliESDInputHandler *esdHandler = new AliESDInputHandler();
79        mgr->SetInputEventHandler(esdHandler);
80        cout<<"ESD handler "<<mgr->GetInputEventHandler()<<endl;
81       }
82     if(kInputData == "AOD"){
83       // AOD handler
84       AliAODInputHandler *aodHandler = new AliAODInputHandler();
85       mgr->SetInputEventHandler(aodHandler);
86       cout<<"AOD handler "<<mgr->GetInputEventHandler()<<endl;
87     }
88     
89      //mgr->SetDebugLevel(-1); // For debugging, do not uncomment if you want no messages.
90
91     // AOD output handler, needed for physics selection
92     cout<<"Init output handler"<<endl;
93     AliAODHandler* aodoutHandler   = new AliAODHandler();
94     aodoutHandler->SetOutputFileName("aod.root");
95     ////aodoutHandler->SetCreateNonStandardAOD();
96     mgr->SetOutputEventHandler(aodoutHandler);
97     
98     //-------------------------------------------------------------------------
99     //Define task, put here any other task that you want to use.
100     //-------------------------------------------------------------------------
101     
102     TString outputFile = AliAnalysisManager::GetCommonFileName(); 
103     AliAnalysisDataContainer *cinput1 = mgr->GetCommonInputContainer();
104     
105     if(kInputData=="ESD" && !kMC)
106     {
107       gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskPhysicsSelection.C");
108       AliPhysicsSelectionTask* physSelTask = AddTaskPhysicsSelection();
109     }
110     
111     //Counting events tasks
112     if(!kMC)
113     {
114       AliAnalysisTaskCounter * counterMB = new AliAnalysisTaskCounter("CounterMB");
115       counterMB->SelectCollisionCandidates(AliVEvent::kMB);
116       
117       AliAnalysisDataContainer *coutputMB =
118       mgr->CreateContainer("counterMB", TList::Class(), AliAnalysisManager::kOutputContainer, outputFile.Data());
119       mgr->AddTask(counterMB);
120       mgr->ConnectInput  (counterMB, 0, cinput1  );
121       mgr->ConnectOutput (counterMB, 1, coutputMB);
122       
123       AliAnalysisTaskCounter * counterEMC = new AliAnalysisTaskCounter("CounterEMC");
124       counterEMC->SelectCollisionCandidates(AliVEvent::kEMC7);
125       
126       AliAnalysisDataContainer *coutputEMC =
127       mgr->CreateContainer("counterEMC", TList::Class(), AliAnalysisManager::kOutputContainer,  outputFile.Data());
128       mgr->AddTask(counterEMC);
129       mgr->ConnectInput  (counterEMC, 0, cinput1   );
130       mgr->ConnectOutput (counterEMC, 1, coutputEMC);
131       
132       AliAnalysisTaskCounter * counterINT = new AliAnalysisTaskCounter("CounterINT");
133       counterINT->SelectCollisionCandidates(AliVEvent::kINT7);
134       
135       AliAnalysisDataContainer *coutputINT =
136       mgr->CreateContainer("counterINT7", TList::Class(), AliAnalysisManager::kOutputContainer,  outputFile.Data());
137       mgr->AddTask(counterINT);
138       mgr->ConnectInput  (counterINT, 0, cinput1   );
139       mgr->ConnectOutput (counterINT, 1, coutputINT);
140     }
141     else
142     {
143       AliAnalysisDataContainer *coutput =
144       mgr->CreateContainer("counter", TList::Class(), AliAnalysisManager::kOutputContainer,  outputFile.Data());
145       mgr->AddTask(counter);
146       mgr->ConnectInput  (counter, 0, cinput1);
147       mgr->ConnectOutput (counter, 1, coutput);
148     }
149     
150     // QA task
151     
152     gROOT->LoadMacro("$ALICE_ROOT/PWGGA/CaloTrackCorrelations/macros/QA/AddTaskCalorimeterQA.C");
153     if(!kMC)
154     {
155       AliAnalysisTaskCaloTrackCorrelation *taskQAEMC = AddTaskCalorimeterQA("EMC",kMC,"",2012);
156       taskQAEMC->SelectCollisionCandidates(AliVEvent::kEMC7);
157       AliAnalysisTaskCaloTrackCorrelation *taskQAINT = AddTaskCalorimeterQA("default",kMC,"",2012);
158       taskQAINT->SelectCollisionCandidates(AliVEvent::kINT7);
159     }
160     else
161     {
162       AliAnalysisTaskCaloTrackCorrelation *taskQA = AddTaskCalorimeterQA("default",kMC,"",2012);
163     }
164     
165     //-----------------------
166     // Run the analysis
167     //-----------------------    
168     TString smode = "";
169     if (mode==mLocal || mode == mLocalCAF) 
170       smode = "local";
171     else if (mode==mPROOF) 
172       smode = "proof";
173     else if (mode==mGRID) 
174       smode = "local";
175     
176     mgr->InitAnalysis();
177     mgr->PrintStatus();
178     mgr->StartAnalysis(smode.Data(),chain);
179
180 cout <<" Analysis ended sucessfully "<< endl ;
181
182   }
183   else cout << "Chain was not produced ! "<<endl;
184   
185 }
186
187 void  LoadLibraries(const anaModes mode) {
188   
189   //--------------------------------------
190   // Load the needed libraries most of them already loaded by aliroot
191   //--------------------------------------
192   gSystem->Load("libTree");
193   gSystem->Load("libGeom");
194   gSystem->Load("libVMC");
195   gSystem->Load("libXMLIO");
196   gSystem->Load("libMatrix");
197   gSystem->Load("libPhysics");
198   //----------------------------------------------------------
199   // >>>>>>>>>>> Local mode <<<<<<<<<<<<<< 
200   //----------------------------------------------------------
201   if (mode==mLocal || mode == mLocalCAF || mode == mGRID) {
202     //--------------------------------------------------------
203     // If you want to use already compiled libraries 
204     // in the aliroot distribution
205     //--------------------------------------------------------
206  
207     gSystem->Load("libSTEERBase");
208     gSystem->Load("libProof");
209     gSystem->Load("libOADB");
210     gSystem->Load("libESD");
211     gSystem->Load("libAOD");
212     gSystem->Load("libANALYSIS");
213     gSystem->Load("libANALYSISalice");
214     gSystem->Load("libESDfilter");
215
216     gSystem->Load("libPHOSUtils");
217     gSystem->Load("libEMCALUtils");
218     
219     gSystem->Load("libTender");
220     gSystem->Load("libTenderSupplies");
221     
222     gSystem->Load("libCORRFW");
223     gSystem->Load("libPWGTools");
224     
225     gSystem->Load("libPWGCaloTrackCorrBase");
226     gSystem->Load("libPWGGACaloTrackCorrelations");
227
228  
229     //--------------------------------------------------------
230     //If you want to use root and par files from aliroot
231     //--------------------------------------------------------  
232 //     SetupPar("STEERBase");
233 //     SetupPar("ESD");
234 //     SetupPar("AOD");
235 //     SetupPar("ANALYSIS");
236 //     SetupPar("ANALYSISalice");
237 //     //If your analysis needs PHOS geometry uncomment following lines
238 //     SetupPar("PHOSUtils");
239 //     SetupPar("EMCALUtils");
240 //
241 //     SetupPar("PWGCaloTrackCorrBase");
242 //     SetupPar("PWGGACaloTrackCorrelations");
243
244
245   }
246
247   //---------------------------------------------------------
248   // <<<<<<<<<< PROOF mode >>>>>>>>>>>>
249   //---------------------------------------------------------
250   else if (mode==mPROOF) {
251     //
252     // Connect to proof
253     // Put appropriate username here
254     // TProof::Reset("proof://mgheata@lxb6046.cern.ch"); 
255     TProof::Open("proof://mgheata@lxb6046.cern.ch");
256     
257     //    gProof->ClearPackages();
258     //    gProof->ClearPackage("ESD");
259     //    gProof->ClearPackage("AOD");
260     //    gProof->ClearPackage("ANALYSIS");   
261     //    gProof->ClearPackage("PWG4PartCorrBase");
262     //    gProof->ClearPackage("PWG4PartCorrDep");
263     
264     // Enable the STEERBase Package
265     gProof->UploadPackage("STEERBase.par");
266     gProof->EnablePackage("STEERBase");
267     // Enable the ESD Package
268     gProof->UploadPackage("ESD.par");
269     gProof->EnablePackage("ESD");
270     // Enable the AOD Package
271     gProof->UploadPackage("AOD.par");
272     gProof->EnablePackage("AOD");
273     // Enable the Analysis Package
274     gProof->UploadPackage("ANALYSIS.par");
275     gProof->EnablePackage("ANALYSIS");
276         // Enable the PHOS geometry Package
277     //gProof->UploadPackage("PHOSUtils.par");
278     //gProof->EnablePackage("PHOSUtils");
279     // Enable PartCorr analysis
280     gProof->UploadPackage("PWG4PartCorrBase.par");
281     gProof->EnablePackage("PWG4PartCorrBase");
282         gProof->UploadPackage("PWG4PartCorrDep.par");
283     gProof->EnablePackage("PWG4PartCorrDep");    
284     gProof->ShowEnabledPackages();
285   }  
286   
287 }
288
289 void SetupPar(char* pararchivename)
290 {
291   //Load par files, create analysis libraries
292   //For testing, if par file already decompressed and modified
293   //classes then do not decompress.
294  
295   TString cdir(Form("%s", gSystem->WorkingDirectory() )) ; 
296   TString parpar(Form("%s.par", pararchivename)) ; 
297   if ( gSystem->AccessPathName(parpar.Data()) ) {
298     gSystem->ChangeDirectory(gSystem->Getenv("ALICE_ROOT")) ;
299     TString processline(Form(".! make %s", parpar.Data())) ; 
300     gROOT->ProcessLine(processline.Data()) ;
301     gSystem->ChangeDirectory(cdir) ; 
302     processline = Form(".! mv $ALICE_ROOT/%s .", parpar.Data()) ;
303     gROOT->ProcessLine(processline.Data()) ;
304   } 
305   if ( gSystem->AccessPathName(pararchivename) ) {  
306     TString processline = Form(".! tar xvzf %s",parpar.Data()) ;
307     gROOT->ProcessLine(processline.Data());
308   }
309   
310   TString ocwd = gSystem->WorkingDirectory();
311   gSystem->ChangeDirectory(pararchivename);
312   
313   // check for BUILD.sh and execute
314   if (!gSystem->AccessPathName("PROOF-INF/BUILD.sh")) {
315     printf("*******************************\n");
316     printf("*** Building PAR archive    ***\n");
317     cout<<pararchivename<<endl;
318     printf("*******************************\n");
319     
320     if (gSystem->Exec("PROOF-INF/BUILD.sh")) {
321       Error("runProcess","Cannot Build the PAR Archive! - Abort!");
322       return -1;
323     }
324   }
325   // check for SETUP.C and execute
326   if (!gSystem->AccessPathName("PROOF-INF/SETUP.C")) {
327     printf("*******************************\n");
328     printf("*** Setup PAR archive       ***\n");
329     cout<<pararchivename<<endl;
330     printf("*******************************\n");
331     gROOT->Macro("PROOF-INF/SETUP.C");
332   }
333   
334   gSystem->ChangeDirectory(ocwd.Data());
335   printf("Current dir: %s\n", ocwd.Data());
336 }
337
338
339
340 void CreateChain(const anaModes mode, TChain * chain){
341   //Fills chain with data
342   TString ocwd = gSystem->WorkingDirectory();
343   
344   //-----------------------------------------------------------
345   //Analysis of CAF data locally and with PROOF
346   //-----------------------------------------------------------
347   if(mode ==mPROOF || mode ==mLocalCAF){
348     // Chain from CAF
349     gROOT->LoadMacro("$ALICE_ROOT/PWG0/CreateESDChain.C");
350     // The second parameter is the number of input files in the chain
351     chain = CreateESDChain("ESD12001.txt", 5);  
352   }
353   
354   //---------------------------------------
355   //Local files analysis
356   //---------------------------------------
357   else if(mode == mLocal){    
358     //If you want to add several ESD files sitting in a common directory INDIR
359     //Specify as environmental variables the directory (INDIR), the number of files 
360     //to analyze (NFILES) and the pattern name of the directories with files (PATTERN)
361
362     if(gSystem->Getenv("INDIR"))  
363       kInDir = gSystem->Getenv("INDIR") ; 
364     else cout<<"INDIR not set, use default: "<<kInDir<<endl;    
365     
366     if(gSystem->Getenv("PATTERN"))   
367       kPattern = gSystem->Getenv("PATTERN") ; 
368     else  cout<<"PATTERN not set, use default: "<<kPattern<<endl;
369     
370     if(gSystem->Getenv("NFILES"))
371       kFile = atoi(gSystem->Getenv("NFILES")) ;
372     else cout<<"NFILES not set, use default: "<<kFile<<endl;
373     
374     //Check if env variables are set and are correct
375     if ( kInDir  && kFile) {
376       printf("Get %d files from directory %s\n",kFile,kInDir);
377       if ( ! gSystem->cd(kInDir) ) {//check if ESDs directory exist
378         printf("%s does not exist\n", kInDir) ;
379         return ;
380       }
381
382       cout<<"INDIR   : "<<kInDir<<endl;
383       cout<<"NFILES  : "<<kFile<<endl;
384       cout<<"PATTERN : " <<kPattern<<endl;
385
386       TString datafile="";
387       if(kInputData == "ESD") datafile = "AliESDs.root" ;
388       else if(kInputData == "AOD") datafile = "AliAOD.root" ;
389       
390       //Loop on ESD files, add them to chain
391       Int_t event =0;
392       Int_t skipped=0 ; 
393       char file[120] ;
394       
395       for (event = 0 ; event < kFile ; event++) {
396         sprintf(file, "%s/%s%d/%s", kInDir,kPattern,event,datafile.Data()) ; 
397         TFile * fESD = 0 ; 
398         //Check if file exists and add it, if not skip it
399         if ( fESD = TFile::Open(file)) {
400           if ( fESD->Get(kTreeName) ) { 
401             printf("++++ Adding %s\n", file) ;
402             chain->AddFile(file);
403           }
404         }
405         else { 
406           printf("---- Skipping %s\n", file) ;
407           skipped++ ;
408         }
409       }
410       printf("number of entries # %lld, skipped %d\n", chain->GetEntries(), skipped*100) ;      
411     }
412     else {
413       TString input = "AliESDs.root" ;
414       cout<<">>>>>> No list added, take a single file <<<<<<<<< "<<input<<endl;
415       chain->AddFile(input);
416     }
417     
418   }// local files analysis
419   
420   //------------------------------
421   //GRID xml files
422   //-----------------------------
423   else if(mode == mGRID){
424     //Get colection file. It is specified by the environmental
425     //variable XML
426
427     if(gSystem->Getenv("XML") )
428       kXML = gSystem->Getenv("XML");
429     else
430       sprintf(kXML, "collection.xml") ; 
431     
432     if (!TFile::Open(kXML)) {
433       printf("No collection file with name -- %s -- was found\n",kXML);
434       return ;
435     }
436     else cout<<"XML file "<<kXML<<endl;
437
438     //Load necessary libraries and connect to the GRID
439     gSystem->Load("libNetx") ; 
440     gSystem->Load("libRAliEn"); 
441     TGrid::Connect("alien://") ;
442
443     //Feed Grid with collection file
444     TGridCollection * collection = (TGridCollection*) TAlienCollection::Open(kXML);
445     if (! collection) {
446       AliError(Form("%s not found", kXML)) ; 
447       return kFALSE ; 
448     }
449     TGridResult* result = collection->GetGridResult("",0 ,0);
450    
451     // Makes the ESD chain 
452     printf("*** Getting the Chain       ***\n");
453     for (Int_t index = 0; index < result->GetEntries(); index++) {
454       TString alienURL = result->GetKey(index, "turl") ; 
455       cout << "================== " << alienURL << endl ; 
456       chain->Add(alienURL) ; 
457     }
458   }// xml analysis
459   
460   gSystem->ChangeDirectory(ocwd.Data());
461 }
462