KaliVeda  1.12/06
Heavy-Ion Analysis Toolkit
KVSimDirAnalyser.cpp
Go to the documentation of this file.
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 
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")
33 }
34 
35 
36 
38 
40 {
41  if (fListOfAuxFiles && TString(fListOfAuxFiles->GetName()) == "toDelete")
43 }
44 
45 
46 
49 
51 {
52  // Destructor
56 }
57 
58 
59 
62 
64 {
65  // Set up and run the analysis
66 
67  BuildChain();
68 
69  Bool_t read_all_events = (GetNbEventToRead() == 0);
70  Long64_t nevents = (!read_all_events ? GetNbEventToRead() : fAnalysisChain->GetEntries());
71  Long64_t update_interval = (nevents > 10 ? nevents / 10 : 1);
72  TString results_file_name;
73  KVSimFile* first_file = (KVSimFile*)fListOfSimFiles->First();
74  results_file_name.Form("%s_%s", GetUserClass(), first_file->GetName());
75  TString options;
76  options.Form("SimFileName=%s,SimTitle=%s,OutputDir=%s,EventsReadInterval=%lld,BranchName=%s,CombinedOutputFile=%s,SimulationInfos=%s",
77  first_file->GetName(), fAnalysisChain->GetTitle(), (IsCopyFilesToWorkDir() ? "." : first_file->GetSimDir()->GetDirectory()),
78  update_interval, first_file->GetBranchName(),
79  results_file_name.Data(), first_file->GetTitle());
80  if (first_file->IsFiltered()) options += Form(",DataSet=%s,System=%s,Run=%d",
81  first_file->GetDataSet(), first_file->GetSystem(), first_file->GetRun());
82  if (fListOfAuxFiles) {
83  // Set up list of auxiliary files
84  KVString auxfiles;
86  KVSimFile* auxf;
87  while ((auxf = (KVSimFile*)ifi())) {
88  if (auxfiles != "") auxfiles += "|";
89  auxfiles += auxf->GetName();
90  }
91  options += ",AuxFiles=";
92  options += auxfiles;
93  options += ",AuxDir=";
95  options += ffaux->GetSimDir()->GetDirectory();
96  options += ",AuxTreeName=";
97  options += ffaux->GetTreeName();
98  options += ",AuxBranchName=";
99  options += ffaux->GetBranchName();
100  }
101  // Add any user-defined options
102  if (GetUserClassOptions() != "") {
103  options += ",";
104  options += GetUserClassOptions();
105  }
106 
107  // Check compilation of user class & run
108  if (CheckIfUserClassIsValid()) {
109  Info("SubmitTask", "Beginning TChain::Process...");
110 #ifdef WITH_CPP11
111  if (GetProofMode() != KVDataAnalyser::EProofMode::None) fAnalysisChain->SetProof(kTRUE);
112 #else
114 #endif
115  TString analysis_class;
116  if (GetAnalysisTask()->WithUserClass()) analysis_class.Form("%s%s", GetUserClassImp().Data(), GetACliCMode());
117  else analysis_class = GetUserClass();
118  if (read_all_events) {
119  fAnalysisChain->Process(analysis_class, options.Data());
120  }
121  else {
122  fAnalysisChain->Process(analysis_class, options.Data(), GetNbEventToRead());
123  }
124  }
125  delete fAnalysisChain;
126  fAnalysisChain = nullptr;
127 }
128 
129 
130 
133 
135 {
136  // Returns path to data to be analysed
137  if (!NeedToChooseWhatToAnalyse()) {
138  KVSimFile* first_file = (KVSimFile*)fListOfSimFiles->First();
139  return first_file->GetSimDir()->GetDirectory();
140  }
141  return "";
142 }
143 
144 
145 
151 
153 {
154  //Save (in the TEnv fBatchEnv) all necessary information on analysis task which can be used to execute it later
155  //(i.e. when batch processing system executes the job).
156  //If save=kTRUE (default), write the information in a file whose name is given by ".jobname"
157  //where 'jobname' is the name of the job as given to the batch system.
158 
161  KVSimDir* simD = simF->GetSimDir();
162  GetBatchInfoFile()->SetValue("SimDir", simD->GetDirectory());
163  if (simF->IsFiltered()) GetBatchInfoFile()->SetValue("SimFile.Type", "filtered");
164  else GetBatchInfoFile()->SetValue("SimFile.Type", "simulated");
165  GetBatchInfoFile()->SetValue("SimFiles", simF->GetName());
166  if (fListOfSimFiles->GetEntries() > 1) {
167  TIter next(fListOfSimFiles);
168  next();
169  while ((simF = (KVSimFile*)next())) GetBatchInfoFile()->SetValue("+SimFiles", simF->GetName());
170  }
171  if (fListOfAuxFiles) {
172  // auxiliary files for batch job
173  GetBatchInfoFile()->SetValue("AuxFiles", fListOfAuxFiles->First()->GetName());
174  if (fListOfAuxFiles->GetEntries() > 1) {
175  TIter next(fListOfAuxFiles);
176  next();
177  while ((simF = (KVSimFile*)next())) GetBatchInfoFile()->SetValue("+AuxFiles", simF->GetName());
178  }
179  }
180  if (sav) GetBatchInfoFile()->SaveLevel(kEnvUser);
181 }
182 
183 
184 
189 
191 {
192  //Read the batch env file "filename" and initialise the analysis task using the
193  //informations in the file
194  //Returns kTRUE if all goes well
195 
196  Bool_t ok = kFALSE;
197 
198  if (!KVDataAnalyser::ReadBatchEnvFile(filename)) return ok;
199 
200  KVString simdir = GetBatchInfoFile()->GetValue("SimDir", "");
201  if (simdir == "") return ok;
202 
203  fSimDir = new KVSimDir("SIMDIR", simdir);
205 
206  KVString filetype = GetBatchInfoFile()->GetValue("SimFile.Type", "");
207  if (filetype == "") return ok;
208 
209  KVString simfiles = GetBatchInfoFile()->GetValue("SimFiles", "");
210  if (simfiles == "") return ok;
211 
213  fListOfSimFiles = new TList;
214  fListOfSimFiles->SetName("toDelete");
215 
216  simfiles.Begin(" ");
217  while (!simfiles.End()) {
218  if (filetype == "simulated") fListOfSimFiles->Add(fSimDir->GetSimDataList()->FindObject(simfiles.Next()));
219  else if (filetype == "filtered") fListOfSimFiles->Add(fSimDir->GetFiltDataList()->FindObject(simfiles.Next()));
220  }
221 
222  // this option, if set, copies the files to be analysed to the current working directory
223  SetCopyFilesToWorkDir(GetBatchInfoFile()->GetValue("SimDirAnalyser.CopyFilesToWorkingDirectory", false));
224 
225  KVString auxfiles = GetBatchInfoFile()->GetValue("AuxFiles", "");
226  if (auxfiles == "") return (ok = kTRUE);
227 
229  fListOfAuxFiles = new TList;
230  fListOfAuxFiles->SetName("toDelete");
231 
232  auxfiles.Begin(" ");
233  while (!auxfiles.End()) {
235  }
236 
237  ok = kTRUE;
238 
239  return ok;
240 }
241 
242 
243 
248 
250 {
251  // Build a TChain with all files/trees to be analysed
252  //
253  // If fCopyFilesToWorkDir==true, files are first copied to working directory
254 
255  TIter next(fListOfSimFiles);
256  KVSimFile* file;
257  while ((file = (KVSimFile*)next())) {
258  if (!fAnalysisChain) {
259  fAnalysisChain = new TChain(file->GetTreeName());
260  }
261  TString fullpath;
262  AssignAndDelete(fullpath, gSystem->ConcatFileName(file->GetSimDir()->GetDirectory(), file->GetName()));
263  if (IsCopyFilesToWorkDir()) {
264  Info("BuildChain", "Copying file to analyse from %s to %s", fullpath.Data(), Form("./%s", gSystem->BaseName(fullpath)));
265  TStopwatch timer;
266  TFile::Cp(fullpath, Form("./%s", gSystem->BaseName(fullpath)), kFALSE, 1e+08);
267  Info("BuildChain", "Copied file in %g seconds", timer.RealTime());
268  fAnalysisChain->Add(gSystem->BaseName(fullpath));
269  }
270  else
271  fAnalysisChain->Add(fullpath);
272  }
273  if (IsCopyFilesToWorkDir()) {
274  // rescan the working directory to include the newly-added copies of the files to analyse
276  }
277 }
278 
279 
280 
285 
287 {
288  // Use first file in list to analyse to obtain information for generating
289  // automatic batch job names (name of model, eventually info on simulated reaction,
290  // etc.)
291 
292  KVSimFile* sf = dynamic_cast<KVSimFile*>(fListOfSimFiles->First());
293  // name of TTree is (usually) name of model
294  fModel = sf->GetTreeName();
295  if (fModel == "dit_events") fModel = "DIT"; // except for DIT
296 }
297 
298 
299 
300 
303 
304 void KVSimDirAnalyser::Make(const Char_t* kvsname)
305 {
306  // Generate a new simulated analysis selector class
307 
308 #ifndef USING_ROOT6
309  KVClassFactory cf(kvsname, "Analysis of simulated events", "",
310  kTRUE, "SimulatedEventAnalysisTemplate");
311 #else
312  KVClassFactory cf(kvsname, "Analysis of simulated events", "",
313  kTRUE, "ROOT6SimulatedEventAnalysisTemplate");
314 #endif
315  cf.AddImplIncludeFile("KVSimNucleus.h");
316  cf.AddImplIncludeFile("KVBatchSystem.h");
317 
318  cf.GenerateCode();
319 }
320 
321 
322 
325 
327 {
328  // Replace any special symbols in auto-format jobname with current values
329 
331  tmp.ReplaceAll("KVEventFiltering",
332  Form("%s_%s_FILTER_%s", fModel.Data(), fFilterSystem.Data(), fFilterDataSet.Data()));
333  tmp.ReplaceAll(" ", "");
334  tmp.ReplaceAll("@", "_");
335  tmp.ReplaceAll("+", "_");
336  tmp.ReplaceAll("/", "_");
337  return tmp;
338 }
339 
340 
void AssignAndDelete(TString &target, char *tobedeleted)
ClassImp(KVPartitionList) void KVPartitionList
Initialisation.
KVString GetValue(KVString &l, char c)
Definition: KVTGID.cpp:800
#define SafeDelete(p)
#define e(i)
char Char_t
const Bool_t kFALSE
bool Bool_t
const Bool_t kTRUE
kEnvUser
char * Form(const char *fmt,...)
R__EXTERN TSystem * gSystem
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 Bool_t CheckIfUserClassIsValid(const KVString &alternative_base_class="")
virtual void WriteBatchEnvFile(const Char_t *, Bool_t sav=kTRUE)
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 Char_t *)
KVDataAnalysisTask * GetAnalysisTask() const
TEnv * GetBatchInfoFile() const
virtual TObject * FindObject(const char *name) const
Class piloting analyses of simulated data.
Bool_t ReadBatchEnvFile(const Char_t *)
void SubmitTask()
Set up and run the analysis.
KVString GetRootDirectoryOfDataToAnalyse() const
Returns path to data to be analysed.
TString ExpandAutoBatchName(const Char_t *format) const
Replace any special symbols in auto-format jobname with current values.
virtual Bool_t NeedToChooseWhatToAnalyse() const
TList * fListOfAuxFiles
[optional] list of original simulated data to be used during filtered data analysis
KVSimDir * fSimDir
used for batch analysis
Bool_t IsCopyFilesToWorkDir() const
TList * fListOfSimFiles
list of files/trees to analyse
TChain * fAnalysisChain
TChain for analysis.
virtual ~KVSimDirAnalyser()
Destructor.
void GetInfosForJobNameFromFiles()
void SetCopyFilesToWorkDir(Bool_t on=kTRUE)
void WriteBatchEnvFile(const Char_t *, Bool_t sav=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
static void Make(const Char_t *kvsname="MySimulatedAnalysis")
Generate a new simulated analysis selector class.
Handle directory containing simulated and/or filtered simulated data ,.
Definition: KVSimDir.h:43
virtual const KVList * GetFiltDataList() const
Definition: KVSimDir.h:72
virtual void AnalyseDirectory()
Definition: KVSimDir.cpp:126
virtual const KVList * GetSimDataList() const
Definition: KVSimDir.h:68
virtual const Char_t * GetDirectory() const
Definition: KVSimDir.h:60
Handle file containing simulated and/or filtered simulated data ,.
Definition: KVSimFile.h:18
const Char_t * GetSystem() const
Definition: KVSimFile.h:80
const Char_t * GetBranchName() const
Definition: KVSimFile.h:107
Int_t GetRun() const
Definition: KVSimFile.h:90
const Char_t * GetDataSet() const
Definition: KVSimFile.h:75
KVSimDir * GetSimDir() const
Definition: KVSimFile.h:65
Bool_t IsFiltered() const
Definition: KVSimFile.h:44
const Char_t * GetTreeName() const
Definition: KVSimFile.h:102
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:72
void Begin(TString delim) const
Definition: KVString.cpp:562
Bool_t End() const
Definition: KVString.cpp:625
KVString Next(Bool_t strip_whitespace=kFALSE) const
Definition: KVString.cpp:675
virtual Long64_t Process(const char *filename, Option_t *option="", Long64_t nentries=kMaxEntries, Long64_t firstentry=0)
virtual Int_t Add(const char *name, Long64_t nentries=TTree::kMaxEntries)
virtual Long64_t GetEntries() const
virtual void SetProof(Bool_t on=kTRUE, Bool_t refresh=kFALSE, Bool_t gettreeheader=kFALSE)
virtual const char * GetName() const
void SetName(const char *name)
virtual Int_t GetEntries() const
virtual const char * GetValue(const char *name, const char *dflt) const
virtual void SetValue(const char *name, const char *value, EEnvLevel level=kEnvChange, const char *type=nullptr)
virtual void SaveLevel(EEnvLevel level)
virtual Bool_t Cp(const char *dst, Bool_t progressbar=kTRUE, UInt_t buffersize=1000000)
virtual void Add(TObject *obj)
virtual TObject * First() const
virtual const char * GetName() const
virtual const char * GetTitle() const
virtual const char * GetName() const
virtual void Info(const char *method, const char *msgfmt,...) const
Double_t RealTime()
const char * Data() const
void Form(const char *fmt,...)
TString & ReplaceAll(const char *s1, const char *s2)
virtual char * ConcatFileName(const char *dir, const char *name)
virtual const char * BaseName(const char *pathname)
long long Long64_t