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