KaliVeda
Toolkit for HIC analysis
KVSimReader.cpp
1 //Created by KVClassFactory on Wed Jun 30 17:45:01 2010
2 //Author: bonnet
3 
4 #include "KVSimReader.h"
5 #include "TDirectory.h"
6 #include "TStopwatch.h"
7 
9 
10 
11 
12 //____________________________________________________
13 
14 
16 void KVSimReader::init()
17 {
18 
19  kmode = kTRUE;
20  tree = 0;
21  evt = 0;
22  nuc = 0 ;
23  nevt = 0;
24  fMultiFiles = kFALSE;
25  fFileIndex = 1;
26  fOutputDirectory = "./";
27 
28  tree_name = "SIMULATION_NAME";
29  tree_title = "SIMULATION";
30  root_file_name = "Output.root";
31  //ascii_file_name = "";
32  branch_name = "Simulated_evts";
33 
34  nv = new KVNameValueList();
35 
36  CreateObjectList();
37  CreateInfoList();
38 
39 }
40 
41 
42 
45 
47 {
48  // Default constructor
49  init();
50 }
51 
52 
53 
56 
58 {
59  // Method called by constructors with KVString filename argument
60  if (!OpenFileToRead(filename)) return;
61 
62  Run();
63 
64  CloseFile();
65 }
66 
67 
68 
71 
73 {
74  // Destructor
75  CleanAll();
76 
77  delete nv;
78  delete linked_objects;
79  delete linked_info;
80 
81 }
82 
83 
84 
86 
88 {
89 
90  nv->Clear();
91  GetSimuInfo()->Clear();
93 
94 }
95 
96 
97 
105 
107 {
108  // Use root_file_name as basename for file
109  // If multiple files are being read, they will be named
110  // [root_file_name].1
111  // [root_file_name].2
112  // [root_file_name].etc
113  // Files are written in fOutputDirectory (default: "./")
114 
117  if (fMultiFiles) filename += Form(".%d", fFileIndex);
118  if (!fOutputDirectory.EndsWith("/")) fOutputDirectory += "/";
119  filename.Prepend(fOutputDirectory);
120  Info("DeclareTree", "Ouverture du fichier de stockage %s", filename.Data());
121  file = new TFile(filename, option);
122 
123  tree = new TTree(tree_name, tree_title);
125 }
126 
127 
128 
130 
132 {
133 
134  Info("ReadFile", "To be defined in child class");
135 
136  /*
137  while (f_in.good()){
138  if (ReadHeader()){
139  for (Int_t nn=0;nn<nv->GetIntValue("stat"));nn+=1)
140  if (!ReadEvent()) break;
141  else if (HasToFill()) FillTree();
142  else {}
143  }
144 
145  }
146 
147  */
148 }
149 
150 
151 
153 
155 {
156 
157  Info("ReadHeader", "To be defined in child class");
158  /*
159  Int_t res = ReadLineAndCheck(2," ");
160  switch (res){
161  case 0:
162  Info("ReadHeader","case 0 line est vide");
163  return kFALSE;
164  case 1:
165  nv->SetValue("J",GetDoubleReadPar(0));
166  nv->SetValue("stat",GetDoubleReadPar(1));
167 
168  Info("ReadHeader","case 1 moment angulaire %1.0lf stat %1.0lf",nv->GetIntValue("J"),nv->GetIntValue("stat"));
169  return kTRUE;
170  default:
171  Info("ReadHeader","case 2 nombre de par lus %d differents de celui attendu %d",toks->GetEntries(),2);
172  //Info("ReadHeader","line %s",line.Data());
173  return kFALSE;
174  }
175  */
176  return kTRUE;
177 }
178 
179 
180 
182 
184 {
185 
186  Info("ReadEvent", "To be defined in child class");
187  /*
188  evt->Clear();
189  Int_t mult=0;
190  Int_t res = ReadLineAndCheck(1," ");
191  switch (res){
192  case 0:
193  Info("ReadEvent","case 0 line est vide");
194  return kFALSE;
195 
196  case 1:
197  evt->SetNumber(nevt);
198  mult = GetIntReadPar(0);
199  evt->GetParameters()->SetValue("ll",kSIM_var1);
200 
201  for (Int_t mm=0; mm<mult; mm+=1){
202  nuc = (KVSimNucleus* )evt->AddParticle();
203  ReadNucleus();
204  }
205 
206  nevt+=1;
207  return kTRUE;
208 
209  default:
210  Info("ReadEvent","case 2 nombre de par lus %d differents de celui attendu %d",toks->GetEntries(),1);
211  //Info("ReadHeader","line %s",line.Data());
212  return kFALSE;
213  }
214  */
215  return kTRUE;
216 
217 }
218 
219 
220 
222 
224 {
225 
226  Info("ReadNucleus", "To be defined in child class");
227  /*
228  Int_t res = ReadLineAndCheck(5," ");
229  switch (res){
230  case 0:
231  Info("ReadNucleus","case 0 line est vide");
232  return kFALSE;
233 
234  case 1:
235 
236  nuc->SetZ( GetIntReadPar(0) );
237  nuc->SetA( GetIntReadPar(1) );
238  nuc->SetKE( GetDoubleReadPar(2) );
239  nuc->SetTheta( GetDoubleReadPar(3) * TMath::RadToDeg() );
240  nuc->SetPhi( GetDoubleReadPar(4) * TMath::RadToDeg() );
241 
242  return kTRUE;
243 
244  default:
245 
246  Info("ReadNucleus","case 2 nombre de par lus %d differents de celui attendu %d",toks->GetEntries(),5);
247  //Info("ReadHeader","line %s",line.Data());
248  return kFALSE;
249  }
250  */
251  return kTRUE;
252 }
253 
254 
255 
258 
260 {
261  // Read events, convert and save in ROOT file
263  SaveTree();
264 }
265 
266 
267 
271 
273 {
274  // Static method
275  // Returns an instance of a child class specific to given model.
276 
277  TPluginHandler* ph = LoadPlugin("KVSimReader", model_uri);
278  if (!ph) {
279  ::Error("KVSimReader::MakeSimReader", "No plugin defined with name %s", model_uri);
280  return nullptr;
281  }
282  return (KVSimReader*)ph->ExecPlugin(0);
283 }
284 
285 
286 
288 
290 {
291 
292  AddInfo({"ascii file read", GetFileName()});
293 
294  TStopwatch chrono;
295  chrono.Start();
296 
297  evt = new KVSimEvent();
298  if (HasToFill()) DeclareTree(option);
299  nuc = 0;
300  nevt = 0;
301 
302  ReadFile();
303 
304  if (HasToFill())
305  GetTree()->ResetBranchAddress(GetTree()->GetBranch(branch_name.Data()));
306 
307  delete evt;
308  chrono.Stop();
309 
310  //Info("Run","%d evts lus en %lf seconds",GetNumberOfEvents(),chrono.RealTime());
311 
312  AddInfo({"number of events read", nevt});
313  AddInfo({"date", GetDate()});
314  AddInfo({"user", gSystem->GetUserInfo()->fUser});
315 
316 }
317 
318 
319 
321 
323 {
324 
325  WriteObjects();
326  WriteInfo();
327 
328  GetTree()->Write();
329  CleanAll();
330  file->Close();
331 
332 }
333 
334 
335 
337 
339 {
340  return linked_info;
341 }
342 
343 
344 
351 
352 void KVSimReader::AddInfo(const KVNamedParameter& info)
353 {
354  // Add information on simulation using KVNamedParameter, can be int, double or string:
355  //~~~{.cpp}
356  // AddInfo({"model", "AMD"});
357  // AddInfo({"sigma_NN_screen_factor", 0.85});
358  // AddInfo({"N_part_test", 150});
359 
360  linked_info->Add(new KVNamedParameter(info));
361 }
362 
363 
364 
366 
368 {
369  TList* list = GetTree()->GetUserInfo();
371  for (Int_t ii = 0; ii < nentries; ii += 1) {
372  list->Add(linked_info->RemoveAt(0));
373  }
374 }
375 
376 
377 
379 
381 {
382  linked_info = new KVUnownedList();
383  linked_info->SetName("Simulation info");
384 }
385 
386 
387 
388 
390 
392 {
393  return linked_objects;
394 }
395 
396 
397 
399 
401 {
402  GetLinkedObjects()->Add(obj);
403 }
404 
405 
406 
408 
410 {
411 
412  TList* list = GetTree()->GetUserInfo();
414  for (Int_t ii = 0; ii < nentries; ii += 1) {
415  list->Add(linked_objects->RemoveAt(0));
416  }
417 
418 }
419 
420 
421 
423 
425 {
427  linked_objects->SetName("List of objects");
428 }
429 
430 
431 /*
432 
433 fichier correspondant au format de lecture:
434 
435 0 2
436 10
437 1 1 5.810748 2.580167 -2.821796
438 1 1 9.990623 2.013791 3.080918
439 1 1 12.384371 1.813083 1.459004
440 1 1 7.553329 1.807012 1.629090
441 0 1 1.493379 2.652475 2.525657
442 0 1 0.971787 2.151803 0.616233
443 0 1 1.889498 0.789652 -1.659597
444 1 1 5.860981 1.636198 2.878590
445 1 1 4.296264 2.359454 2.377290
446 50 109 1.424833 0.958413 -0.798826
447 9
448 1 1 7.719303 0.954321 0.703780
449 1 1 13.956905 0.957730 1.756477
450 1 1 6.952527 1.496332 0.294119
451 1 1 7.750355 2.158870 -2.119215
452 1 1 5.972726 2.073538 1.496040
453 1 1 6.617680 1.352951 3.015066
454 1 1 7.526133 1.842921 3.073465
455 0 1 0.435096 1.950363 2.852936
456 49 110 0.426353 1.688514 -1.100454
457 10 3
458 9
459 0 1 2.385526 2.187467 -2.383691
460 2 4 13.103434 1.147666 2.958037
461 1 1 7.084269 1.675831 0.720834
462 0 1 0.742568 1.607419 0.278490
463 1 1 9.079510 2.765702 -0.297527
464 1 1 9.300734 0.170849 -0.891740
465 1 1 8.797631 1.098980 -0.344032
466 1 1 4.680343 1.923954 -3.007086
467 49 107 0.133186 2.320088 -0.156727
468 8
469 1 1 7.249172 1.496348 -0.976620
470 2 4 17.685099 0.551523 -2.905849
471 2 4 27.911638 1.766578 0.524513
472 1 1 8.419456 1.628637 -1.882687
473 0 1 0.680420 0.786916 0.688403
474 1 1 4.465025 1.745583 -1.592567
475 1 1 4.812778 2.287951 -3.126746
476 48 105 0.364456 2.255794 2.579171
477 9
478 1 1 12.462525 1.826489 1.113706
479 1 1 7.000116 2.608909 1.919405
480 0 1 2.846559 2.288652 -1.294329
481 0 1 1.650262 0.578845 -0.605950
482 1 1 6.264917 2.046585 1.862553
483 1 1 5.637622 0.790498 -2.418233
484 0 1 1.533256 0.051853 -0.220994
485 2 4 13.625276 0.567988 0.888917
486 50 107 0.708753 2.151470 -1.940892
487 
488 */
489 
int Int_t
bool Bool_t
constexpr Bool_t kFALSE
constexpr Bool_t kTRUE
const char Option_t
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
int nentries
char * Form(const char *fmt,...)
R__EXTERN TSystem * gSystem
void Error(const char *method, const char *msgfmt,...) const override
colourised errors (red) !
Definition: KVBase.cpp:1712
static TPluginHandler * LoadPlugin(const Char_t *base, const Char_t *uri="0")
Definition: KVBase.cpp:796
static void MakeEventBranch(TTree *tree, const TString &branchname, T &event, Int_t bufsize=10000000)
Definition: KVEvent.h:211
KVString GetFileName()
Definition: KVFileReader.h:190
void CloseFile()
Definition: KVFileReader.h:237
Bool_t OpenFileToRead(const KVString &filename)
Definition: KVFileReader.h:210
Handles lists of named parameters with different types, a list of KVNamedParameter objects.
void Clear(Option_t *opt="") override
A generic named parameter storing values of different types.
KaliVeda extensions to ROOT collection classes.
void Add(TObject *obj) override
void Clear(Option_t *option="") override
Container class for simulated nuclei, KVSimNucleus.
Definition: KVSimEvent.h:22
Base class to read output files for simulation and create tree using KVSimEvent class.
Definition: KVSimReader.h:51
KVSeqCollection * GetSimuInfo()
virtual Bool_t ReadHeader()
KVString root_file_name
Definition: KVSimReader.h:63
Int_t nevt
Definition: KVSimReader.h:62
void WriteObjects()
KVUnownedList * linked_objects
liste d'objets a enregistree avec l'arbre
Definition: KVSimReader.h:72
KVUnownedList * linked_info
Liste d'info (TNamed) enregistree dans l arbre.
Definition: KVSimReader.h:71
void DeclareTree(Option_t *option)
KVString tree_name
Definition: KVSimReader.h:63
KVString tree_title
Definition: KVSimReader.h:63
virtual Bool_t ReadNucleus()
virtual Bool_t ReadEvent()
void AddInfo(const KVNamedParameter &)
TString fOutputDirectory
where to save converted events
Definition: KVSimReader.h:67
void Run(Option_t *option="recreate")
KVSeqCollection * GetLinkedObjects()
void CleanAll()
Definition: KVSimReader.cpp:87
KVString branch_name
Definition: KVSimReader.h:63
KVSimNucleus * nuc
Definition: KVSimReader.h:60
static KVSimReader * MakeSimReader(const char *model_uri)
Bool_t fMultiFiles
Definition: KVSimReader.h:65
virtual Bool_t HasToFill()
Definition: KVSimReader.h:127
Int_t fFileIndex
Definition: KVSimReader.h:66
void AddObject(TObject *obj)
void CreateObjectList()
void WriteInfo()
virtual void ConvertEventsInFile(KVString filename)
Method called by constructors with KVString filename argument.
Definition: KVSimReader.cpp:57
void CreateInfoList()
KVSimReader()
Default constructor.
Definition: KVSimReader.cpp:46
KVSimEvent * evt
Definition: KVSimReader.h:59
virtual ~KVSimReader()
Destructor.
Definition: KVSimReader.cpp:72
TTree * GetTree()
Definition: KVSimReader.h:119
virtual KVString GetDate()
Definition: KVSimReader.h:97
KVNameValueList * nv
Definition: KVSimReader.h:74
virtual void ReadFile()
virtual void ConvertAndSaveEventsInFile(KVString filename)
Read events, convert and save in ROOT file.
virtual void SaveTree()
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:73
Extended TList class which does not own its objects by default.
Definition: KVUnownedList.h:20
void SetName(const char *name)
virtual Int_t GetEntries() const
void Add(TObject *obj) override
virtual void Info(const char *method, const char *msgfmt,...) const
Longptr_t ExecPlugin(int nargs)
virtual TObject * RemoveAt(Int_t idx)
void Start(Bool_t reset=kTRUE)
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
const char * Data() const
virtual UserGroup_t * GetUserInfo(const char *user=nullptr)
virtual void ResetBranchAddress(TBranch *)
Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0) const override
virtual TList * GetUserInfo()
str tree_name
ClassImp(TPyArg)