KaliVeda  1.12/06
Heavy-Ion Analysis Toolkit
KVRTGIDManager.cpp
Go to the documentation of this file.
1 //Created by KVClassFactory on Thu Oct 11 11:51:14 2012
2 //Author: Dijon Aurore
3 
4 #include "KVRTGIDManager.h"
5 #include "KVTGIDZ.h"
6 #include "KVTGIDGrid.h"
7 #include "KVIDGridManager.h"
8 #include "KVMultiDetArray.h"
9 #include "KVIDTelescope.h"
10 using namespace std;
11 
13 
14 
15 
16 
18 
19 
26 
28 {
29  // Default constructor
30  // The list fIDList is not owner anymore.
31  // Now the identification function belong to fIDGlobalList,
32  // the list return by GetListOfIDFunctions() is not the owner
33  // of identification functions anymore.
34 
35  fIDList.SetOwner(kFALSE);
36 
37 }
38 
39 
40 
45 
47 {
48  // Destructor
49  // Remove the global list of identification functions if
50  // it is empty.
51 
52  if (fIDGlobalList && !fIDGlobalList->GetEntries()) SafeDelete(fIDGlobalList);
53 }
54 
55 
56 
61 
63 {
64  // Add an identification object to the global list (fIDGlobalList).
65  // The identification object is not added to the local list (fIDList) in this method anymore. To do that, see SetIDFunctionInTelescopes)
66  // This method just calls AddTGIDToGlobalList(KVTGID *);
67 
68  AddTGIDToGlobalList(_tgid);
69 }
70 
71 
72 
83 
85  const Char_t* id_type,
86  const Char_t* grid_type)
87 {
88  //Overrides the same method of KVTGIDManager. This method
89  //Retrieve the identification object using:
90  // id_type = type of identification ("Z", "A", "A_for_Z=3", etc.)
91  // grid_type = type of identification grid ("GG", "PG1", etc.)
92  // but the name of ID telescope is not used anymore because identification
93  // object can be associated to several ID telescopes.
94  // If any object is found then it returns the first:
95  // - KVTGIDZA object if id_type contains "A"
96  // - KVTGIDZ object else.
97 
98  Char_t cname[9] = "KVTGIDZ";
99  TString buff = id_type;
100 
101  if (buff.Contains("A")) strcpy(cname, "KVTGIDZA");
102 
103  buff.Form("_%s_%s", id_type, grid_type);
104 
105  TIter next(&fIDList);
106  KVTGID* tgid = NULL;
107  KVTGID* tgid2 = NULL;
108  TString name;
109 
110  while ((tgid = (KVTGID*)next())) {
111  name = tgid->GetName();
112  if (name.Contains(buff)) return tgid;
113  if (!tgid2 && !strcmp(tgid->ClassName(), cname)) tgid2 = tgid;
114  }
115 
116  return tgid2;
117 }
118 
119 
121 
122 
123 
126 {
127  // Add an identification object to the local list (fIDList).
128  KVTGIDManager::AddTGID(_tgid);
129 }
130 
131 
132 
135 
137 {
138  //Remove the identification object with name from the local list.
139 
140  KVTGID* tgid = 0;
141  // Remove the function from the current list
142  if (!(tgid = (KVTGID*)fIDList.FindObject(tgid))) return;
143  fIDList.Remove(tgid);
144  //recalculate fID_max if needed
145  if (tgid->GetIDmax() == fID_max) {
146  fID_max = 0.;
147  TIter n(&fIDList);
148  KVTGID* t;
149  while ((t = (KVTGID*)n())) fID_max = TMath::Max(fID_max, t->GetIDmax());
150  }
151 }
152 
153 
154 
156 
158 {
159  if (!fIDGlobalList) return;
160 
161  //Delete the identification object with name from the global list.
162  KVTGID* tgid = 0;
163 
164  // Delete the function from the global list
165  if (!(tgid = (KVTGID*)fIDGlobalList->FindObject(name))) return;
166  fIDGlobalList->Remove(tgid);
167  delete tgid;
168  if (!fIDGlobalList->GetEntries()) SafeDelete(fIDGlobalList);
169 }
170 
171 
172 
177 
179 {
180  // Fill a sublist of TGID found in object inheriting
181  // from KVTGIDGrid in gIDGridManager.
182  // Return kFALSE if gIDGridManager is not defined.
183 
184  if (!gIDGridManager) return kFALSE;
185 
186  tgid_list->SetOwner(kFALSE);
187  KVList* grid_list = NULL;
189  grid_list = gm->GetGrids();
190  TIter next_g(grid_list);
191  TObject* obj = NULL;
192  KVTGID* tgid = NULL;
193  while ((obj = next_g())) {
194  if (obj->InheritsFrom("KVTGIDGrid")) {
195  tgid = const_cast <KVTGID*>(((KVTGIDGrid*)obj)->GetTGID());
196  if (tgid)
197  tgid_list->Add(tgid);
198  }
199  }
200  return kTRUE;
201 }
202 
203 
204 
208 
210 {
211 
212  //Read file, create KVTGID fits corresponding to information in file.
213  //Comment line beginning by '#' is skipped.
214 
215  ifstream fitfile(filename);
216  if (!fitfile.good()) {
217  Error("KVRTGIDManager::ReadAsciiFile", "File %s cannot be opened", filename);
218  return kFALSE;
219  }
220  KVString s, cname;
221  while (fitfile.good()) {
222  //read a line
223  s.ReadLine(fitfile);
224 
225  if (s.BeginsWith("#")) continue;
226  if (s.BeginsWith("++")) {
227  //New fit
228  //Get name of class by stripping off the '+' at the start of the line
229  s.Remove(0, 2);
230  cname = s;
231  cname.Remove(s.Index("::"));
232  TClass* cl = TClass::GetClass(cname.Data());
233  if (cl && cl->InheritsFrom("KVTGID")) {
234 
235  s.Remove(0, s.Index("::") + 2);
236 
237  // Make new identification function
238  KVTGID* fit = NULL;
239  fit = KVTGID::ReadFromAsciiFile(s.Data(), fitfile);
240  AddTGIDToGlobalList(fit);
241 
242  // when mass identification is possible, make a copy
243  // for Z identification
244  if (!fit->GetZorA()) {
245  KVTGIDZ* fitz = new KVTGIDZ(*fit);
246  fitz->SetTitle(Form("COPY from %p", fit));
247  AddTGIDToGlobalList(fitz);
248  }
249  }
250  }
251  }
252  fitfile.close();
253  return kTRUE;
254 }
255 
256 
257 
260 
262 {
263  //Remove all identification objects of the local list.
264  fIDList.Clear();
265  fID_max = 0.;
266 }
267 
268 
269 
272 
274 {
275 // Add an identification object to the global list (fIDGlobalList).
276 
277  if (!tgid) return;
278  if (!fIDGlobalList) {
279  fIDGlobalList = new KVList;
280  fIDGlobalList->SetCleanup();
281  }
282  fIDGlobalList->Add(tgid);
283 }
284 
285 
286 
305 
306 void KVRTGIDManager::BuildGridForAllTGID(const Char_t* idtype, Double_t xmin, Double_t xmax, Int_t ID_min, Int_t ID_max, Int_t npoints, Bool_t logscale)
307 {
308  // Build a grid (KVTGIDGrid) for all the identification functions
309  // of the global list. The new grids are automatically loaded in
310  // gIDGridManager and are visible in the Grid Manager GUI.
311  // If a function is already associated to a grid then a new grid
312  // is not built. No grid is built for copies of KVTGID's made in
313  // the method ReadAsciiFile(...).
314  //
315  // Inputs: idtype - type of the identification for which the
316  // grids will be built (CI-SI, SI-CSI, CI-CSI,
317  // SI75-SILI, ...). By default, all grids are
318  // built
319  // xmin
320  // xmax
321  // ID_min
322  // ID_max
323  // npoints
324  // logscale - see KVTGIDGrid::Generate(...)
325 
326  if (!fIDGlobalList) return;
327 
328  // First make a sublist of TGID found in object inheriting
329  // from KVTGIDGrid in gIDGridManager
330  TList tgid_list;
331  GetTGIDfromIDGridManager(&tgid_list);
333  KVList* grid_list = NULL;
334  if (gm) {
335  grid_list = gm->GetGrids();
336  grid_list->Disconnect("Modified()", gm, "Modified()");
337  }
338  // If the TGID of the global list is not in the sublist then
339  // build grid
340  TIter next(fIDGlobalList);
341  Bool_t IDtypeOK = strcmp(idtype, "");
342  KVTGID* tgid = NULL;
343  while ((tgid = (KVTGID*)next())) {
344  if (tgid_list.FindObject(tgid)) continue;
345 
346  if (IDtypeOK) {
347  KVBase* idt = NULL;
348  TSeqCollection* idt_list = (TSeqCollection*)GetIDTelescopesForTGID(tgid);
349  if (!idt_list) continue;
350  if (!(idt = (KVBase*)idt_list->First())) continue;
351  SafeDelete(idt_list);
352  if (strcmp(idtype, idt->GetLabel())) continue;
353  }
354  // Not built grid for a KVTGID copy
355  TString tmp = tgid->GetTitle();
356  if (tmp.Contains("COPY")) {
357  tmp.Remove(0, tmp.Index("0x"));
358  KVTGID* tmp_tgid = reinterpret_cast<KVTGID*>((Int_t)tmp.Atof());
359  Warning("KVRTGIDManager::BuildGridForAllTGID", "No grid built for %s (%s, %p) because it is a copy of %s (%s, %p)"
360  , tgid->GetName(), tgid->ClassName(), tgid
361  , tmp_tgid->GetName(), tmp_tgid->ClassName(), tmp_tgid);
362  continue;
363  }
364  KVTGIDGrid* grid = new KVTGIDGrid(tgid);
365  grid->SetOnlyZId((Bool_t)tgid->GetZorA());
366  if (tgid->GetZorA()) grid->SetMassFormula(tgid->GetMassFormula());
367  grid->Generate(xmax, xmin, ID_min, ID_max, npoints, logscale);
368  Info("KVRTGIDManager::BuildGridForAllTGID", "grid built from its TGID function %s (%s, %p)"
369  , tgid->GetName(), tgid->ClassName(), tgid);
370  }
371  if (grid_list) grid_list->Connect("Modified()", "KVIDGridManager", gm, "Modified()");
372  gm->Modified();
373 }
374 
375 
376 
380 
382 {
383  //Delete the global list of identification objects. Since this
384  //list is owner then all listed identification objects are deleted.
385 
386  SafeDelete(fIDGlobalList);
387 }
388 
389 
390 
394 
396 {
397  // For each identification function of the global list which is valid for this run, we add it in the associated telescope which
398  // have to inherits from KVTGIDManager and from a KVIDTelescope.
399 
400  TIter next(fIDGlobalList);
401  KVTGID* tgid = NULL;
402 
403  while ((tgid = (KVTGID*)next())) {
404 
405  if (!tgid->IsValidForRun(run)) continue;
406 
407  // this list have to be deleted after use
408  TCollection* lidtel = GetIDTelescopesForTGID(tgid);
409  TIter nextidt(lidtel);
410  TObject* idt = NULL;
411  KVRTGIDManager* tgidm = NULL;
412 
413  while ((idt = nextidt())) {
414  if (!idt->InheritsFrom("KVRTGIDManager")) {
415  Error("KVRTGIDManager::SetIDFuncInTelescopes", "The IDtelescope %s does not inherit from KVRTGIDManager", idt->GetName());
416  continue;
417  }
418  tgidm = (KVRTGIDManager*)idt->IsA()->DynamicCast(KVRTGIDManager::Class(), idt);
419  tgidm->SetTGID(tgid);
420  }
421  delete lidtel;
422  }
423 }
424 
425 
426 
431 
433 {
434  // Returns the number of identification functions found
435  // in grids of gIDGridManager which were missing in the
436  // the global list and updates this list.
437 
438  TList tgid_list;
439  if (!GetTGIDfromIDGridManager(&tgid_list)) return 0;
440 
441  // If the TGID of tgid_list is not in the global list then
442  // add it
443  TIter next(&tgid_list);
444  KVTGID* tgid = NULL;
445  Int_t ntgid = 0;
446  while ((tgid = (KVTGID*)next())) {
447  if (!fIDGlobalList) {
448  AddTGIDToGlobalList(tgid);
449  ntgid++;
450  continue;
451  }
452  if (fIDGlobalList->FindObject(tgid)) continue;
453  AddTGIDToGlobalList(tgid);
454  ntgid++;
455  }
456  return ntgid;
457 }
458 
459 
460 
467 
468 Int_t KVRTGIDManager::WriteAsciiFile(const Char_t* filename, const TCollection* selection, Bool_t update)
469 {
470  // Write identification functions in file 'filename'.
471  // If selection=0 (default), write all grids.
472  // If update=true, call UpdateListFromIDGridManager() before writing
473  // If selection!=0, write only grids in list.
474  // Returns number of functions written in file.
475 
476  if (update) UpdateListFromIDGridManager();
477  if (!fIDGlobalList) {
478  Warning("KVRTGIDManager::WriteAsciiFile", "No listed identification functions to write");
479  return 0;
480  }
481 
482  ofstream tgidfile(filename);
483  if (!tgidfile.is_open()) {
484  Error("KVRTGIDManager::WriteAsciiFile", "No write permission for file %s", filename);
485  return 0;
486  }
487 
488  const TCollection* list_tgid = (selection ? selection : fIDGlobalList);
489  TIter next(list_tgid);
490  KVTGID* tgid = NULL;
491  Int_t n_saved = 0;
492 
493  while ((tgid = (KVTGID*)next())) {
494 
495  // Not write a KVTGID copy
496  TString tmp = tgid->GetTitle();
497  if (tmp.Contains("COPY")) {
498  tmp.Remove(0, tmp.Index("0x"));
499  KVTGID* tmp_tgid = reinterpret_cast<KVTGID*>((Int_t)tmp.Atof());
500  Warning("KVRTGIDManager::WriteAsciiFile", "The function %s (%s, %p) is not written because it is a copy of %s (%s, %p)"
501  , tgid->GetName(), tgid->ClassName(), tgid
502  , tmp_tgid->GetName(), tmp_tgid->ClassName(), tmp_tgid);
503  continue;
504  }
505  tgid->WriteToAsciiFile(tgidfile);
506  Info("KVRTGIDManager::WriteAsciiFile", "%s (%s, %p) saved", tgid->GetName(), tgid->ClassName(), tgid);
507  n_saved++;
508  }
509 
510  tgidfile.close();
511  return n_saved;
512 }
513 
514 
515 
521 
523 {
524  // Return TList of all ID telescopes for which fit is valid.
525  // Returns 0 if no telescopes are defined, or if gMultiDetArray object is not defined
526  // (we search for the telescopes using their name and gMultiDetArray->GetIDTelescope()).
527  // WARNING: DELETE this list after use !!!
528  if (tgid->GetIDTelescopes() == "/" || !gMultiDetArray) return 0;
529  TList* list = new TList;
530  tgid->GetIDTelescopes().Begin("/");
531  while (!tgid->GetIDTelescopes().End()) {
533  }
534  return list;
535 }
536 
537 
int Int_t
unsigned int UInt_t
KVIDGridManager * gIDGridManager
KVMultiDetArray * gMultiDetArray
ClassImp(KVPartitionList) void KVPartitionList
Initialisation.
#define SafeDelete(p)
char Char_t
const Bool_t kFALSE
bool Bool_t
double Double_t
const Bool_t kTRUE
float xmin
float xmax
char * Form(const char *fmt,...)
Base class for KaliVeda framework.
Definition: KVBase.h:135
const Char_t * GetLabel() const
Definition: KVBase.h:192
void SetMassFormula(Int_t)
Definition: KVIDGraph.cpp:1461
virtual void SetOnlyZId(Bool_t yes=kTRUE)
Definition: KVIDGraph.cpp:1484
Handles a stock of identification grids to be used by one or more identification telescopes.
KVList * GetGrids()
Extended TList class which owns its objects by default.
Definition: KVList.h:27
KVIDTelescope * GetIDTelescope(const Char_t *name) const
Return pointer to DeltaE-E ID Telescope with "name".
Run-dependant KVTGIDManager.
static void BuildGridForAllTGID(const Char_t *idtype="", Double_t xmin=0., Double_t xmax=4096., Int_t ID_min=0, Int_t ID_max=0, Int_t npoints=50, Bool_t logscale=kTRUE)
static void SetIDFuncInTelescopes(UInt_t run)
virtual ~KVRTGIDManager()
static Int_t WriteAsciiFile(const Char_t *filename, const TCollection *selection=0, Bool_t update=kTRUE)
static Int_t UpdateListFromIDGridManager()
virtual KVTGID * GetTGID(const Char_t *idt_name, const Char_t *id_type, const Char_t *grid_type)
static TCollection * GetIDTelescopesForTGID(KVTGID *)
static Bool_t ReadAsciiFile(const Char_t *filename)
virtual void SetTGID(KVTGID *)
Add an identification object to the local list (fIDList).
virtual void RemoveTGID(const Char_t *name)
Remove the identification object with name from the local list.
static void Clear()
virtual void AddTGID(KVTGID *)
static Bool_t GetTGIDfromIDGridManager(TList *tgid_list)
virtual void RemoveAllTGID()
Remove all identification objects of the local list.
static void DeleteTGID(const Char_t *name)
static KVList * fIDGlobalList
Global list of TGID of all the KVRTGIDManager.
static void AddTGIDToGlobalList(KVTGID *)
----— static methods -----------------------—//
virtual void SetCleanup(Bool_t enable=kTRUE)
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
Grid representing result of fit.
Definition: KVTGIDGrid.h:23
virtual void Generate(Double_t xmax, Double_t xmin, Int_t ID_min=0, Int_t ID_max=0, Int_t npoints=50, Bool_t logscale=kTRUE)
Definition: KVTGIDGrid.cpp:131
virtual void AddTGID(KVTGID *)
Abstract base class for charged particle Z identfication using functionals developed by L....
Definition: KVTGIDZ.h:32
Abstract base class for particle identfication using functionals developed by L. Tassan-Got (IPN Orsa...
Definition: KVTGID.h:43
Bool_t IsValidForRun(Int_t run) const
Definition: KVTGID.h:256
const KVString & GetIDTelescopes() const
Definition: KVTGID.h:295
Double_t GetIDmax() const
Definition: KVTGID.h:120
void WriteToAsciiFile(std::ofstream &) const
Write parameters of LTG fit in file.
Definition: KVTGID.cpp:758
Int_t GetZorA() const
Definition: KVTGID.h:88
static KVTGID * ReadFromAsciiFile(const Char_t *name, std::ifstream &)
Definition: KVTGID.cpp:813
Int_t GetMassFormula() const
Definition: KVTGID.h:239
static TClass * GetClass(Bool_t load=kTRUE, Bool_t silent=kFALSE)
virtual void SetOwner(Bool_t enable=kTRUE)
virtual void SetTitle(const char *title="")
virtual void Add(TObject *obj)
virtual TObject * FindObject(const char *name) const
virtual const char * GetName() const
virtual const char * GetTitle() const
virtual const char * GetName() const
virtual const char * ClassName() const
virtual TObject * FindObject(const char *name) const
virtual Bool_t InheritsFrom(const char *classname) const
virtual TObject * First() const=0
Double_t Atof() const
const char * Data() const
void Form(const char *fmt,...)
TString & Remove(EStripType s, char c)
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
const Int_t n
const long double s
Definition: KVUnits.h:94
const long double cl
Definition: KVUnits.h:85
def fit(model, train_loader, val_loader, num_epochs, batch_size, optimizer, criterion, save_best, scheduler)
void Info(const char *location, const char *va_(fmt),...)
void Error(const char *location, const char *va_(fmt),...)
void Warning(const char *location, const char *va_(fmt),...)
void update(const LAYERDATA &prevLayerData, LAYERDATA &currLayerData, double factorWeightDecay, EnumRegularization regularization)
Double_t Max(Double_t a, Double_t b)