1 ////////////////////////////////////////////////////////////////////////////
3 // Helper class for PWG1 TRD train //
6 // Markus Fasel <M.Fasel@gsi.de> //
8 ////////////////////////////////////////////////////////////////////////////
13 #include <TObjArray.h>
14 #include <TObjString.h>
15 #include <TFileMerger.h>
24 #include "AliTRDpwg1Helper.h"
26 const Char_t * AliTRDpwg1Helper::fgkTRDtaskClassName[AliTRDpwg1Helper::kNTRDTASKS] = {
36 ,"AliTRDalignmentTask"
38 ,"AliTRDclusterResolution"
42 const Char_t * AliTRDpwg1Helper::fgkTRDtaskOpt[AliTRDpwg1Helper::kNTRDTASKS+1] = {
59 //______________________________________________________
60 Bool_t AliTRDpwg1Helper::DoTask(Int_t idx, Int_t map)
62 return TESTBIT(map, idx);
65 //______________________________________________________
66 Int_t AliTRDpwg1Helper::ParseOptions(Char_t *trd)
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),
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);
96 } else if(s.CompareTo("NOMC") == 0 || s.CompareTo("NOFR") == 0){
97 continue; // taken care by special functions
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);
107 if(!foundOpt) Info("ParseOptions()", Form("TRD task %s not implemented (yet).", s.Data()));
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);
123 //______________________________________________________
124 void AliTRDpwg1Helper::MergeProd(const Char_t *mark, const Char_t *files, const Int_t nBatch, Int_t level)
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.
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.));
133 std::string filename;
134 std::ifstream file(files);
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));
141 Int_t nBatches(TMath::Ceil(Double_t(iline)/nBatch));
142 Info("MergeProd()", Form("Merge %d files in %d batches.", iline, nBatches));
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));
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));
161 Info("MergeProd()", Form("Merge level %d.", level));
162 MergeProd(mark, lMERGE, nBatch, level);
164 gSystem->Exec(Form("rm -fv %s %s", lMERGE, lPURGE));
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)
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".
176 // On return the name of the merged file is return or NULL in case of failure.
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;
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", Basename(filename.c_str()), gSystem->ExpandPathName("$PWD"))) == 0) kSVN=kFALSE;
193 Info("MergeBatch()", filename.c_str());
194 if(!fFM.AddFile(filename.c_str())) return NULL;
195 arr.Add(new TObjString(filename.c_str()));
197 if(nbatch==nfiles) break;
200 Warning("MergeBatch()", "NOTHING TO MERGE"); return NULL;
202 Info("MergeBatch()", "MERGING FILES[%d] START[%d] %s ... ", nbatch, first, ((nbatch<nfiles)?"INCOMPLETE":""));
204 if(!fFM.Merge()) return NULL;
207 for(Int_t ifile(0); ifile<arr.GetEntries(); ifile++){
208 gSystem->Exec(Form("rm -fv %s", ((TObjString*)arr.At(ifile))->GetString().Data()));
211 return fFM.GetOutputFileName();
214 //______________________________________________________
215 const Char_t* AliTRDpwg1Helper::Basename(const char* filepath)
217 // Implementation of shell "basename" builtin
219 Int_t idx(s.Last('/')+1);
224 //______________________________________________________
225 const Char_t* AliTRDpwg1Helper::Dirname(const char* filepath)
227 // Implementation of shell "dirname" builtin
229 Int_t idx(s.Last('/'));
234 //______________________________________________________
235 Int_t AliTRDpwg1Helper::GetTaskIndex(const Char_t *name)
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;
244 //______________________________________________________
245 Bool_t AliTRDpwg1Helper::HasReadMCData(Char_t *opt)
247 // Use MC data option
248 return !(Bool_t)strstr(opt, "NOMC");
251 //____________________________________________
252 Bool_t AliTRDpwg1Helper::HasReadFriendData(Char_t *opt)
254 // Use friends data option
255 return !(Bool_t)strstr(opt, "NOFR");