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