KaliVeda
Toolkit for HIC analysis
KVSimDirAnalyser.cpp
1 //Created by KVClassFactory on Tue Feb 14 11:13:53 2017
2 //Author: John Frankland,,,
3 
4 #include "KVSimDirAnalyser.h"
5 #include "KVDataAnalysisTask.h"
6 #include <KVSimFile.h>
7 #include <KVSimDir.h>
8 #include <KVClassFactory.h>
9 #include <TStopwatch.h>
10 #include "TSystem.h"
11 
12 ClassImp(KVSimDirAnalyser)
13 
14 
15 
19  : KVDataAnalyser(), fListOfSimFiles(nullptr), fListOfAuxFiles(nullptr), fAnalysisChain(nullptr), fSimDir(nullptr), fCopyFilesToWorkDir(false)
20 {
21  // Default constructor
22 }
23 
24 
25 
26 
28 
30 {
31  if (fListOfSimFiles && TString(fListOfSimFiles->GetName()) == "toDelete")
32  SafeDelete(fListOfSimFiles);
33 }
34 
35 
36 
38 
40 {
41  if (fListOfAuxFiles && TString(fListOfAuxFiles->GetName()) == "toDelete")
42  SafeDelete(fListOfAuxFiles);
43 }
44 
45 
46 
47 
48 
51 
53 {
54  // Set up and run the analysis
55 
56  BuildChain();
57 
58  Bool_t read_all_events = (GetNbEventToRead() == 0);
59  Long64_t nevents = (!read_all_events ? GetNbEventToRead() : fAnalysisChain->GetEntries());
60  Long64_t update_interval = (nevents > 10 ? nevents / 10 : 1);
61  TString results_file_name;
62  KVSimFile* first_file = (KVSimFile*)fListOfSimFiles->First();
63  results_file_name.Form("%s_%s", GetUserClass(), first_file->GetName());
64  TString options;
65  options.Form("SimFileName=%s,SimTitle=%s,OutputDir=%s,EventsReadInterval=%lld,BranchName=%s,CombinedOutputFile=%s,SimulationInfos=%s",
66  first_file->GetName(), fAnalysisChain->GetTitle(), (IsCopyFilesToWorkDir() ? "." : first_file->GetSimDir()->GetDirectory()),
67  update_interval, first_file->GetBranchName(),
68  results_file_name.Data(), first_file->GetTitle());
69  if (first_file->IsFiltered()) {
70  options += Form(",DataSet=%s", first_file->GetDataSet());
71  if (first_file->HasRun())
72  options += Form(",Run=%d", first_file->GetRun());
73  if (first_file->HasSystem())
74  options += Form(",System=%s", first_file->GetSystem());
75  }
76  if (fListOfAuxFiles) {
77  // Set up list of auxiliary files
78  KVString auxfiles;
79  TIter ifi(fListOfAuxFiles);
80  KVSimFile* auxf;
81  while ((auxf = (KVSimFile*)ifi())) {
82  if (auxfiles != "") auxfiles += "|";
83  auxfiles += auxf->GetName();
84  }
85  options += ",AuxFiles=";
86  options += auxfiles;
87  options += ",AuxDir=";
88  KVSimFile* ffaux = (KVSimFile*)fListOfAuxFiles->First();
89  options += ffaux->GetSimDir()->GetDirectory();
90  options += ",AuxTreeName=";
91  options += ffaux->GetTreeName();
92  options += ",AuxBranchName=";
93  options += ffaux->GetBranchName();
94  }
95  // Add any user-defined options
96  if (GetUserClassOptions() != "") {
97  options += ",";
98  options += GetUserClassOptions();
99  }
100 
101  // Check compilation of user class & run
102  if (CheckIfUserClassIsValid()) {
103  Info("SubmitTask", "Beginning TChain::Process...");
104 #ifdef WITH_CPP11
105  if (GetProofMode() != KVDataAnalyser::EProofMode::None) fAnalysisChain->SetProof(kTRUE);
106 #else
107  if (GetProofMode() != KVDataAnalyser::None) fAnalysisChain->SetProof(kTRUE);
108 #endif
109  TString analysis_class;
110  if (GetAnalysisTask()->WithUserClass()) analysis_class.Form("%s%s", GetUserClassImp().Data(), GetACliCMode());
111  else analysis_class = GetUserClass();
112  if (read_all_events) {
113  fAnalysisChain->Process(analysis_class, options.Data());
114  }
115  else {
116  fAnalysisChain->Process(analysis_class, options.Data(), GetNbEventToRead());
117  }
118  }
119  delete fAnalysisChain;
120  fAnalysisChain = nullptr;
121 }
122 
123 
124 
127 
129 {
130  // Returns path to data to be analysed
131  if (!NeedToChooseWhatToAnalyse()) {
132  KVSimFile* first_file = (KVSimFile*)fListOfSimFiles->First();
133  return first_file->GetSimDir()->GetDirectory();
134  }
135  return "";
136 }
137 
138 
139 
145 
146 void KVSimDirAnalyser::WriteBatchEnvFile(const TString& jobname, Bool_t sav)
147 {
148  //Save (in the TEnv fBatchEnv) all necessary information on analysis task which can be used to execute it later
149  //(i.e. when batch processing system executes the job).
150  //If save=kTRUE (default), write the information in a file whose name is given by ".jobname"
151  //where 'jobname' is the name of the job as given to the batch system.
152 
153  KVDataAnalyser::WriteBatchEnvFile(jobname, kFALSE);
154  KVSimFile* simF = (KVSimFile*)fListOfSimFiles->First();
155  KVSimDir* simD = simF->GetSimDir();
156  GetBatchInfoFile()->SetValue("SimDir", simD->GetDirectory());
157  if (simF->IsFiltered()) GetBatchInfoFile()->SetValue("SimFile.Type", "filtered");
158  else GetBatchInfoFile()->SetValue("SimFile.Type", "simulated");
159  GetBatchInfoFile()->SetValue("SimFiles", simF->GetName());
160  if (fListOfSimFiles->GetEntries() > 1) {
161  TIter next(fListOfSimFiles);
162  next();
163  while ((simF = (KVSimFile*)next())) GetBatchInfoFile()->SetValue("+SimFiles", simF->GetName());
164  }
165  if (fListOfAuxFiles) {
166  // auxiliary files for batch job
167  GetBatchInfoFile()->SetValue("AuxFiles", fListOfAuxFiles->First()->GetName());
168  if (fListOfAuxFiles->GetEntries() > 1) {
169  TIter next(fListOfAuxFiles);
170  next();
171  while ((simF = (KVSimFile*)next())) GetBatchInfoFile()->SetValue("+AuxFiles", simF->GetName());
172  }
173  }
174  if (sav) GetBatchInfoFile()->SaveLevel(kEnvUser);
175 }
176 
177 
178 
183 
184 Bool_t KVSimDirAnalyser::ReadBatchEnvFile(const TString& filename)
185 {
186  //Read the batch env file "filename" and initialise the analysis task using the
187  //informations in the file
188  //Returns kTRUE if all goes well
189 
190  Bool_t ok = kFALSE;
191 
192  if (!KVDataAnalyser::ReadBatchEnvFile(filename)) return ok;
193 
194  KVString simdir = GetBatchInfoFile()->GetValue("SimDir", "");
195  if (simdir == "") return ok;
196 
197  fSimDir = new KVSimDir("SIMDIR", simdir);
199 
200  KVString filetype = GetBatchInfoFile()->GetValue("SimFile.Type", "");
201  if (filetype == "") return ok;
202 
203  KVString simfiles = GetBatchInfoFile()->GetValue("SimFiles", "");
204  if (simfiles == "") return ok;
205 
207  fListOfSimFiles = new TList;
208  fListOfSimFiles->SetName("toDelete");
209 
210  simfiles.Begin(" ");
211  while (!simfiles.End()) {
212  if (filetype == "simulated") fListOfSimFiles->Add(fSimDir->GetSimDataList()->FindObject(simfiles.Next()));
213  else if (filetype == "filtered") fListOfSimFiles->Add(fSimDir->GetFiltDataList()->FindObject(simfiles.Next()));
214  }
215 
216  // this option, if set, copies the files to be analysed to the current working directory
217  SetCopyFilesToWorkDir(GetBatchInfoFile()->GetValue("SimDirAnalyser.CopyFilesToWorkingDirectory", false));
218 
219  KVString auxfiles = GetBatchInfoFile()->GetValue("AuxFiles", "");
220  if (auxfiles == "") return (ok = kTRUE);
221 
223  fListOfAuxFiles = new TList;
224  fListOfAuxFiles->SetName("toDelete");
225 
226  auxfiles.Begin(" ");
227  while (!auxfiles.End()) {
228  fListOfAuxFiles->Add(fSimDir->GetSimDataList()->FindObject(auxfiles.Next()));
229  }
230 
231  ok = kTRUE;
232 
233  return ok;
234 }
235 
236 
237 
242 
244 {
245  // Build a TChain with all files/trees to be analysed
246  //
247  // If fCopyFilesToWorkDir==true, files are first copied to working directory
248 
249  TIter next(fListOfSimFiles);
250  KVSimFile* file;
251  while ((file = (KVSimFile*)next())) {
252  if (!fAnalysisChain) {
253  fAnalysisChain = new TChain(file->GetTreeName());
254  }
255  TString fullpath;
256  AssignAndDelete(fullpath, gSystem->ConcatFileName(file->GetSimDir()->GetDirectory(), file->GetName()));
257  if (IsCopyFilesToWorkDir()) {
258  Info("BuildChain", "Copying file to analyse from %s to %s", fullpath.Data(), Form("./%s", gSystem->BaseName(fullpath)));
259  TStopwatch timer;
260  TFile::Cp(fullpath, Form("./%s", gSystem->BaseName(fullpath)), kFALSE, 1e+08);
261  Info("BuildChain", "Copied file in %g seconds", timer.RealTime());
262  fAnalysisChain->Add(gSystem->BaseName(fullpath));
263  }
264  else
265  fAnalysisChain->Add(fullpath);
266  }
267  if (IsCopyFilesToWorkDir()) {
268  // rescan the working directory to include the newly-added copies of the files to analyse
270  }
271 }
272 
273 
274 
279 
281 {
282  // Use first file in list to analyse to obtain information for generating
283  // automatic batch job names (name of model, eventually info on simulated reaction,
284  // etc.)
285 
286  KVSimFile* sf = dynamic_cast<KVSimFile*>(fListOfSimFiles->First());
287  // name of TTree is (usually) name of model
288  fModel = sf->GetTreeName();
289  if (fModel == "dit_events") fModel = "DIT"; // except for DIT
290 }
291 
292 
293 
294 
297 
298 void KVSimDirAnalyser::Make(const Char_t* kvsname)
299 {
300  // Generate a new simulated analysis selector class
301 
302 #ifndef USING_ROOT6
303  KVClassFactory cf(kvsname, "Analysis of simulated events", "",
304  kTRUE, "SimulatedEventAnalysisTemplate");
305 #else
306  KVClassFactory cf(kvsname, "Analysis of simulated events", "",
307  kTRUE, "ROOT6SimulatedEventAnalysisTemplate");
308 #endif
309  cf.AddImplIncludeFile("KVSimNucleus.h");
310  cf.AddImplIncludeFile("KVBatchSystem.h");
311 
312  cf.GenerateCode();
313 }
314 
315 
316 
319 
320 TString KVSimDirAnalyser::ExpandAutoBatchName(const Char_t* format) const
321 {
322  // Replace any special symbols in auto-format jobname with current values
323 
324  TString tmp = KVDataAnalyser::ExpandAutoBatchName(format);
325  tmp.ReplaceAll("KVEventFiltering",
326  Form("%s_%s_FILTER_%s", fModel.Data(), fFilterSystem.Data(), fFilterDataSet.Data()));
327  tmp.ReplaceAll(" ", "");
328  tmp.ReplaceAll("@", "_");
329  tmp.ReplaceAll("+", "_");
330  tmp.ReplaceAll("/", "_");
331  return tmp;
332 }
333 
334 
Factory class for generating skeleton files for new classes.
void GenerateCode()
Generate header and implementation file for currently-defined class.
void AddImplIncludeFile(const Char_t *filename)
Manager class which sets up and runs data analysis tasks.
const Char_t * GetACliCMode()
virtual TString ExpandAutoBatchName(const Char_t *format) const
const KVString & GetUserClassImp() const
virtual void WriteBatchEnvFile(const TString &, Bool_t sav=kTRUE)
virtual Bool_t CheckIfUserClassIsValid(const KVString &alternative_base_class="")
TList * fWorkDirInit
list of files in working directory before task runs
const KVString & GetUserClassOptions() const
const Char_t * GetUserClass()
EProofMode GetProofMode() const
void ScanWorkingDirectory(TList **)
Long64_t GetNbEventToRead(void) const
virtual Bool_t ReadBatchEnvFile(const TString &)
KVDataAnalysisTask * GetAnalysisTask() const
TEnv * GetBatchInfoFile() const
virtual TObject * FindObject(const char *name) const
Class piloting analyses of simulated data.
TList * fListOfAuxFiles
[optional] list of original simulated data to be used during filtered data analysis
TString ExpandAutoBatchName(const Char_t *format) const override
Replace any special symbols in auto-format jobname with current values.
Bool_t NeedToChooseWhatToAnalyse() const override
KVSimDir * fSimDir
used for batch analysis
Bool_t IsCopyFilesToWorkDir() const
TList * fListOfSimFiles
list of files/trees to analyse
TChain * fAnalysisChain
TChain for analysis.
Bool_t ReadBatchEnvFile(const TString &) override
void GetInfosForJobNameFromFiles()
KVString GetRootDirectoryOfDataToAnalyse() const override
Returns path to data to be analysed.
void SetCopyFilesToWorkDir(Bool_t on=kTRUE)
TString fFilterDataSet
name of dataset to be used for filtering the simulation
TString fFilterSystem
name of system to be used for filtering the simulation
TString fModel
name of model used for simulation
void SubmitTask() override
Set up and run the analysis.
static void Make(const Char_t *kvsname="MySimulatedAnalysis")
Generate a new simulated analysis selector class.
void WriteBatchEnvFile(const TString &, Bool_t sav=kTRUE) override
Handle directory containing simulated and/or filtered simulated data ,.
Definition: KVSimDir.h:44
virtual const KVList * GetFiltDataList() const
Definition: KVSimDir.h:73
virtual void AnalyseDirectory()
Definition: KVSimDir.cpp:126
virtual const KVList * GetSimDataList() const
Definition: KVSimDir.h:69
virtual const Char_t * GetDirectory() const
Definition: KVSimDir.h:61
Handle file containing simulated and/or filtered simulated data ,.
Definition: KVSimFile.h:19
const Char_t * GetSystem() const
Definition: KVSimFile.h:81
const Char_t * GetBranchName() const
Definition: KVSimFile.h:118
Int_t GetRun() const
Definition: KVSimFile.h:96
Bool_t HasRun() const
Definition: KVSimFile.h:101
const Char_t * GetDataSet() const
Definition: KVSimFile.h:76
KVSimDir * GetSimDir() const
Definition: KVSimFile.h:66
Bool_t HasSystem() const
Definition: KVSimFile.h:86
Bool_t IsFiltered() const
Definition: KVSimFile.h:45
const Char_t * GetTreeName() const
Definition: KVSimFile.h:113
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:73
void Begin(TString delim) const
Definition: KVString.cpp:565
Bool_t End() const
Definition: KVString.cpp:634
KVString Next(Bool_t strip_whitespace=kFALSE) const
Definition: KVString.cpp:695