1 ////////////////////////////////////////////////////////////////////////////
3 // Helper class for PWGPP TRD train //
6 // Markus Fasel <M.Fasel@gsi.de> //
8 ////////////////////////////////////////////////////////////////////////////
13 #include <TObjArray.h>
14 #include <TObjString.h>
15 #include <TFileMerger.h>
24 #include "AliTRDpwgppHelper.h"
26 const Char_t * AliTRDpwgppHelper::fgkTRDtaskClassName[AliTRDpwgppHelper::kNTRDTASKS] = {
37 ,"AliTRDalignmentTask"
39 ,"AliTRDclusterResolution"
43 const Char_t * AliTRDpwgppHelper::fgkTRDtaskOpt[AliTRDpwgppHelper::kNTRDTASKS+1] = {
61 //______________________________________________________
62 Bool_t AliTRDpwgppHelper::DoTask(Int_t idx, Int_t map)
64 return TESTBIT(map, idx);
67 //______________________________________________________
68 Int_t AliTRDpwgppHelper::ParseOptions(const Char_t *trd)
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),
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);
99 } else if(s.CompareTo("NOMC") == 0 || s.CompareTo("NOFR") == 0){
100 continue; // taken care by special functions
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);
110 if(!foundOpt) Warning("AliTRDpwgppHelper::ParseOptions()", "TRD task %s not implemented (yet).", s.Data());
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);
127 //______________________________________________________
128 void AliTRDpwgppHelper::MergeProd(const Char_t *mark, const Char_t *files, const Int_t nBatch, Int_t level)
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.
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.));
137 std::string filename;
138 std::ifstream file(files);
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));
145 Int_t nBatches=Int_t(TMath::Ceil(Double_t(iline)/nBatch));
146 Info("MergeProd()", "Merge %d files in %d batches.", iline, nBatches);
149 for(Int_t ibatch(0); ibatch<nBatches; ibatch++){
150 first = ibatch*nBatch;
151 if(gSystem->Exec(Form("aliroot -b -q \'$ALICE_ROOT/PWGPP/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));
157 Info("AliTRDpwgppHelper::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("AliTRDpwgppHelper::MergeProd()", "Merge %d files in 1 batch.", nBatches);
161 if(!gSystem->Exec(Form("aliroot -b -q \'$ALICE_ROOT/PWGPP/TRD/macros/mergeBatch.C(\"%s\", \"%s\", %d, 0, kFALSE)\'", mark, lMERGE, nBatches))) return;
162 gSystem->Exec(Form("mv 0_%s %s", mark, mark));
165 Info("AliTRDpwgppHelper::MergeProd()", "Merge level %d.", level);
166 MergeProd(mark, lMERGE, nBatch, level);
168 gSystem->Exec(Form("rm -fv %s %s", lMERGE, lPURGE));
172 //______________________________________________________
173 Int_t AliTRDpwgppHelper::MergeBatch(const Char_t *mark, const Char_t *files, const Int_t nfiles, const Int_t first, Bool_t kSVN, Bool_t kCLEAR)
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".
180 // On return the name of the merged file is return or NULL in case of failure.
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;
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;
197 Info("AliTRDpwgppHelper::MergeBatch()", "%s", filename.c_str());
198 if(!fFM.AddFile(filename.c_str())) return NULL;
199 arr.Add(new TObjString(filename.c_str()));
201 if(nbatch==nfiles) break;
204 Warning("AliTRDpwgppHelper::MergeBatch()", "NOTHING TO MERGE"); return NULL;
206 Info("AliTRDpwgppHelper::MergeBatch()", "MERGING FILES[%d] START[%d] %s ... ", nbatch, first, ((nbatch<nfiles)?"INCOMPLETE":""));
209 Info("AliTRDpwgppHelper::MergeBatch()", "Failed [%s]", fFM.GetOutputFileName());
212 Info("AliTRDpwgppHelper::MergeBatch()", "Done [%s]", fFM.GetOutputFileName());
215 for(Int_t ifile(0); ifile<arr.GetEntries(); ifile++){
216 gSystem->Exec(Form("rm -fv %s", ((TObjString*)arr.At(ifile))->GetString().Data()));
222 //______________________________________________________
223 const Char_t* AliTRDpwgppHelper::Basename(const char* filepath)
225 // Implementation of shell "basename" builtin
227 Int_t idx(s.Last('/')+1);
232 //______________________________________________________
233 const Char_t* AliTRDpwgppHelper::Dirname(const char* filepath)
235 // Implementation of shell "dirname" builtin
237 Int_t idx(s.Last('/'));
242 //______________________________________________________
243 Int_t AliTRDpwgppHelper::GetTaskIndex(const Char_t *name)
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;
252 //______________________________________________________
253 Bool_t AliTRDpwgppHelper::HasReadMCData(const Char_t *opt)
255 // Use MC data option
256 return !(Bool_t)strstr(opt, "NOMC");
259 //____________________________________________
260 Bool_t AliTRDpwgppHelper::HasReadFriendData(const Char_t *opt)
262 // Use friends data option
263 return !(Bool_t)strstr(opt, "NOFR");