KaliVeda  1.12/06
Heavy-Ion Analysis Toolkit
KVIDGridManager.cpp
Go to the documentation of this file.
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 "Riostream.h"
22 #include "KVString.h"
23 #include "TClass.h"
24 #include "TROOT.h"
25 
26 using namespace std;
27 
30 
31 
36 
38 {
39  //Default constructor
40  //Initialise global pointer gIDGridManager
41  //Create list for ID grids
42  gIDGridManager = this;
43  fGrids = new KVList;
44  fGrids->SendModifiedSignals(kTRUE);
45  fGrids->Connect("Modified()", "KVIDGridManager", this, "Modified()");
46  fGrids->SetCleanup();
47 }
48 
49 
50 
55 
56 KVIDGridManager::~KVIDGridManager()
57 {
58  //Destructor
59  //Reset global pointer gIDGridManager
60  //Delete list of ID grids (this deletes all the grids)
61  Info("~KVIDGridManager", "DELETING ID GRID MANAGER");
62  if (gIDGridManager == this)
63  gIDGridManager = 0;
64  fGrids->Disconnect("Modified()", this, "Modified()");
65  fLastReadGrids.Clear();
66  fGrids->Delete();
67  delete fGrids;
68 }
69 
70 
71 
74 
76 {
77  // Add a grid to the collection. It will be deleted by the manager.
78 
79  fGrids->Add(grid);
80 }
81 
82 
83 
88 
90 {
91  //Remove grid from manager's list and delete it
92  //update flag allows to disable the emission of the 'Modified' signal in case the GUI
93  //is deleting a list of grids - in this case we don't want to update until the end
94 
95  if (!update) fGrids->Disconnect("Modified()", this, "Modified()");
96  fGrids->Remove(grid);
97  delete grid;
98  if (!update) fGrids->Connect("Modified()", "KVIDGridManager", this, "Modified()");
99 }
100 
101 
102 
105 
107 {
108  //Delete all grids and empty list, ready to start anew
109  Info("Clear", "DELETING ALL GRIDS IN IDGRIDMANAGER");
110  fGrids->Disconnect("Modified()", this, "Modified()");
111  fLastReadGrids.Clear();
112  fGrids->Delete();
113  Modified(); // emit signal to say something changed
114  fGrids->Connect("Modified()", "KVIDGridManager", this, "Modified()");
115 }
116 
117 
118 
130 
132 {
133  //read file, create grids corresponding to information in file.
134  //note: any existing grids are not destroyed, use Clear() beforehand if you want to
135  //start afresh and anew (ais athat aOK?)
136  //
137  // N.B. the links between each grid and the IDtelescope(s) for which it is to be used
138  // are not set up by this method.
139  //
140  //the list of grids created by reading the file can be accessed with method
141  //GetLastReadGrids() after calling this method
142 
143  // clear list of read grids
144  fLastReadGrids.Clear();
145 
146  Bool_t is_it_ok = kFALSE;
147  ifstream gridfile(filename);
148  if (!gridfile.good()) {
149  Error("ReadAsciiFile", "File %s cannot be opened", filename);
150  return is_it_ok;
151  }
152 
153  KVString s;
154 
155  fGrids->Disconnect("Modified()", this, "Modified()");
156  while (gridfile.good()) {
157  //read a line
158  s.ReadLine(gridfile);
159  if (s.BeginsWith("++")) {
160  //New grid
161  //Get name of class by stripping off the '+' at the start of the line
162  s.Remove(0, 2);
163  /************ BACKWARDS COMPATIBILITY FIX *************
164  Old grid files may contain obsolete KVIDZGrid class
165  We replace by KVIDZAGrid with SetOnlyZId(kTRUE)
166  */
167  //Make new grid using this class
168  KVIDGraph* grid = 0;
169  Bool_t onlyz = kFALSE;
170  if (s == "KVIDZGrid") {
171  s = "KVIDZAGrid";
172  onlyz = kTRUE;
173  }
174  TClass* clas = TClass::GetClass(s.Data());
175  if (!clas) {
176  Fatal("ReadAsciiFile",
177  "Cannot load TClass information for %s", s.Data());
178  }
179  grid = (KVIDGraph*) clas->New();
180  fLastReadGrids.Add(grid);
181  //read grid
182  grid->ReadFromAsciiFile(gridfile);
183  if (onlyz) grid->SetOnlyZId(kTRUE);
184  }
185  }
186 
187  gridfile.close();
188  is_it_ok = kTRUE;
189  Modified(); // emit signal to say something changed
190  fGrids->Connect("Modified()", "KVIDGridManager", this, "Modified()");
191  return is_it_ok;
192 }
193 
194 
195 
201 
202 Int_t KVIDGridManager::WriteAsciiFile(const Char_t* filename, const TCollection* selection)
203 {
204  // Write grids in file 'filename'.
205  // If selection=0 (default), write all grids.
206  // If selection!=0, write only grids in list.
207  // Returns number of grids written in file.
208 
209  ofstream gridfile(filename);
210 
211  const TCollection* list_of_grids = (selection ? selection : fGrids);
212  TIter next(list_of_grids);
213  KVIDGraph* grid = 0;
214  Int_t n_saved = 0;
215  while ((grid = (KVIDGraph*) next())) {
216 
217  grid->WriteToAsciiFile(gridfile);
218  Info("WriteAsciiFile", "%s saved", grid->GetName());
219  n_saved++;
220 
221  }
222 
223  gridfile.close();
224  return n_saved;
225 }
226 
227 
228 
231 
233 {
234  //Return pointer to grid with name "name"
235  return (KVIDGraph*) GetGrids()->FindObjectByName(name);
236 }
237 
238 
239 
242 
244 {
245  //Opens GUI for managing grids
246  if (gROOT->IsBatch()) {
247  Warning("StartViewer", "To launch graphical interface, you should not use ROOT in batch mode");
248  return;
249  }
250  TClass* cl = TClass::GetClass("KVIDGridManagerGUI");
251  cl->New();
252 }
253 
254 
255 
259 
261 {
262  // Replace contents of KVString with a comma-separated list of all
263  // different labels of ID telescopes associated with current list of ID grids.
264 
265  list = "";
266  TIter next(fGrids);
267  KVIDGraph* grid = 0;
268  KVString lab;
269  while ((grid = (KVIDGraph*) next())) {
270  //cout << "grid=" << grid->GetName() << " label=" << grid->GetIDTelescopeLabel() << endl;
271  lab.Form("/%s/", grid->GetIDTelescopeLabel());
272  if (!list.Contains(lab)) list.Append(lab);
273  }
274  //cout << "list=" << list << endl;
275  list.ReplaceAll("//", ",");
276  list.ReplaceAll("/", "");
277  if (list.EqualTo(",")) list = "";
278 }
279 
280 
281 
285 
287 {
288  // Initialize all grids in ID grid manager's list, i.e. we call the Initialize() method
289  // of every grid/graph.
290  TIter next(fGrids);
291  KVIDGraph* gr = 0;
292  while ((gr = (KVIDGraph*) next())) gr->Initialize();
293 }
294 
295 
296 
int Int_t
KVIDGridManager * gIDGridManager
ClassImp(KVPartitionList) void KVPartitionList
Initialisation.
char Char_t
const Bool_t kFALSE
bool Bool_t
const Bool_t kTRUE
const char Option_t
#define gROOT
Base class for particle identification in a 2D map.
Definition: KVIDGraph.h:31
virtual void WriteToAsciiFile(std::ofstream &gridfile)
Definition: KVIDGraph.cpp:459
virtual void ReadFromAsciiFile(std::ifstream &gridfile)
Definition: KVIDGraph.cpp:616
const Char_t * GetIDTelescopeLabel() const
Definition: KVIDGraph.h:402
const Char_t * GetName() const
Definition: KVIDGraph.cpp:1320
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.
void GetListOfIDTelescopeLabels(KVString &)
void DeleteGrid(KVIDGraph *, Bool_t update=kTRUE)
void Clear(Option_t *opt="")
Delete all grids and empty list, ready to start anew.
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.
Extended TList class which owns its objects by default.
Definition: KVList.h:27
virtual void SendModifiedSignals(Bool_t yes=kTRUE)
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:72
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
const long double s
Definition: KVUnits.h:94
const long double cl
Definition: KVUnits.h:85
void Info(const char *location, const char *va_(fmt),...)
void Error(const char *location, const char *va_(fmt),...)
void Fatal(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)