]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG2/RESONANCES/macros/train/runLocal.C
PWG2/SPECTRA -> PWGLF/SPECTRA migration
[u/mrichter/AliRoot.git] / PWG2 / RESONANCES / macros / train / runLocal.C
1 //
2 // This is an example steering macro for running RSN analysis task
3 // locally with a collection of files specified in a text file:
4 //
5 // Inputs:
6 //   - nReadFiles  = number of files to process from the list
7 //   - nSkipFiles  = how many lines to be skipped when reading the list
8 //   - addTaskName = name of the macro to add the RSN analysis task
9 //                   (assumed to have inside it a function named like the file)
10 //   - inputSource = name of the file containing all the inputs
11 //                   ---> to run on a local collection, the collection file
12 //                        must contain on each line the full path
13 //                        of one input file and it must have the ".txt" extension
14 //                   ---> to run on an AliEn collection, the collection file must be an XML
15 //                        file collection like those built from the "find -x" method in aliensh.
16 //   - options     = a label which is used to know what kind of data are being read
17 //                   (it is propagated to the 'addTask' macro for eventual setting up of something
18 //   - outName     = name for the file with RSN package outputs (without ROOT extension)
19 //
20 // Notes:
21 //   - in case the source is an ESD, and if inputs are a MC production
22 //     the MC input handler is created by default
23 //
24 //
25 // In principle, the user should never modify this macro.
26 //
27 void runLocal
28 (
29    Int_t       nReadFiles      = 0,
30    Int_t       nSkipFiles      = 0,
31    Int_t       nmix            = 0,
32    const char *inputSource     = "pbpb_data.txt",
33    const char *runOptions      = "esd_data_phys_cent",
34    const char *analysisOptions = "tpcpid_tofpid_mult",
35    const char *outName         = "test.root",
36    const char *taskList        = "AddRsnAnalysisTask.C",
37    //const char *taskPath        = "$(ALICE_ROOT)/PWG2/RESONANCES/macros/train"
38    const char *taskPath        = "$(HOME)/code/resonances/alice-rsn-package/PWG2resonances/RESONANCES/macros/test/pulvir"
39 )
40 {
41    //
42    // === PREPARATION ==============================================================================
43    //
44    
45    // this option is not needed when using plugin
46    // gEnv->SetValue("XSec.GSI.DelegProxy","2");
47    
48    // stopwatch
49    TStopwatch timer;
50    timer.Start();
51    
52    // some options
53    TString opt(runOptions);
54    opt.ToUpper();
55    Bool_t useTender = opt.Contains("TENDER");
56    Bool_t isMC      = opt.Contains("MC") || (!opt.Contains("DATA"));
57    Bool_t isESD     = opt.Contains("ESD");
58    ::Info("runPlugin.C", "useTender = %d", useTender);
59    ::Info("runPlugin.C", "isMC      = %d", isMC     );
60    ::Info("runPlugin.C", "runOpts   = %s", runOptions);
61    ::Info("runPlugin.C", "anaOpts   = %s", analysisOptions);
62    
63    // basic configurations
64    gROOT->LoadMacro(Form("%s/AnalysisSetup.C", taskPath));
65    AnalysisSetup(isMC, nmix, runOptions, outName, taskPath);
66    
67    // define tree name
68    Char_t treeName[200];
69    if (isESD) sprintf(treeName, "esdTree"); else sprintf(treeName, "aodTree");
70    
71    //
72    // === BUILD INPUT LIST =========================================================================
73    //
74
75    // check extension of input to distinguish between XML and TXT
76    TString sInput(inputSource);
77    sInput.ToLower();
78    Bool_t isTXT = (!strcmp(sInput(sInput.Length() - 3, 3).Data(), "txt"));
79    cout << "Input = " << (isTXT ? "TXT" : "XML") << endl;
80
81    // if input is XML, connect to AliEn
82    if (!isTXT) TGrid::Connect("alien://");
83
84    // create TChain of input events
85    TChain *analysisChain = 0x0;
86    if (isTXT) analysisChain = CreateChainFromText(inputSource, treeName, nReadFiles, nSkipFiles);
87    else       analysisChain = CreateChainFromXML (inputSource, treeName, nReadFiles, nSkipFiles);
88    if (!analysisChain) {
89       Error("runLocal", "Analysis chain not properly initialized");
90       return;
91    }
92    
93    //
94    // === CONFIGURATION ============================================================================
95    //
96
97    //AliLog::SetGlobalDebugLevel(AliLog::kDebug + 3);
98    //AliLog::SetClassDebugLevel("AliRsnCutESD2010", AliLog::kDebug+3);
99    //AliLog::SetClassDebugLevel("AliRsnCutValue", AliLog::kDebug+3);
100    //AliLog::SetClassDebugLevel("AliRsnCutESDCutMultiplicity", AliLog::kDebug+3);
101    //AliLog::SetClassDebugLevel("AliRsnValue", AliLog::kDebug+3);
102    //AliLog::SetClassDebugLevel("AliRsnCutTrackQuality", AliLog::kDebug+3);
103    //AliLog::SetGlobalDebugLevel(AliLog::kDebug+3);
104    
105    //
106    // === ANALYSIS TASK CREATION AND INCLUSION =====================================================
107    //
108    
109    AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
110    if (!mgr) return;
111    
112    gROOT->LoadMacro(Form("%s/AddRsnAnalysisTask.C", taskPath));
113    AddRsnAnalysisTask(isMC, (nmix > 0), runOptions, analysisOptions);
114
115    // initialize and start analysis
116    if (!mgr->InitAnalysis()) {
117       ::Error("runPlugin.C", "Failed to init analysis");
118       return;
119    }
120    mgr->PrintStatus();
121    if (isTXT) mgr->StartAnalysis("local", analysisChain);
122    else       mgr->StartAnalysis("alien", analysisChain);
123    
124    // final operation
125    // gObjectTable->Print();
126    timer.Stop();
127    timer.Print();
128 }
129
130 //_________________________________________________________________________________________________
131 TChain* CreateChainFromXML
132 (const char *xmlFileName, const char *treeName, Int_t nread, Int_t nskip)
133 {
134 //
135 // Create a TChain with all required files listed into an XML collection.
136 // Necessary to run analysis in AliEn jobs.
137 // ---
138 // Arguments:
139 //  - xmlFileName = input list
140 //  - treeName    = "esdTree" or "aodTree"
141 //  - nread       = how many files to read (0 = all)
142 //  - nskip       = how many files to skip from beginning
143 //
144
145    // if nread argument is 0, it is disabled
146    if (nread == 0) nread = 1000000000;
147
148    // initialize output object
149    TChain *chain = new TChain(treeName);
150
151    // initialize the AliEn collection
152    TAlienCollection *myCollection = TAlienCollection::Open(xmlFileName);
153    if (!myCollection) {
154       Error("CreateChainFromXML", "Cannot create an AliEn collection from %s", xmlFileName);
155       return 0x0;
156    }
157
158    // loop on collection
159    myCollection->Reset();
160    while (myCollection->Next()) {
161       // skip until reached required number of offset
162       if (nskip > 0) {--nskip; continue;}
163
164       // stop if required number of read files is reached
165       // otherwise update the counter
166       if (nread <= 0) break;
167       nread--;
168
169       // recovery file and add it
170       Info("CreateChainFromXML", Form("Adding: %s", myCollection->GetTURL("")));
171       chain->Add(myCollection->GetTURL(""));
172    }
173
174    return chain;
175 }
176
177 //_________________________________________________________________________________________________
178 TChain* CreateChainFromText(const char *fileName, const char *treeName, Int_t nread, Int_t nskip)
179 {
180 //
181 // Create a TChain with all required files listed into a text file.
182 // Necessary to run analysis in local jobs.
183 // ---
184 // Arguments:
185 //  - xmlFileName = input file list
186 //  - treeName    = "esdTree" or "aodTree"
187 //  - nread       = how many files to read (0 = all)
188 //  - nskip       = how many files to skip from beginning
189 //
190
191    // if third argument is 0, it is interpreted
192    // as "read all lines"
193    Bool_t readAll = (nread <= 0);
194
195    // initialize output object
196    TChain* target = new TChain(treeName);
197
198    // open text file
199    ifstream fileIn(fileName);
200
201    // loop on collection
202    TString line;
203    while (fileIn.good()) {
204       fileIn >> line;
205       if (line.IsNull()) continue;
206
207       // skip until reached required number of offset
208       if (nskip > 0) {--nskip; continue;}
209
210       // stop if required number of read files is reached
211       // otherwise update the counter
212       if (!readAll && nread <= 0) break;
213       nread--;
214
215       // add file
216       Info("CreateChainFromText", "Adding '%s'", line.Data());
217       target->Add(line.Data());
218    }
219
220    return target;
221 }