er  dev
ERNXyterCalibrator.cxx
1 /*
2  * ERNXyterCalibrator.cpp
3  *
4  * Created on: Apr 7, 2017
5  * Author: vratik
6  */
7 
8 #include "ERNXyterCalibrator.h"
9 
10 #include "TFile.h"
11 
12 ERNXyterCalibrator& ERNXyterCalibrator::Instance(void) {
13  static ERNXyterCalibrator instance;
14  return instance;
15 }
16 
17 ERNXyterCalibrator::ERNXyterCalibrator() {
18  fConstructed = kFALSE;
19 
20 }
21 
22 ERNXyterCalibrator::~ERNXyterCalibrator() {
23  this->Deconstruct();
24 }
25 
26 void ERNXyterCalibrator::Deconstruct(void) {
27  if (fConstructed) {
28  //for (Int_t i = 0; i < 64; i++) {
29  // delete fGraph[i]; // This causes double-free error. Why!?
30  //}
31  fConstructed = kFALSE;
32  }
33 }
34 
35 UInt_t ERNXyterCalibrator::ImportGraphs(TString graphFile) {
36  //TODO check - check that there is no memory already allocated.
37  //TODO If already allocated - delete! Next allocatation is invoked in GenerateLUTs().
38  if (fConstructed) {
39  this->Deconstruct();
40  }
41 
42  TFile fr(graphFile.Data());
43  if (fr.IsZombie()) {
44  TObject::Error("ERNXyterCalibrator::ImportGraphs", "Error opening file %s", graphFile.Data());
45  return 1;
46  }
47 
48  TList *gList = fr.GetListOfKeys();
49  const Int_t nEntries = gList->GetEntries();
50  if (nEntries != 64) {
51  Error("ERNXyterCalibrator::ImportGraphs", "Something wrong, only %d graphs found instead of 64.", nEntries);
52  return 1; // FAIL
53  }
54 
55  TString gName;
56  for (Int_t i=0; i<nEntries; i++) {
57  gName.Form("gcal%d", i);
58  fGraph[i] = (TGraph*)fr.Get(gName.Data());
59  if (fGraph[i] == 0) {
60  Error("ERNXyterCalibrator::ImportGraphs", "Graph %s was not found in file %s", gName.Data(), fr.GetName());
61  fr.Close();
62  return 1; // FAIL
63  }
64  }
65 
66  fr.Close();
67 
68 // printf("!!!!!!!!!!!! %s !!!!!!!!!!!!!!!", fGraph[4]->GetName() );
69 
70 // this->GenerateLUTs();
71 
72 // this->ExportLUTs("LUTs.root");
73 
74  return 0; // OK
75 }
76 
77 //unsigned int ERNXyterCalibrator::ExportLUTs(TString filename) {
78 //}
79 
80 Float_t ERNXyterCalibrator::GetCalibratedVal(UInt_t ch, Float_t val) {
81 // if (!fConstructed) this->GenerateDummyLUTs();
82 
83  if (ch>=64) return 0.; // We basically ignore channels 64-128
84 
85  // Bin number as input!
86 // return fLUT[ch]->GetBinContent((UInt_t)val+200);
87  if (fGraph[ch] == NULL) return (Float_t)val;
88 
89  return (Float_t)fGraph[ch]->Eval(val);
90 }
91 
92 //void ERNXyterCalibrator::GenerateDummyLUTs(void) {
93 //}
94 //
95 //void ERNXyterCalibrator::GenerateLUTs(void) {
96 //}