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