]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG1/TRD/AliTRDpwg1Helper.cxx
664d772f4e3c1d8e54feb20d10afb775e835952f
[u/mrichter/AliRoot.git] / PWG1 / TRD / AliTRDpwg1Helper.cxx
1 ////////////////////////////////////////////////////////////////////////////
2 //                                                                        //
3 //  Helper class for PWG1 TRD train                                       //
4 //                                                                        //
5 //  Authors:                                                              //
6 //    Markus Fasel <M.Fasel@gsi.de>                                       //
7 //                                                                        //
8 ////////////////////////////////////////////////////////////////////////////
9
10 #include <Rtypes.h>
11 #include <TError.h>
12 #include <TMath.h>
13 #include <TObjArray.h>
14 #include <TObjString.h>
15 #include <TFileMerger.h>
16 #include <TRandom.h>
17 #include <TString.h>
18 #include <TSystem.h>
19
20 #include <string>
21 #include <cstring>
22 #include <fstream>
23
24 #include "AliTRDpwg1Helper.h"
25
26 const Char_t * AliTRDpwg1Helper::fgkTRDtaskClassName[AliTRDpwg1Helper::kNTRDTASKS] = {
27   "AliTRDcheckESD"
28   ,"AliTRDinfoGen"
29   ,"AliTRDcheckDET"
30   ,"AliTRDefficiency"
31   ,"AliTRDresolution"
32   ,"AliTRDcheckPID"
33   ,"AliTRDv0Monitor"
34   ,"AliTRDcalibration"
35   ,"AliTRDefficiencyMC"
36   ,"AliTRDalignmentTask"
37   ,"AliTRDpidRefMaker"
38   ,"AliTRDclusterResolution"
39   ,"AliTRDmultiplicity"
40 };
41
42 const Char_t * AliTRDpwg1Helper::fgkTRDtaskOpt[AliTRDpwg1Helper::kNTRDTASKS+1] = {
43   "ESD"
44   ,"GEN"
45   ,"DET"
46   ,"EFF"
47   ,"RES"
48   ,"PID"
49   ,"V0"
50   ,"CAL"
51   ,"EFFC"
52   ,"ALGN"
53   ,"PIDR"
54   ,"CLRES"
55   ,"MULT"
56   ,"ALL"
57 };
58
59 //______________________________________________________
60 Bool_t AliTRDpwg1Helper::DoTask(Int_t idx, Int_t map)
61 {
62   return TESTBIT(map, idx);
63 }
64
65 //______________________________________________________
66 Int_t AliTRDpwg1Helper::ParseOptions(Char_t *trd)
67 {
68 // Parse space separated options.
69 // Possible options are:
70 //      "ALL" : [default] all performance (no calibration) tasks
71 // ------- Performance tasks ----------
72 //     "ESD"  : Basic TRD Detector checks on ESD only (no TRD tracks analysed)
73 //     "DET"  : Basic TRD Detector checks
74 //     "RES"  : TRD tracking Resolution
75 //     "EFF"  : TRD Tracking Efficiency
76 //     "PID"  : TRD PID - pion efficiency
77 //     "V0"   : monitor V0 performance for use in TRD PID calibration
78 // ------- Calibration tasks ----------
79 //     "EFFC" : TRD Tracking Efficiency Combined (barrel + stand alone) - only in case of simulations
80 //     "MULT"  : TRD single track selection
81 //     "CLRES": clusters Resolution
82 //     "CAL"  : TRD calibration
83 //     "ALGN" : TRD alignment
84 //     "PIDR" : TRD PID - reference data
85 // ------- SPECIAL OPTIONS -----------
86 //     "NOFR" : Data set does not have AliESDfriends.root
87 //     "NOMC" : Data set does not have Monte Carlo Informations (default have MC),
88
89   Int_t fSteerTask = 0;
90   TObjArray *tasksArray = TString(trd).Tokenize(" ");
91   for(Int_t isel = 0; isel < tasksArray->GetEntriesFast(); isel++){
92     TString s = (dynamic_cast<TObjString *>(tasksArray->UncheckedAt(isel)))->String();
93     if(s.CompareTo("ALL") == 0){
94       for(Int_t itask = 0; itask < kNTRDQATASKS; itask++) SETBIT(fSteerTask, itask);
95       continue;
96     } else if(s.CompareTo("NOMC") == 0 || s.CompareTo("NOFR") == 0){
97       continue; // taken care by special functions
98     } else { 
99       Bool_t foundOpt = kFALSE;  
100       for(Int_t itask = 0; itask < kNTRDTASKS; itask++){
101         if(s.CompareTo(fgkTRDtaskOpt[itask]) != 0) continue;
102         SETBIT(fSteerTask, itask); 
103         if(itask>1) SETBIT(fSteerTask, kInfoGen);
104         foundOpt = kTRUE;
105         break;
106       }
107       if(!foundOpt) Info("ParseOptions()", Form("TRD task %s not implemented (yet).", s.Data()));
108     }
109   }
110   // extra rules for calibration tasks
111   if(TESTBIT(fSteerTask, kCalibration)) SETBIT(fSteerTask, kCheckDET);
112   if(TESTBIT(fSteerTask, kMultiplicity)) SETBIT(fSteerTask, kEfficiency);
113   if(TESTBIT(fSteerTask, kEfficiencyMC)) SETBIT(fSteerTask, kEfficiency);
114   if(TESTBIT(fSteerTask, kClErrParam)) SETBIT(fSteerTask, kResolution);
115   if(TESTBIT(fSteerTask, kAlignment)) SETBIT(fSteerTask, kResolution);
116   if(TESTBIT(fSteerTask, kPIDRefMaker)) SETBIT(fSteerTask, kCheckPID);
117   if(TESTBIT(fSteerTask, kV0Monitor)) SETBIT(fSteerTask, kCheckPID);
118
119   return fSteerTask;
120
121 }
122
123 //______________________________________________________
124 void AliTRDpwg1Helper::MergeProd(const Char_t *mark, const Char_t *files, const Int_t nBatch, Int_t level)
125 {
126 // Recursively merge files named "mark" from list in "files" in groups of "nBatch" files.
127 // parameter "level" is used to index recurent calls of this function.
128
129   Char_t lMERGE[8]; snprintf(lMERGE, 8, "%04d.lst", (Int_t)gRandom->Uniform(9999.));
130   Char_t lPURGE[8]; snprintf(lPURGE, 8, "%04d.lst", (Int_t)gRandom->Uniform(9999.));
131
132   // purge file list
133   std::string filename;
134   std::ifstream file(files);
135   Int_t iline(0);
136   while(getline(file, filename)){
137     if(Int_t(filename.find(mark)) < 0) continue;
138     gSystem->Exec(Form("echo %s >> %s", filename.c_str(), lPURGE));
139     iline++;
140   }
141   Int_t nBatches(TMath::Ceil(Double_t(iline)/nBatch));
142   Info("MergeProd()", Form("Merge %d files in %d batches.", iline, nBatches));
143
144   Int_t first(0);
145   for(Int_t ibatch(0); ibatch<nBatches; ibatch++){
146     first = ibatch*nBatch;
147      if(!gSystem->Exec(Form("aliroot -b -q \'$ALICE_ROOT/PWG1/TRD/macros/mergeBatch.C(\"%s\", \"%s\", %d, %d)\'", mark, lPURGE, nBatch, first))) continue;
148     gSystem->Exec(Form("mv %d_%s merge/%d_%d_%s", first, mark, level, first, mark));
149     gSystem->Exec(Form("echo %s/merge/%d_%d_%s >> %s", gSystem->ExpandPathName("$PWD"), level, first, mark, lMERGE));
150   }
151
152   if(nBatches==1){
153     Info("MergeProd()", "Rename 1 merged file.");
154     gSystem->Exec(Form("mv merge/%d_%d_%s %s", level, first, mark, mark));
155   } else if(nBatches<=nBatch){
156     Info("MergeProd()", Form("Merge %d files in 1 batch.", nBatches));
157     if(!gSystem->Exec(Form("aliroot -b -q \'$ALICE_ROOT/PWG1/TRD/macros/mergeBatch.C(\"%s\", \"%s\", %d, 0, kFALSE)\'", mark, lMERGE, nBatches))) return;
158     gSystem->Exec(Form("mv 0_%s %s", mark, mark));
159   } else {
160     level++;
161     Info("MergeProd()", Form("Merge level %d.", level));
162     MergeProd(mark, lMERGE, nBatch, level);
163   }
164   gSystem->Exec(Form("rm -fv %s %s", lMERGE, lPURGE));
165 }
166
167
168 //______________________________________________________
169 const Char_t* AliTRDpwg1Helper::MergeBatch(const Char_t *mark, const Char_t *files, const Int_t nfiles, const Int_t first, Bool_t kSVN, Bool_t kCLEAR)
170 {
171 // Merge files specified in the file list "files" by the token "mark".
172 // The script will merge "nfiles" files starting from the "first" file.
173 // If the file "svnInfo.log" is found together with the files to be merged it is copied locally
174 // if option "kSVN". The input files are removed from disk if option "kCLEAR".
175 //
176 // On return the name of the merged file is return or NULL in case of failure.
177 //
178   TObjArray arr(nfiles); arr.SetOwner(kTRUE);
179   TFileMerger fFM(kTRUE);
180   fFM.OutputFile(Form("%s/%d_%s",  gSystem->ExpandPathName("$PWD"), first, mark));
181   Int_t iline(0), nbatch(0);
182   std::string filename;
183   std::ifstream file(files);
184   while(getline(file, filename)){
185     if(Int_t(filename.find(mark)) < 0) continue;
186     if(iline<first){
187       iline++;
188       continue;
189     }
190     if(kSVN){ // download SVN info for trending
191       if(gSystem->Exec(Form("if [ ! -f svnInfo.log ]; then cp -v %s/svnInfo.log %s; fi", Dirname(filename.c_str()), gSystem->ExpandPathName("$PWD"))) == 0) kSVN=kFALSE;
192     }
193     Info("MergeBatch()", filename.c_str());
194     if(!fFM.AddFile(filename.c_str())) return NULL;
195     arr.Add(new TObjString(filename.c_str()));
196     nbatch++;
197     if(nbatch==nfiles) break;
198   }
199   if(!nbatch){
200     Warning("MergeBatch()", "NOTHING TO MERGE"); return NULL;
201   } else {
202     Info("MergeBatch()", "MERGING FILES[%d] START[%d] %s ... ", nbatch, first, ((nbatch<nfiles)?"INCOMPLETE":""));
203   }
204   if(!fFM.Merge()) return NULL;
205
206   if(kCLEAR){
207     for(Int_t ifile(0); ifile<arr.GetEntries(); ifile++){
208       gSystem->Exec(Form("rm -fv %s", ((TObjString*)arr.At(ifile))->GetString().Data()));
209     }
210   }
211   return fFM.GetOutputFileName();
212 }
213
214 //______________________________________________________
215 const Char_t* AliTRDpwg1Helper::Basename(const char* filepath)
216 {
217 // Implementation of shell "basename" builtin
218   TString s(filepath);
219   Int_t idx(s.Last('/')+1);
220   s=s(idx, idx+100);
221   return s;
222 }
223
224 //______________________________________________________
225 const Char_t* AliTRDpwg1Helper::Dirname(const char* filepath)
226 {
227 // Implementation of shell "dirname" builtin
228   TString s(filepath);
229   Int_t idx(s.Last('/'));
230   s=s(0, idx);
231   return s;
232 }
233
234 //______________________________________________________
235 Int_t AliTRDpwg1Helper::GetTaskIndex(const Char_t *name)
236 {
237 //  Give index in TRD train of task class "name"
238   for(Int_t it(0); it<kNTRDTASKS; it++){
239     if(strcmp(fgkTRDtaskClassName[it], name)==0) return it;
240   }
241   return -1;
242 }
243
244 //______________________________________________________
245 Bool_t AliTRDpwg1Helper::HasReadMCData(Char_t *opt)
246 {
247 // Use MC data option
248   return !(Bool_t)strstr(opt, "NOMC");
249 }
250
251 //____________________________________________
252 Bool_t AliTRDpwg1Helper::HasReadFriendData(Char_t *opt)
253 {
254 // Use friends data option
255   return !(Bool_t)strstr(opt, "NOFR");
256 }
257