]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG2/RESONANCES/macros/test/AliRsnAnalysis.C
Modifications in the balance function code + addition of the very first version of...
[u/mrichter/AliRoot.git] / PWG2 / RESONANCES / macros / test / AliRsnAnalysis.C
1 //=============================================================================
2 //
3 // *** AliRsnAnalysis.C ***
4 //
5 // This macro contains all stuff which is required to run
6 // a resonance analysis in whatever environment.
7 // It contains a pool of macroes which do all the steps to set it up
8 // and run it with the appropriate configurations.
9 // All the possibilities are made available through enumerations,
10 // with the same style of the Config.C file used for simulation.
11 //
12 //=============================================================================
13
14 #include <TProof.h>
15 #include <TGrid.h>
16
17 enum Rsn_DataSource
18 {
19   kTextFile,        // text file with paths of all used files (local analysis)
20   kXMLCollection,   // XML collection of files (local/AliEn analysis)
21   kDataset          // CAF dataset
22 };
23
24 enum Rsn_Environment
25 {
26   kLocal,   // local analysis with a locally defined TXT list of files
27   kAlien,   // AliEn analysis with an XML collection
28   kProof    // CAF analysis with a DataSet
29 };
30
31 static Bool_t            cleanPars = kFALSE;
32 static Bool_t            cleanPWG2resonances = kFALSE;
33 static const char*       pathPar = "/home/pulvir/ALICE/ALIROOT/par-files/afs_proof";
34 static const char*       listPar = "STEERBase:ESD:AOD:ANALYSIS:ANALYSISalice:PWG2resonances";
35 static const char*       proofConnection = "localhost";
36
37 // Uncomment these lines for an example run with a local list of local files
38 //static const char*       inputSource = "local.txt";
39 //static Rsn_DataSource    listType = kTextFile;
40 //static Rsn_Environment   environment = kLocal;
41
42 // Uncomment these lines for an example run with PROOF
43 static const char*       inputSource = "/COMMON/COMMON/LHC08c11_10TeV_0.5T";
44 static Rsn_DataSource    listType    = kDataset;
45 static Rsn_Environment   environment = kProof;
46
47 // Uncomment these lines for an example run with AliEn XML collection
48 //static const char*       inputSource = "wn.xml";
49 //static Rsn_DataSource    listType    = kXMLCollection;
50 //static Rsn_Environment   environment = kAlien;
51
52 static Int_t             nReadFiles = 5;
53 static Int_t             nSkippedFiles = 0;
54 static TString           treeName = "esdTree";
55
56 //_________________________________________________________________________________________________
57 Bool_t IsProof()
58 {
59 //
60 // Check if the environment is PROOF
61 //
62   return (environment == kProof);
63 }
64
65 //_________________________________________________________________________________________________
66 Bool_t IsAlien()
67 {
68 //
69 // Check if the environment is Alien
70 //
71   return (environment == kAlien);
72 }
73
74 //_________________________________________________________________________________________________
75 Bool_t CleanPars(const char *pars)
76 {
77 //
78 // Cleans existing PAR library directories.
79 // The string argument must contain their names
80 // separated by colons (':').
81 // Returns kTRUE if everything went well, otherwise returns kFALSE.
82 //
83
84   TString list(pars);
85   TObjArray* array = list.Tokenize(":");
86   TObjString *ostr;
87   TString str;
88
89   for (Int_t i = 0; i < array->GetEntriesFast(); i++)
90   {
91     ostr = (TObjString *) array->At(i);
92     str = ostr->GetString();
93     Info("", ">> Cleaning PARs: %s", str.Data());
94     if (IsProof()) {
95       if (!gProof) {
96         Error("CleanPars", "gProof object not initialized");
97         return kFALSE;
98       }
99       gProof->ClearPackage(Form("%s.par", str.Data()));
100     }
101     else {
102       gSystem->Exec(Form("rm -Rf %s/", str.Data()));
103     }
104   }
105
106   return kTRUE;
107 }
108
109 //_________________________________________________________________________________________________
110 Bool_t LoadPars(TString pars)
111 {
112 //
113 // Loads required PAR libraries.
114 // The string argument must contain their names
115 // separated by colons (':').
116 // Returns kTRUE if everything went well, otherwise returns kFALSE.
117 //
118
119   TObjArray *array = pars.Tokenize(":");
120   TObjString *ostr;
121   TString str;
122
123   for (Int_t i = 0; i < array->GetEntriesFast(); i++) {
124     ostr = (TObjString *) array->At(i);
125     str = ostr->GetString();
126     Info("", ">> Creating PARs: %s", str.Data());
127     if (IsProof()) {
128        if (!gProof) {
129         Error("CleanPars", "gProof object not initialized");
130         return kFALSE;
131       }
132       if (!str.CompareTo("PWG2resonances")) {
133         gProof->UploadPackage(Form("%s.par", str.Data()));
134       }
135       else {
136           gProof->UploadPackage(Form("%s/%s.par", pathPar, str.Data()));
137       }
138       gProof->EnablePackage(str.Data());
139     }
140     else {
141       // check that file exists...
142       if (gSystem->AccessPathName(Form("%s.par", str.Data()))) {
143         Error("", " !!! File not found");
144         return kFALSE;
145       }
146       // ...explode tar-ball...
147       gROOT->ProcessLine(Form(".! tar xzf %s.par", str.Data()));
148       // ...go to unpacked directory of PAR library...
149       TString ocwd = gSystem->WorkingDirectory();
150       gSystem->ChangeDirectory(Form("%s", str.Data()));
151       // ...checks for BUILD.sh and execute it...
152       if (!gSystem->AccessPathName("PROOF-INF/BUILD.sh")) {
153         if (gSystem->Exec("PROOF-INF/BUILD.sh")) {
154           Error("", " !!! Build error");
155           return kFALSE;
156         }
157       }
158       // ...check and execute SETUP.C...
159       if (!gSystem->AccessPathName("PROOF-INF/SETUP.C")) {
160         if (gROOT->Macro("PROOF-INF/SETUP.C")) {
161           Error("", " !!! Set-up error");
162           return kFALSE;
163         }
164       }
165       // ...return to main dir and finish
166       gSystem->ChangeDirectory(ocwd);
167     }
168   }
169
170   return kTRUE;
171 }
172
173 //_________________________________________________________________________________________________
174 TChain* CreateChainFromTXT()
175 {
176 //
177 // Create a TChain of files to be analyzed from a text file.
178 // This text file must contain in each line the FULL PATH of an analyzed file.
179 // Last argument specifies the TTree name.
180 //
181
182   TChain *chain = new TChain(treeName.Data());
183
184   ifstream fileIn(inputSource);
185   Int_t count = 0, skip = nSkippedFiles;
186
187   TString line;
188   while (fileIn.good()) {
189     fileIn >> line;
190     if (line.IsNull()) continue;
191     if (skip > 0) {
192       --skip;
193       continue;
194     }
195     if (nReadFiles > 0 && count++ >= nReadFiles) break;
196     chain->Add(Form("%s", line.Data()));
197   }
198   fileIn.close();
199
200   return chain;
201 }
202
203 //_________________________________________________________________________________________________
204 TChain* CreateChainFromXML()
205 {
206 //
207 // Create a TChain of files to be analyzed from a XML file.
208 // Last argument specifies the TTree name.
209 //
210
211   if (!gGrid) {
212     Error("CreateChainFromXML", "gGrid object not instantiated");
213     return 0;
214   }
215
216   TChain *chain = new TChain(treeName.Data());
217   TAlienCollection *myCollection = TAlienCollection::Open(inputSource);
218   if (!myCollection)
219   {
220     Error("CreateChainFromXML", "Cannot create an AliEn collection from %s", collectionFile);
221     return 0x0;
222   }
223
224   // initialize a counter to check the number of read files
225   Int_t nfiles = 0, skip = nSkippedFiles;
226   TString filename;
227   myCollection->Reset();
228   while (myCollection->Next())
229   {
230     if (skip > 0) { --skip; continue; }
231     if (nReadFiles > 0 && nfiles >= nReadFiles) break;
232     // char fileName[255];
233     // sprintf(fileName, "%s", myCollection->GetTURL(""));
234     filename = myCollection->GetTURL("");
235     chain->Add(filename.Data());
236     nfiles++;
237   }
238
239   return chain;
240 }
241
242 //_________________________________________________________________________________________________
243 TChain* CreateChainFromDataset()
244 {
245 //
246 // Create a TChain of files to be analyzed from a CAF dataset.
247 // Last argument specifies the TTree name.
248 //
249
250   if (!gProof) {
251     Error("CreateChainFromDataset", "gProof object not initialized");
252     return 0;
253   }
254
255   TFileCollection *fc = gProof->GetDataSet(inputSource)->GetStagedSubset();
256   TIter iter(fc->GetList());
257
258   TChain* target = new TChain(treeName.Data());
259
260   TFileInfo* fileInfo = 0;
261   Int_t nfiles = 0, skip = nSkippedFiles;
262   while ((fileInfo = dynamic_cast<TFileInfo*> (iter()))) {
263     if (fileInfo->GetFirstUrl()) {
264       if (skip > 0) { --skip; continue; }
265       if (nReadFiles > 0 && nfiles >= nReadFiles) break;
266       target->Add(fileInfo->GetFirstUrl()->GetUrl());
267       nfiles++;
268     }
269   }
270
271   return target;
272 }
273
274 //_________________________________________________________________________________________________
275 Int_t AliRsnAnalysis(const char *addMacro = "AddRsnAnalysisTask.C")
276 {
277 //
278 // Main macro (named after the macro filename).
279 // It initializes the job, creates the AnalysisManager and calls the config macro which
280 // fills this AnalysisManager with all AnalysisTask objects for resonance analysis.
281 // ---
282 // The argument is the name of the macro used for configuration
283 // ---
284 // Return values:
285 // - 0 = successful execution
286 // - 1 = problem while cleanind PAR libraries
287 // - 2 = problem while loading PAR libraries
288 // - 3 = invalid TTree name (allowed: esdTree, aodTree)
289 // - 4 = data source not found
290 // - 5 = wrong definition of source type (allowed any member of enum Rsn_DataSource)
291 // - 6 = TChain initialization returned a NULL object
292 // - 7 = error while initializing AliAnalysisManager object
293 //
294
295   // connect to PROOF if required
296   if (IsProof()) TProof::Open(proofConnection);
297   else if (IsAlien()) TGrid::Connect("alien://");
298
299   //
300   // *** SETUP PAR LIBRARIES **********************************************************************
301   //  - libraries are cleaned if requested
302   //
303
304   if (cleanPars) {
305     if (!CleanPars(listPar)) {
306       Error("AliRsnAnalysis", "Error while cleaning PAR libraries");
307       return 1;
308     }
309   }
310   if (cleanPWG2resonances) CleanPars("PWG2resonances");
311   if (!LoadPars(listPar)) {
312     Error("AliRsnAnalysis", "Error while initializing PAR libraries");
313     return 2;
314   }
315
316   // set up log level
317   AliLog::SetGlobalLogLevel(AliLog::kError);
318
319   // *** CREATE TChain OF INPUT EVENTS ************************************************************
320
321   // preliminary check #1: is the tree name OK?
322   if (treeName != "esdTree" && treeName != "aodTree") {
323     Error("AliRsnAnalysis", "Invalid tree name specified");
324     return 3;
325   }
326
327   // preliminary check #2: does the input source exist?
328   // (valid only for local files and XML collections)
329   if (listType == kTextFile || listType == kXMLCollection) {
330     Long_t id, size, flags, modtime;
331     if (gSystem->GetPathInfo(inputSource, &id, &size, &flags, &modtime)) {
332       Error("AliRsnAnalysis", "Input source not found");
333       return 4;
334     }
335   }
336
337   // create TChain
338   TChain *analysisChain = 0;
339   switch (listType) {
340     case kTextFile:
341       analysisChain = CreateChainFromTXT();
342       break;
343     case kXMLCollection:
344       analysisChain = CreateChainFromXML();
345       break;
346     case kDataset:
347       analysisChain = CreateChainFromDataset();
348       break;
349     default:
350       Error("AliRsnAnalysis", "Input type not supported");
351       return 5;
352   }
353   if (!analysisChain) {
354     Error("AliRsnAnalysis", "Analysis TChain not properly initialized");
355     return 6;
356   }
357
358   //
359   // *** INITIALIZE ANALYSIS MANAGER **************************************************************
360   //
361
362   AliAnalysisManager *mgr = new AliAnalysisManager("ResonanceAnalysis");
363   if (!mgr) {
364     Error("AliRsnAnalysis", "Error occurred while initializing AnalysisManager");
365     return 7;
366   }
367
368   // set ESD and MC input handlers
369   AliESDInputHandler *esdHandler = new AliESDInputHandler();
370   AliAODInputHandler *aodHandler = new AliAODInputHandler();
371   AliMCEventHandler* mcHandler = new AliMCEventHandler();
372   esdHandler->SetInactiveBranches("FMD CaloCluster");
373   if (!treeName.CompareTo("esdTree")) {
374     mgr->SetInputEventHandler(esdHandler);
375   }
376   else if (!treeName.CompareTo("aodTree")) {
377     mgr->SetInputEventHandler(aodHandler);
378   }
379   else {
380     Error("AliRsnAnalysis", "Input tree type not supported");
381     return 8;
382   }
383   mgr->SetMCtruthEventHandler(mcHandler); 
384
385   // load configuration macro and uses it to initialize this object
386   gROOT->LoadMacro(addMacro);
387   AddRsnAnalysisTask();
388
389   // initialize analysis and run it
390   TString strEnv = (IsProof() ? "proof" : "local");
391   TStopwatch timer;
392   timer.Start();
393   mgr->InitAnalysis();
394   mgr->StartAnalysis(strEnv.Data(), analysisChain);
395   timer.Stop();
396   timer.Print();
397
398   return 0;
399 }