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