KaliVeda
Toolkit for HIC analysis
KVIDGridManager.cpp
1 /***************************************************************************
2  KVIDGridManager.cpp - description
3  -------------------
4  begin : Jan 27 2005
5  copyright : (C) 2005 by J.D. Frankland
6  email : frankland@ganil.fr
7 
8 $Id: KVIDGridManager.cpp,v 1.13 2009/03/03 14:27:15 franklan Exp $
9 ***************************************************************************/
10 
11 /***************************************************************************
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * *
18  ***************************************************************************/
19 
20 #include "KVIDGridManager.h"
21 #include "KVString.h"
22 #include "TClass.h"
23 #include "TROOT.h"
24 
25 using namespace std;
26 
28 KVIDGridManager* gIDGridManager = nullptr;
29 
30 
35 
37 {
38  //Default constructor
39  //Initialise global pointer gIDGridManager
40  //Create list for ID grids
41  gIDGridManager = this;
42  fGrids.SendModifiedSignals(kTRUE);
43  fGrids.Connect("Modified()", "KVIDGridManager", this, "Modified()");
44  fGrids.SetOwner(kFALSE);
45 }
46 
47 
48 
52 
53 KVIDGridManager::~KVIDGridManager()
54 {
55  //Destructor
56  //Reset global pointer gIDGridManager
57 
58  if (gIDGridManager == this)
59  gIDGridManager = 0;
60  fGrids.Disconnect("Modified()", this, "Modified()");
61  fLastReadGrids.Clear();
62 }
63 
64 
65 
68 
70 {
71  // Add a grid to the collection. It will be deleted by the manager.
72 
73  fGrids.Add(grid);
74 }
75 
76 
77 
82 
84 {
85  //Remove grid from manager's list and delete it
86  //update flag allows to disable the emission of the 'Modified' signal in case the GUI
87  //is deleting a list of grids - in this case we don't want to update until the end
88 
89  if (!update) fGrids.Disconnect("Modified()", this, "Modified()");
90  fGrids.Remove(grid);
91  delete grid;
92  if (!update) fGrids.Connect("Modified()", "KVIDGridManager", this, "Modified()");
93 }
94 
95 
96 
99 
101 {
102  //Delete all grids and empty list, ready to start anew
103 
104  fGrids.Disconnect("Modified()", this, "Modified()");
105  fLastReadGrids.Clear();
106  fGrids.Clear();
107  Modified(); // emit signal to say something changed
108  fGrids.Connect("Modified()", "KVIDGridManager", this, "Modified()");
109 }
110 
111 
112 
124 
126 {
127  //read file, create grids corresponding to information in file.
128  //note: any existing grids are not destroyed, use Clear() beforehand if you want to
129  //start afresh and anew (ais athat aOK?)
130  //
131  // N.B. the links between each grid and the IDtelescope(s) for which it is to be used
132  // are not set up by this method.
133  //
134  //the list of grids created by reading the file can be accessed with method
135  //GetLastReadGrids() after calling this method
136 
137  // clear list of read grids
138  fLastReadGrids.Clear();
139 
140  Bool_t is_it_ok = kFALSE;
141  ifstream gridfile(filename);
142  if (!gridfile.good()) {
143  Error("ReadAsciiFile", "File %s cannot be opened", filename);
144  return is_it_ok;
145  }
146 
147  KVString s;
148 
149  fGrids.Disconnect("Modified()", this, "Modified()");
150  while (gridfile.good()) {
151  //read a line
152  s.ReadLine(gridfile);
153  if (s.BeginsWith("++")) {
154  //New grid
155  //Get name of class by stripping off the '+' at the start of the line
156  s.Remove(0, 2);
157  /************ BACKWARDS COMPATIBILITY FIX *************
158  Old grid files may contain obsolete KVIDZGrid class
159  We replace by KVIDZAGrid with SetOnlyZId(kTRUE)
160  */
161  //Make new grid using this class
162  KVIDGraph* grid = 0;
163  Bool_t onlyz = kFALSE;
164  if (s == "KVIDZGrid") {
165  s = "KVIDZAGrid";
166  onlyz = kTRUE;
167  }
168  TClass* clas = TClass::GetClass(s.Data());
169  if (!clas) {
170  Fatal("ReadAsciiFile",
171  "Cannot load TClass information for %s", s.Data());
172  }
173  grid = (KVIDGraph*) clas->New();
174  fLastReadGrids.Add(grid);
175  //read grid
176  grid->ReadFromAsciiFile(gridfile);
177  if (onlyz) grid->SetOnlyZId(kTRUE);
178  }
179  }
180 
181  gridfile.close();
182  is_it_ok = kTRUE;
183  Modified(); // emit signal to say something changed
184  fGrids.Connect("Modified()", "KVIDGridManager", this, "Modified()");
185  return is_it_ok;
186 }
187 
188 
189 
195 
196 Int_t KVIDGridManager::WriteAsciiFile(const Char_t* filename, const TCollection* selection)
197 {
198  // Write grids in file 'filename'.
199  // If selection=0 (default), write all grids.
200  // If selection!=0, write only grids in list.
201  // Returns number of grids written in file.
202 
203  ofstream gridfile(filename);
204 
205  const TCollection* list_of_grids = (selection ? selection : &fGrids);
206  TIter next(list_of_grids);
207  KVIDGraph* grid = 0;
208  Int_t n_saved = 0;
209  while ((grid = (KVIDGraph*) next())) {
210 
211  grid->WriteToAsciiFile(gridfile);
212  Info("WriteAsciiFile", "%s saved", grid->GetName());
213  n_saved++;
214 
215  }
216 
217  gridfile.close();
218  return n_saved;
219 }
220 
221 
222 
225 
227 {
228  //Return pointer to grid with name "name"
229  return (KVIDGraph*) GetGrids()->FindObjectByName(name);
230 }
231 
232 
233 
236 
238 {
239  //Opens GUI for managing grids
240  if (gROOT->IsBatch()) {
241  Warning("StartViewer", "To launch graphical interface, you should not use ROOT in batch mode");
242  return;
243  }
244  TClass* cl = TClass::GetClass("KVIDGridManagerGUI");
245  cl->New();
246 }
247 
248 
249 
253 
255 {
256  // Replace contents of KVString with a comma-separated list of all
257  // different labels of ID telescopes associated with current list of ID grids.
258 
259  list = "";
260  TIter next(&fGrids);
261  KVIDGraph* grid = 0;
262  KVString lab;
263  while ((grid = (KVIDGraph*) next())) {
264  //cout << "grid=" << grid->GetName() << " label=" << grid->GetIDTelescopeLabel() << endl;
265  lab.Form("/%s/", grid->GetIDTelescopeLabel());
266  if (!list.Contains(lab)) list.Append(lab);
267  }
268  //cout << "list=" << list << endl;
269  list.ReplaceAll("//", ",");
270  list.ReplaceAll("/", "");
271  if (list.EqualTo(",")) list = "";
272 }
273 
274 
275 
279 
281 {
282  // Initialize all grids in ID grid manager's list, i.e. we call the Initialize() method
283  // of every grid/graph.
284  TIter next(&fGrids);
285  KVIDGraph* gr = 0;
286  while ((gr = (KVIDGraph*) next())) gr->Initialize();
287 }
288 
289 
290 
int Int_t
bool Bool_t
char Char_t
constexpr Bool_t kFALSE
constexpr Bool_t kTRUE
const char Option_t
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
char name[80]
#define gROOT
Base class for particle identification in a 2D map.
Definition: KVIDGraph.h:32
virtual void WriteToAsciiFile(std::ofstream &gridfile)
Definition: KVIDGraph.cpp:442
virtual void ReadFromAsciiFile(std::ifstream &gridfile)
Definition: KVIDGraph.cpp:601
const Char_t * GetName() const override
Definition: KVIDGraph.cpp:1332
const Char_t * GetIDTelescopeLabel() const
Definition: KVIDGraph.h:430
virtual void SetOnlyZId(Bool_t yes=kTRUE)
Definition: KVIDGraph.cpp:1496
Handles a stock of identification grids to be used by one or more identification telescopes.
void Clear(Option_t *opt="") override
Delete all grids and empty list, ready to start anew.
void GetListOfIDTelescopeLabels(KVString &)
void DeleteGrid(KVIDGraph *, Bool_t update=kTRUE)
Int_t WriteAsciiFile(const Char_t *filename, const TCollection *selection=0)
Bool_t ReadAsciiFile(const Char_t *filename)
void StartViewer() const
Opens GUI for managing grids.
KVIDGraph * GetGrid(const Char_t *name)
Return pointer to grid with name "name".
void Initialize(Option_t *="")
void AddGrid(KVIDGraph *)
Add a grid to the collection. It will be deleted by the manager.
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:73
void * New(ENewType defConstructor=kClassNew, Bool_t quiet=kFALSE) const
static TClass * GetClass(Bool_t load=kTRUE, Bool_t silent=kFALSE)
Bool_t EqualTo(const char *cs, ECaseCompare cmp=kExact) const
TString & Append(char c, Ssiz_t rep=1)
void Form(const char *fmt,...)
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
TString & ReplaceAll(const char *s1, const char *s2)
TGraphErrors * gr
void Error(const char *location, const char *fmt,...)
void Info(const char *location, const char *fmt,...)
void Fatal(const char *location, const char *fmt,...)
void Warning(const char *location, const char *fmt,...)
void update(const LAYERDATA &prevLayerData, LAYERDATA &currLayerData, double factorWeightDecay, EnumRegularization regularization)
ClassImp(TPyArg)