KaliVeda
Toolkit for HIC analysis
KVINDRAPulserDataTree.cpp
1 /*
2 $Id: KVINDRAPulserDataTree.cpp,v 1.7 2009/03/27 16:42:58 franklan Exp $
3 $Revision: 1.7 $
4 $Date: 2009/03/27 16:42:58 $
5 */
6 
7 //Created by KVClassFactory on Wed Jan 21 11:56:26 2009
8 //Author: franklan
9 
10 #include "KVINDRAPulserDataTree.h"
11 #include "KVDataSet.h"
12 #include "KVINDRADBRun.h"
13 #include <KVEBYEDAT_ACQParam.h>
14 #include "KVMultiDetArray.h"
15 
16 using namespace std;
17 
19 
20 
21 
22 
23 
27 {
28  // Default constructor
29  fArb = 0;
30  fRun = 0;
31  fVal = 0;
32  fIndex = 0;
33  fRunlist = 0;
34 }
35 
36 
37 
40 
42 {
43  // Destructor
44  if (fVal) delete [] fVal;
45  if (fIndex) {
46  fIndex->Delete();
47  delete fIndex;
48  }
49 }
50 
51 
52 
113 
115 {
116  // Create and fill tree with pulser data.
117  // We look for the following two directories:
118  //
119  // $KVROOT/KVFiles/name_of_dataset/gene_detecteurs
120  // $KVROOT/KVFiles/name_of_dataset/gene_pins
121  //
122  // If not found, we look for the following compressed archives:
123  //
124  // $KVROOT/KVFiles/name_of_dataset/gene_detecteurs.tgz
125  // $KVROOT/KVFiles/name_of_dataset/gene_pins.tgz
126  //
127  // and if found, uncompress them ('tar -zxf').
128  // [[ N.B. in this case the extracted directories will be deleted after reading,
129  // [[ ensuring that if new archives are supplied,
130  // [[ we always use the latest versions of files.
131  //
132  // The default names of these directories are defined in .kvrootrc by:
133  //
134  // KVINDRAPulserDataTree.GeneDetDir: gene_detecteurs
135  // KVINDRAPulserDataTree.GenePinDir: gene_pins
136  //
137  // Dataset-dependent alternatives can be defined using:
138  //
139  // dataset_name.KVINDRAPulserDataTree.GeneDetDir: dataset_specific_value
140  //
141  // The first directory (gene_detecteurs) must contain 1 file per run with names like:
142  //
143  // run8820.gene
144  //
145  // These files are generated using example analysis class GetGeneMean
146  // (see Examples). They contain the mean value of every acquisition parameter
147  // associated with a detector in the run:
148  //
149  // CI_0201_GG 3095.28
150  // CI_0201_PG 275.626
151  // CI_0203_GG 2863.66
152  // CI_0203_PG 263.308
153  // CI_0205_GG 3042.83
154  // etc.
155  //
156  // i.e. 'name of acquisition parameter' 'mean value for run'
157  //
158  // The second directory (gene_pins) must contain 1 file per run with names like:
159  //
160  // run8820.genepin
161  // OR run8820.laserpin
162  // OR run8820.genelaserpin
163  //
164  // These files are generated using example analysis class GetGeneMeanPin
165  // (see Examples). They contain the mean values of the acquisition parameters
166  // associated with pin diodes in the run:
167  //
168  // PILA_01_PG_gene 792.616
169  // PILA_01_PG_laser 1747.18
170  // PILA_01_GG_gene 0
171  // etc.
172  //
173  // We create a TTree with 1 branch for each detector acquisition parameter.
174  // For PILA and SI_PIN parameters, we create a 'gene' and a 'laser' branch for each.
175 
176  fGeneDir = new KVTarArchive(GetDirectoryName("GeneDetDir"), gDataSet->GetDataSetDir());
177  fPinDir = new KVTarArchive(GetDirectoryName("GenePinDir"), gDataSet->GetDataSetDir());
178  if (!fGeneDir->IsOK() && !fPinDir->IsOK()) {
179  Info("Build", "No data available to build pulser data tree");
180  return;
181  }
182  CreateTree();
183  ReadData();
184  delete fGeneDir;
185  delete fPinDir;
186 }
187 
188 
189 
196 
198 {
199  // Returns the name of the directory defined by the .kvrootrc environment variable
200  //
201  // KVINDRAPulserDataTree.[dirvar]
202  // OR
203  // dataset_name.KVINDRAPulserDataTree.[dirvar]:
204 
205  TString search, datasetenv;
206  datasetenv.Form("KVINDRAPulserDataTree.%s", dirvar);
207  search = gDataSet->GetDataSetEnv(datasetenv.Data(), "");
208  if (search == "") {
209  Error("GetDirectoryName", "%s is not defined for dataset %s. Check .kvrootrc files.",
210  datasetenv.Data(), gDataSet->GetName());
211  }
212  return search;
213 }
214 
215 
216 
224 
226 {
227  // Create new TTree with
228  // 1 branch 'Run' with run number
229  // 1 branch for each acquisition parameter of every detector (except time markers)
230  // 2 branches for each 'PILA_...' or 'SI_PIN...' parameter, suffixed with '_laser' and '_gene'
231  //
232  // NB if multidetector has not been built, it will be built by this method
233 
234  fArb = new TTree("PulserData", "Created by KVINDRAPulserDataTree");
235  fArb->SetDirectory(0);
236 
237  fArb->Branch("Run", &fRun, "Run/I");
238 
239  if (!gMultiDetArray) KVMultiDetArray::MakeMultiDetector(gDataSet->GetName());
240 
241  const KVSeqCollection* acq_pars = nullptr;//gIndra->GetACQParams();
242 
243  //fTab_siz = acq_pars->GetEntries() + 20;
244  fVal = new Float_t[fTab_siz];
245  fIndex = new THashTable(20, 5);
246  fIndex->SetOwner(kTRUE);
247 
248  TIter nxtACQ(acq_pars);
249  KVEBYEDAT_ACQParam* ap = 0;
250  Int_t ap_num = 0;
251  KVBase* iob;
252  while ((ap = (KVEBYEDAT_ACQParam*)nxtACQ())) {
253 
254  TString ap_name(ap->GetName());
255  TString ap_type(ap->GetType());
256  if (ap_name.BeginsWith("PILA") || ap_name.BeginsWith("SI_PIN")) {
257  ap_name += "_laser";
258  iob = new KVBase(ap_name.Data());
259  iob->SetNumber(ap_num);
260  fIndex->Add(iob);
261  fArb->Branch(ap_name.Data(), &fVal[ap_num++], Form("%s/F", ap_name.Data()));
262  ap_name.Form("%s%s", ap->GetName(), "_gene");
263  iob = new KVBase(ap_name.Data());
264  iob->SetNumber(ap_num);
265  fIndex->Add(iob);
266  fArb->Branch(ap_name.Data(), &fVal[ap_num++], Form("%s/F", ap_name.Data()));
267  }
268  else if (ap_type != "T") {
269  iob = new KVBase(ap_name.Data());
270  iob->SetNumber(ap_num);
271  fIndex->Add(iob);
272  fArb->Branch(ap_name.Data(), &fVal[ap_num++], Form("%s/F", ap_name.Data()));
273  }
274  if (ap_num > fTab_siz - 2) {
275  Error("CreateTree",
276  "Number of branches to create is greater than estimated (%d). Not all parameters can be treated.",
277  fTab_siz);
278  return;
279  }
280 
281  }
282  //keep number of used 'slots' in array
283  fTab_siz = ap_num;
284 }
285 
286 
287 
290 
292 {
293  // Read data in one file
294 
295  KVString line;
296  line.ReadLine(fin);
297  while (fin.good()) {
298  if (!line.BeginsWith("#")) {
299  line.Begin(" ");
300  KVString br_name = line.Next(kTRUE);
301  Int_t index = GetIndex(br_name.Data());
302  fVal[index] = line.Next(kTRUE).Atof();
303  }
304  line.ReadLine(fin);
305  }
306  fin.close();
307 }
308 
309 
310 
313 
315 {
316  // Read data for one run, fill tree
317 
318  UChar_t msg = 0;
319  fRun = run;
320  if (fGeneDir->IsOK()) {
321  ifstream f;
322  if (OpenGeneData(run, f)) ReadFile(f);
323  else msg = msg | 1;
324  }
325  if (fPinDir->IsOK()) {
326  ifstream f;
327  if (OpenPinData(run, f)) ReadFile(f);
328  else msg = msg | 2;
329  }
330  fArb->Fill();
331  return msg;
332 }
333 
334 
335 
340 
342 {
343  // Open gene data for one run
344  // We look for file 'runXXXX.gene' in the directory given by
345  // environment variable KVINDRAPulserDataTree.GeneDetDir.
346 
347  TString fname;
348  fname.Form("/run%d.gene", run);
349  fname.Prepend(gDataSet->GetDataSetEnv("KVINDRAPulserDataTree.GeneDetDir", ""));
350  return gDataSet->OpenDataSetFile(fname.Data(), f);
351 }
352 
353 
354 
363 
365 {
366  // Open pin data for one run
367  // We look for one of the following files in the directory given by
368  // environment variable KVINDRAPulserDataTree.GenePinDir:
369  //
370  // runXXXX.genepin
371  // OR runXXXX.laserpin
372  // OR runXXXX.genelaserpin
373 
374  TString fname;
375  fname.Form("/run%d.genepin", run);
376  TString pindir(gDataSet->GetDataSetEnv("KVINDRAPulserDataTree.GenePinDir", ""));
377  fname.Prepend(pindir);
378  if (gDataSet->OpenDataSetFile(fname.Data(), f)) return kTRUE;
379  fname.Form("/run%d.laserpin", run);
380  fname.Prepend(pindir);
381  if (gDataSet->OpenDataSetFile(fname.Data(), f)) return kTRUE;
382  fname.Form("/run%d.genelaserpin", run);
383  fname.Prepend(pindir);
384  return gDataSet->OpenDataSetFile(fname.Data(), f);
385 }
386 
387 
388 
391 
393 {
394  // Read data for every run in dataset
395 
396  if (!fRunlist) {
397  Error("ReadData", "Must set list of runs first using SetRunList(TList*)");
398  return;
399  }
400  Info("ReadData", "Reading pulser and laser data for all runs");
401  TIter Nxt_r(fRunlist);
402  KVINDRADBRun* run = 0;
403  KVNumberList missing1, missing2;
404  while ((run = (KVINDRADBRun*)Nxt_r())) {
405  Int_t run_num = run->GetNumber();
406  //reset all array members to -1
407  for (int i = 0; i < fTab_siz; i++) fVal[i] = -1.0;
408  std::cout << "\rInfo in <KVINDRAPulserDataTree::ReadData>: Reading data for run " << run_num << std::flush;
409  UChar_t msg = ReadData(run_num);
410  if (msg & 1) missing1.Add(run_num);
411  if (msg & 2) missing2.Add(run_num);
412  }
413  std::cout << std::endl;
414  if (missing1.GetEntries())
415  Warning("ReadData", "Missing file 'run[run_num].gene' for runs: %s", missing1.GetList());
416  if (missing2.GetEntries())
417  Warning("ReadData", "Missing file 'run[run_num].[gene][laser]pin' for runs: %s", missing2.GetList());
418 }
419 
420 
421 
424 
426 {
427  // Read pulser data tree from file
428 
429  fArb = (TTree*)file->Get("PulserData");
430  if (fArb) {
431  //disable all branches except Run number
432  fArb->SetBranchStatus("*", 0);
433  fArb->SetBranchStatus("Run", 1);
434  }
435 }
436 
437 
438 
442 
444 {
445  // Write pulser data tree in file
446  // We build and index based on the Run number and store it in the tree.
447 
448  if (fArb) {
449  fArb->SetDirectory(file);
450  fArb->BuildIndex("Run");
451  fArb->Write();
452  }
453 }
454 
455 
456 
466 
468 {
469  // Return mean value of pulser/laser for given parameter and run.
470  // For detectors, param should be name of an acquisition parameter
471  // e.g. CI_0201_PG, CSI_1301_L, etc.
472  // For pin laser diodes, param should be name of associated acquisition parameter
473  // with either '_laser' or '_gene' appended
474  // e.g. PILA_05_PG_laser, SI_PIN1_PG_gene
475  //
476  // Returns -1.0 if no data available for this parameter/run.
477 
478  if (!fArb) return -1.0;
479 
480  //find corresponding branch
481  TBranch* br = fArb->GetBranch(param);
482  if (!br) {
483  //no branch found - wrong name given ?
484  Error("GetMean", "No branch found with name %s", param);
485  return -1.0;
486  }
487  //enable branch
488  fArb->SetBranchStatus(param, 1);
489  //connect variable to branch
490  Float_t value = -1.0;
491  br->SetAddress(&value);
492  //read entry corresponding to run
493  Int_t bytes = fArb->GetEntryWithIndex(run);
494  if (bytes < 0) {
495  //unknown run number
496  Error("GetMean", "Unknown run %d", run);
497  return -1.0;
498  }
499  //disable branch
500  fArb->SetBranchStatus(param, 0);
501 
502  return value;
503 }
504 
505 
int Int_t
#define f(i)
bool Bool_t
unsigned char UChar_t
char Char_t
float Float_t
constexpr Bool_t kTRUE
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
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 Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t bytes
char * Form(const char *fmt,...)
Base class for KaliVeda framework.
Definition: KVBase.h:142
virtual const Char_t * GetType() const
Definition: KVBase.h:177
virtual void SetNumber(UInt_t num)
Definition: KVBase.h:216
virtual Int_t GetNumber() const
Definition: KVDBRecord.h:73
const Char_t * GetDataSetDir() const
Definition: KVDataSet.cpp:729
const Char_t * GetDataSetEnv(const Char_t *type, const Char_t *defval="") const
Definition: KVDataSet.cpp:767
Bool_t OpenDataSetFile(const Char_t *filename, std::ifstream &file)
GANIL VXI/VME 16 bit (maximum) EBYEDAT acquisition parameter.
Database entry for each run of an INDRA experiment.
Definition: KVINDRADBRun.h:30
Handles TTree with mean pulser data for every run.
void ReadFile(std::ifstream &)
Read data in one file.
Bool_t OpenGeneData(Int_t, std::ifstream &)
virtual ~KVINDRAPulserDataTree()
Destructor.
Float_t GetMean(const Char_t *, Int_t)
TString GetDirectoryName(const Char_t *)
void ReadData()
Read data for every run in dataset.
void ReadTree(TFile *)
Read pulser data tree from file.
Bool_t OpenPinData(Int_t, std::ifstream &)
static KVMultiDetArray * MakeMultiDetector(const Char_t *dataset_name, Int_t run=-1, TString classname="KVMultiDetArray")
Strings used to represent a set of ranges of values.
Definition: KVNumberList.h:85
const Char_t * GetList() const
Int_t GetEntries() const
Definition: KVNumberList.h:171
void Add(Int_t)
Add value 'n' to the list.
KaliVeda extensions to ROOT collection classes.
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:73
Handles directories stored in .tgz archive files.
Definition: KVTarArchive.h:22
virtual void SetAddress(void *add)
const char * GetName() const override
const char * Data() const
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
TString & Prepend(char c, Ssiz_t rep=1)
void Form(const char *fmt,...)
TLine * line
void Error(const char *location, const char *fmt,...)
void Info(const char *location, const char *fmt,...)
void Warning(const char *location, const char *fmt,...)
ClassImp(TPyArg)
size_t fIndex