change library dependencies and paths to the new places of the analysis under PWGGA...
[u/mrichter/AliRoot.git] / PWGGA / EMCALTasks / macros / emcalReclusterize.C
CommitLineData
6fdd30c4 1/* $Id: $ */
2//--------------------------------------------------
3// Example macro to do Calorimeters filtering
4// copy ESDs into AODs
5//
6// Pay attention to the options and definitions
7// set in the lines below
8//
9// Author : Gustavo Conesa Balbastre (INFN-LNF)
10//
11//-------------------------------------------------
12enum anaModes {mLocal, mLocalCAF,mPROOF,mGRID};
13//mLocal: Analyze locally files in your computer
14//mLocalCAF: Analyze locally CAF files
15//mPROOF: Analyze CAF files with PROOF
16
17//---------------------------------------------------------------------------
18//Settings to read locally several files, only for "mLocal" mode
19//The different values are default, they can be set with environmental
20//variables: INDIR, PATTERN, NFILES, respectivelly
6060ed91 21char * kInDir = "/Users/Gustavo/Work/data/134908/pass1";
22//char * kInDir = "/Users/Gustavo/Work/data/137366/";
6fdd30c4 23char * kPattern = ""; // Data are in files kInDir/kPattern+i
24Int_t kFile = 1; // Number of files
25//---------------------------------------------------------------------------
26//Collection file for grid analysis
27char * kXML = "collection.xml";
28//---------------------------------------------------------------------------
29
6060ed91 30TString kInputData = "AOD"; //ESD, AOD, MC
6fdd30c4 31TString kTreeName = "esdTree";
32Bool_t kUsePhysSel = kFALSE;
6060ed91 33Bool_t kEmbed = kTRUE;
6fdd30c4 34
35void emcalReclusterize(Int_t mode=mLocal)
36{
37 // Main
6060ed91 38 //char cmd[200] ;
39 //sprintf(cmd, ".! rm -rf outputAOD.root") ;
40 //gROOT->ProcessLine(cmd) ;
6fdd30c4 41 //--------------------------------------------------------------------
42 // Load analysis libraries
43 // Look at the method below,
44 // change whatever you need for your analysis case
45 // ------------------------------------------------------------------
46 LoadLibraries(mode) ;
5994e71f 47
6fdd30c4 48 //-------------------------------------------------------------------------------------------------
49 //Create chain from ESD and from cross sections files, look below for options.
50 //-------------------------------------------------------------------------------------------------
5994e71f 51 if(kInputData == "ESD") kTreeName = "esdTree" ;
6fdd30c4 52 else if(kInputData == "AOD") kTreeName = "aodTree" ;
53 else {
54 cout<<"Wrong data type "<<kInputData<<endl;
55 break;
56 }
5994e71f 57
6fdd30c4 58 TChain *chain = new TChain(kTreeName) ;
59 CreateChain(mode, chain);
5994e71f 60
6fdd30c4 61 if(chain){
62 AliLog::SetGlobalLogLevel(AliLog::kError);//Minimum prints on screen
63
64 //--------------------------------------
65 // Make the analysis manager
66 //-------------------------------------
67 AliAnalysisManager *mgr = new AliAnalysisManager("Manager", "Manager");
5994e71f 68
6fdd30c4 69 // AOD output handler
70 AliAODHandler* aodoutHandler = new AliAODHandler();
6060ed91 71 aodoutHandler->SetOutputFileName("outputAOD.root");
72 if(kEmbed){
73 aodoutHandler->SetCreateNonStandardAOD();
74 kInputData = "AOD";
75 }
6fdd30c4 76 mgr->SetOutputEventHandler(aodoutHandler);
77
6060ed91 78
6fdd30c4 79 //input
80 if(kInputData == "ESD")
5994e71f 81 {
82 // ESD handler
83 AliESDInputHandler *esdHandler = new AliESDInputHandler();
84 mgr->SetInputEventHandler(esdHandler);
85 esdHandler->SetReadFriends(kFALSE);
86 cout<<"ESD handler "<<mgr->GetInputEventHandler()<<endl;
87 }
6fdd30c4 88 if(kInputData == "AOD")
5994e71f 89 {
90 // AOD handler
91 AliAODInputHandler *aodHandler = new AliAODInputHandler();
92 mgr->SetInputEventHandler(aodHandler);
6060ed91 93 if(kEmbed){
94 aodHandler->SetMergeEvents(kTRUE);
95 aodHandler->AddFriend("AliAOD.root");
96 }
97
5994e71f 98 cout<<"AOD handler "<<mgr->GetInputEventHandler()<<endl;
99
100 }
6fdd30c4 101
6060ed91 102 //mgr->SetDebugLevel(1);
6fdd30c4 103
104 //-------------------------------------------------------------------------
105 //Define task, put here any other task that you want to use.
106 //-------------------------------------------------------------------------
5994e71f 107 AliAnalysisDataContainer *cinput1 = mgr->GetCommonInputContainer();
108 AliAnalysisDataContainer *coutput1 = mgr->GetCommonOutputContainer();
6fdd30c4 109
110 // ESD physics selection task
111 if(kInputData == "ESD" && kUsePhysSel){
112 gROOT->LoadMacro("AddTaskPhysicsSelection.C");
113 AliPhysicsSelectionTask* physSelTask = AddTaskPhysicsSelection();
114 }
6fdd30c4 115
5994e71f 116 gROOT->LoadMacro("AddTaskEMCALClusterize.C");
117 AliAnalysisTaskEMCALClusterize * clusterize = AddTaskEMCALClusterize();
6fdd30c4 118
5994e71f 119// AliAnalysisTaskEMCALClusterize * clusterize = new AliAnalysisTaskEMCALClusterize();
120// clusterize->SetConfigFileName("ConfigEMCALClusterize.C");
121// clusterize->SetOCDBPath("local://$ALICE_ROOT/OCDB");
122// mgr->AddTask(clusterize);
123//
124// // Create containers for input/output
125// AliAnalysisDataContainer *cinput1 = mgr->GetCommonInputContainer() ;
126// AliAnalysisDataContainer *coutput1 = mgr->GetCommonOutputContainer() ;
127//
128// mgr->ConnectInput (clusterize, 0, cinput1 );
129// mgr->ConnectOutput (clusterize, 0, coutput1 );
6fdd30c4 130
6fdd30c4 131 //-----------------------
132 // Run the analysis
133 //-----------------------
134 TString smode = "";
135 if (mode==mLocal || mode == mLocalCAF)
136 smode = "local";
137 else if (mode==mPROOF)
138 smode = "proof";
139 else if (mode==mGRID)
140 smode = "local";
141
142 mgr->InitAnalysis();
143 mgr->PrintStatus();
144 mgr->StartAnalysis(smode.Data(),chain);
5994e71f 145
146 cout <<" Analysis ended sucessfully "<< endl ;
147
6fdd30c4 148 }
149 else cout << "Chain was not produced ! "<<endl;
150
151}
152
153void LoadLibraries(const anaModes mode) {
154
155 //--------------------------------------
156 // Load the needed libraries most of them already loaded by aliroot
157 //--------------------------------------
158 gSystem->Load("libTree.so");
159 gSystem->Load("libGeom.so");
160 gSystem->Load("libVMC.so");
161 gSystem->Load("libXMLIO.so");
162 gSystem->Load("libMatrix.so");
163 gSystem->Load("libPhysics.so");
164
165 //----------------------------------------------------------
166 // >>>>>>>>>>> Local mode <<<<<<<<<<<<<<
167 //----------------------------------------------------------
168 if (mode==mLocal || mode == mLocalCAF || mode == mGRID) {
169 //--------------------------------------------------------
170 // If you want to use already compiled libraries
171 // in the aliroot distribution
172 //--------------------------------------------------------
173
174 gSystem->Load("libSTEERBase.so");
175 gSystem->Load("libESD.so");
176 gSystem->Load("libAOD.so");
177 gSystem->Load("libANALYSIS.so");
178 gSystem->Load("libANALYSISalice.so");
179 gSystem->Load("libANALYSISalice.so");
180 gSystem->Load("libEMCALUtils.so");
181
182 //SetupPar("EMCALUtils");
5fdef45d 183 //SetupPar("PWGGAEMCALTasks");
6fdd30c4 184
5994e71f 185 //TGeoManager::Import("geometry.root") ; //need file "geometry.root" in local dir!!!!
5fdef45d 186 gSystem->Load("libPWGGAEMCALTasks.so");
6fdd30c4 187
188 /*
6fdd30c4 189 // gSystem->Load("libCORRFW.so");
40a0a69c 190 // gSystem->Load("libPWGHFbase.so");
191 // gSystem->Load("libPWGmuon.so");
6fdd30c4 192 */
193 //--------------------------------------------------------
194 //If you want to use root and par files from aliroot
195 //--------------------------------------------------------
196 /*
197 SetupPar("STEERBase");
198 SetupPar("ESD");
199 SetupPar("AOD");
200 SetupPar("ANALYSIS");
201 SetupPar("ANALYSISalice");
202 SetupPar("PHOSUtils");
203 SetupPar("EMCALUtils");
204 //Create Geometry
205 TGeoManager::Import("geometry.root") ; //need file "geometry.root" in local dir!!!!
5fdef45d 206 SetupPar("PWGGAEMCALTasks");
6fdd30c4 207*/
208 }
209
210 //---------------------------------------------------------
211 // <<<<<<<<<< PROOF mode >>>>>>>>>>>>
212 //---------------------------------------------------------
213 else if (mode==mPROOF) {
214 //
215 // Connect to proof
216 // Put appropriate username here
217 // TProof::Reset("proof://mgheata@lxb6046.cern.ch");
218 TProof::Open("proof://mgheata@lxb6046.cern.ch");
219
220 // gProof->ClearPackages();
221 // gProof->ClearPackage("ESD");
222 // gProof->ClearPackage("AOD");
223 // gProof->ClearPackage("ANALYSIS");
6fdd30c4 224
225 // Enable the STEERBase Package
226 gProof->UploadPackage("STEERBase.par");
227 gProof->EnablePackage("STEERBase");
228 // Enable the ESD Package
229 gProof->UploadPackage("ESD.par");
230 gProof->EnablePackage("ESD");
231 // Enable the AOD Package
232 gProof->UploadPackage("AOD.par");
233 gProof->EnablePackage("AOD");
234 // Enable the Analysis Package
235 gProof->UploadPackage("ANALYSIS.par");
236 gProof->EnablePackage("ANALYSIS");
237 // Enable the PHOS geometry Package
238 //gProof->UploadPackage("PHOSUtils.par");
239 //gProof->EnablePackage("PHOSUtils");
6fdd30c4 240 gProof->ShowEnabledPackages();
241 }
242
243}
244
245void SetupPar(char* pararchivename)
246{
247 //Load par files, create analysis libraries
248 //For testing, if par file already decompressed and modified
249 //classes then do not decompress.
250
251 TString cdir(Form("%s", gSystem->WorkingDirectory() )) ;
252 TString parpar(Form("%s.par", pararchivename)) ;
253 if ( gSystem->AccessPathName(parpar.Data()) ) {
254 gSystem->ChangeDirectory(gSystem->Getenv("ALICE_ROOT")) ;
255 TString processline(Form(".! make %s", parpar.Data())) ;
256 gROOT->ProcessLine(processline.Data()) ;
257 gSystem->ChangeDirectory(cdir) ;
258 processline = Form(".! mv $ALICE_ROOT/%s .", parpar.Data()) ;
259 gROOT->ProcessLine(processline.Data()) ;
260 }
261 if ( gSystem->AccessPathName(pararchivename) ) {
262 TString processline = Form(".! tar xvzf %s",parpar.Data()) ;
263 gROOT->ProcessLine(processline.Data());
264 }
265
266 TString ocwd = gSystem->WorkingDirectory();
267 gSystem->ChangeDirectory(pararchivename);
268
269 // check for BUILD.sh and execute
270 if (!gSystem->AccessPathName("PROOF-INF/BUILD.sh")) {
271 printf("*******************************\n");
272 printf("*** Building PAR archive ***\n");
273 cout<<pararchivename<<endl;
274 printf("*******************************\n");
275
276 if (gSystem->Exec("PROOF-INF/BUILD.sh")) {
277 Error("runProcess","Cannot Build the PAR Archive! - Abort!");
278 return -1;
279 }
280 }
281 // check for SETUP.C and execute
282 if (!gSystem->AccessPathName("PROOF-INF/SETUP.C")) {
283 printf("*******************************\n");
284 printf("*** Setup PAR archive ***\n");
285 cout<<pararchivename<<endl;
286 printf("*******************************\n");
287 gROOT->Macro("PROOF-INF/SETUP.C");
288 }
289
290 gSystem->ChangeDirectory(ocwd.Data());
291 printf("Current dir: %s\n", ocwd.Data());
292}
293
294
295
296void CreateChain(const anaModes mode, TChain * chain){
297 //Fills chain with data
298 TString ocwd = gSystem->WorkingDirectory();
299
300 //-----------------------------------------------------------
301 //Analysis of CAF data locally and with PROOF
302 //-----------------------------------------------------------
303 if(mode ==mPROOF || mode ==mLocalCAF){
304 // Chain from CAF
305 gROOT->LoadMacro("$ALICE_ROOT/PWG0/CreateESDChain.C");
306 // The second parameter is the number of input files in the chain
307 chain = CreateESDChain("ESD12001.txt", 5);
308 }
309
310 //---------------------------------------
311 //Local files analysis
312 //---------------------------------------
313 else if(mode == mLocal){
314 //If you want to add several ESD files sitting in a common directory INDIR
315 //Specify as environmental variables the directory (INDIR), the number of files
316 //to analyze (NFILES) and the pattern name of the directories with files (PATTERN)
317
318 if(gSystem->Getenv("INDIR"))
319 kInDir = gSystem->Getenv("INDIR") ;
320 else cout<<"INDIR not set, use default: "<<kInDir<<endl;
321
322 if(gSystem->Getenv("PATTERN"))
323 kPattern = gSystem->Getenv("PATTERN") ;
324 else cout<<"PATTERN not set, use default: "<<kPattern<<endl;
325
326 if(gSystem->Getenv("NFILES"))
327 kFile = atoi(gSystem->Getenv("NFILES")) ;
328 else cout<<"NFILES not set, use default: "<<kFile<<endl;
329
330 //Check if env variables are set and are correct
331 if ( kInDir && kFile) {
332 printf("Get %d files from directory %s\n",kFile,kInDir);
333 if ( ! gSystem->cd(kInDir) ) {//check if ESDs directory exist
334 printf("%s does not exist\n", kInDir) ;
335 return ;
336 }
337
338
339 cout<<"INDIR : "<<kInDir<<endl;
340 cout<<"NFILES : "<<kFile<<endl;
341 cout<<"PATTERN : " <<kPattern<<endl;
342
343 TString datafile="";
344 if(kInputData == "ESD") datafile = "AliESDs.root" ;
345 else if(kInputData == "AOD") datafile = "AliAODs.root" ;
346 else if(kInputData == "MC") datafile = "galice.root" ;
347
348 //Loop on ESD files, add them to chain
349 Int_t event =0;
350 Int_t skipped=0 ;
351 char file[120] ;
352
353 for (event = 0 ; event < kFile ; event++) {
354 sprintf(file, "%s/%s%d/%s", kInDir,kPattern,event,datafile.Data()) ;
355 TFile * fESD = 0 ;
356 //Check if file exists and add it, if not skip it
357 if ( fESD = TFile::Open(file)) {
358 if ( fESD->Get(kTreeName) ) {
359 printf("++++ Adding %s\n", file) ;
360 chain->AddFile(file);
361 }
362 }
363 else {
364 printf("---- Skipping %s\n", file) ;
365 skipped++ ;
366 }
367 }
368 printf("number of entries # %lld, skipped %d\n", chain->GetEntries(), skipped*100) ;
369 }
370 else {
371 TString input = "AliESDs.root" ;
372 cout<<">>>>>> No list added, take a single file <<<<<<<<< "<<input<<endl;
373 chain->AddFile(input);
374 }
375
376 }// local files analysis
377
378 //------------------------------
379 //GRID xml files
380 //-----------------------------
381 else if(mode == mGRID){
382 //Get colection file. It is specified by the environmental
383 //variable XML
384
385 if(gSystem->Getenv("XML") )
386 kXML = gSystem->Getenv("XML");
387 else
388 sprintf(kXML, "collection.xml") ;
389
390 if (!TFile::Open(kXML)) {
391 printf("No collection file with name -- %s -- was found\n",kXML);
392 return ;
393 }
394 else cout<<"XML file "<<kXML<<endl;
395
396 //Load necessary libraries and connect to the GRID
397 gSystem->Load("libNetx.so") ;
398 gSystem->Load("libRAliEn.so");
399 TGrid::Connect("alien://") ;
400
401 //Feed Grid with collection file
402 //TGridCollection * collection = (TGridCollection*)gROOT->ProcessLine(Form("TAlienCollection::Open(\"%s\", 0)", kXML));
403 TGridCollection * collection = (TGridCollection*) TAlienCollection::Open(kXML);
404 if (! collection) {
405 AliError(Form("%s not found", kXML)) ;
406 return kFALSE ;
407 }
408 TGridResult* result = collection->GetGridResult("",0 ,0);
409
410 // Makes the ESD chain
411 printf("*** Getting the Chain ***\n");
412 for (Int_t index = 0; index < result->GetEntries(); index++) {
413 TString alienURL = result->GetKey(index, "turl") ;
414 cout << "================== " << alienURL << endl ;
415 chain->Add(alienURL) ;
416 }
417 }// xml analysis
418
419 gSystem->ChangeDirectory(ocwd.Data());
420}
421