er  dev
ERNXyterTreeSource.cxx
1 /*
2  * ERNXyterTreeSource.cxx
3  *
4  * Created on: Apr 3, 2017
5  * Author: vratik
6  */
7 
8 #include "ERNXyterTreeSource.h"
9 
10 #include "FairRootManager.h"
11 #include "cls_RootHit.h"
12 
13 #include "ERNXyterCalibrator.h"
14 
15 #include <iostream>
16 using namespace std;
17 
18 ERNXyterTreeSource::ERNXyterTreeSource() :
19  fInEvent(NULL),
20  fOutEvent(NULL),
21  fFile(NULL),
22  fTree(NULL),
23  fPath(""),
24  fTreeName(""),
25  fBranchName(""),
26  fCalParFileName(""),
27  fNonLinGraphsFileName(""),
28  fEvent(0)
29 {
30  for (Int_t i = 0; i < 64; i++) {
31  fCalPar[i] = 1;
32  }
33 
34  fPedestalsCorrection[0] = 25;
35  fPedestalsCorrection[1] = 19;
36  fPedestalsCorrection[2] = 23;
37  fPedestalsCorrection[3] = 21;
38  fPedestalsCorrection[4] = 21;
39  fPedestalsCorrection[5] = 19;
40  fPedestalsCorrection[6] = 27;
41  fPedestalsCorrection[7] = 25;
42  fPedestalsCorrection[8] = 23;
43  fPedestalsCorrection[9] = -3;
44  fPedestalsCorrection[10] = 27;
45  fPedestalsCorrection[11] = 23;
46  fPedestalsCorrection[12] = 1;
47  fPedestalsCorrection[13] = 1;
48  fPedestalsCorrection[14] = 3;
49  fPedestalsCorrection[15] = 23;
50  fPedestalsCorrection[16] = 27;
51  fPedestalsCorrection[17] = 19;
52  fPedestalsCorrection[18] = 25;
53  fPedestalsCorrection[19] = 17;
54  fPedestalsCorrection[20] = 19;
55  fPedestalsCorrection[21] = 29;
56  fPedestalsCorrection[22] = 15;
57  fPedestalsCorrection[23] = 21;
58  fPedestalsCorrection[24] = 19;
59  fPedestalsCorrection[25] = 25;
60  fPedestalsCorrection[26] = 1;
61  fPedestalsCorrection[27] = 21;
62  fPedestalsCorrection[28] = 21;
63  fPedestalsCorrection[29] = 19;
64  fPedestalsCorrection[30] = 17;
65  fPedestalsCorrection[31] = 3;
66  fPedestalsCorrection[32] = 17;
67  fPedestalsCorrection[33] = 21;
68  fPedestalsCorrection[34] = 21;
69  fPedestalsCorrection[35] = 25;
70  fPedestalsCorrection[36] = 21;
71  fPedestalsCorrection[37] = 23;
72  fPedestalsCorrection[38] = 15;
73  fPedestalsCorrection[39] = 23;
74  fPedestalsCorrection[40] = 15;
75  fPedestalsCorrection[41] = 19;
76  fPedestalsCorrection[42] = -1;
77  fPedestalsCorrection[43] = 15;
78  fPedestalsCorrection[44] = 15;
79  fPedestalsCorrection[45] = -3;
80  fPedestalsCorrection[46] = 19;
81  fPedestalsCorrection[47] = 11;
82  fPedestalsCorrection[48] = 23;
83  fPedestalsCorrection[49] = 19;
84  fPedestalsCorrection[50] = 19;
85  fPedestalsCorrection[51] = 15;
86  fPedestalsCorrection[52] = 19;
87  fPedestalsCorrection[53] = 3;
88  fPedestalsCorrection[54] = 21;
89  fPedestalsCorrection[55] = 25;
90  fPedestalsCorrection[56] = 27;
91  fPedestalsCorrection[57] = 17;
92  fPedestalsCorrection[58] = 19;
93  fPedestalsCorrection[59] = 17;
94  fPedestalsCorrection[60] = -3;
95  fPedestalsCorrection[61] = 17;
96  fPedestalsCorrection[62] = 13;
97  fPedestalsCorrection[63] = 21;
98 
99 }
100 
101 ERNXyterTreeSource::~ERNXyterTreeSource() {
102  // TODO Auto-generated destructor stub
103 }
104 
105 Bool_t ERNXyterTreeSource::Init() {
106 
107  //input files opening
108  if (fPath == "")
109  Fatal("ERNXyterTreeSource::Init", "No files for source ERNXyterTreeSource");
110  fFile = new TFile(fPath);
111  if (!fFile->IsOpen()) {
112  Fatal("ERNXyterTreeSource::Init", "Can`t open file for source ERNXyterTreeSource");
113  return kFALSE;
114  }
115 
116  fTree = (TTree*)fFile->Get(fTreeName);
117  if (!fTree)
118  Fatal("ERNXyterTreeSource::Init", "Can`t find tree in input file for source ERNXyterTreeSource");
119 
120  fInEvent = new cls_RootEvent();
121  fTree->SetBranchAddress(fBranchName.Data(),&fInEvent);
122 // cout << fTree->GetEntries() << " events" << endl;
123 
124  fOutEvent = new WCalEvent();
125 
126  FairRootManager* ioman = FairRootManager::Instance();
127 // cout << "branch name: " << fBranchName.Data() << endl;
128  //todo: name of branch is not saved in root file!!!!
129  ioman->Register(fBranchName.Data(), "RawEvents", fInEvent, kTRUE);
130  ioman->Register("outBranch.", "RawEvents", fOutEvent, kTRUE);
131 
132  ImportCalParameters();
133  ImportNonLinGraphs();
134 
135  return kTRUE;
136 }
137 
138 Int_t ERNXyterTreeSource::ReadEvent(UInt_t) {
139 
140 // cout << "begin of function ReadEvent" << endl;
141 
142  if ( !(fEvent%100000) ) {
143  std::cout << "####### EVENT " << fEvent << " #####" << std::endl;
144  }
145 
146  if (fTree->GetEntriesFast() == fEvent+1) {
147  std::cout << "####### EVENT " << fEvent << " #####" << std::endl;
148  return 1;
149  }
150 
151  fTree->GetEntry(fEvent);
152 
153 
154  TClonesArray *rawHits = fInEvent->GetHits();
155  cls_RootHit *rawHit;
156  UChar_t channel;
157  Int_t adcVal;
158  Float_t adcNonLin, adcNonLinCorr, adcCalibrated;
159 
160  //fOutEvent->SetNumOfHits(fInEvent->GetNumOfHits());
161 
162 // cout << "number of hits: " << rawHits->GetLast() << "\t" << fInEvent->GetNumOfHits() << endl;
163  for (Int_t i = 0; i <= rawHits->GetLast(); i++) {
164  rawHit = (cls_RootHit*)rawHits->ConstructedAt(i);
165 // cout << (Int_t)rawHit->GetChannel() << "\t" << rawHit->GetAdcVal()
166 // << "\t" << ERNXyterCalibrator::Instance().GetCalibratedVal(channel, adcVal) << endl;
167  channel = rawHit->GetChannel();
168  adcVal = rawHit->GetAdcVal();
169  adcNonLin = ERNXyterCalibrator::Instance().GetCalibratedVal(channel, adcVal);
170  adcNonLinCorr = adcNonLin - fPedestalsCorrection[channel];
171 // adcCalibrated = (adcNonLinCorr+calRandom.Uniform(-0.5, 0.5))/fEffCalib[ch];
172  adcCalibrated = (adcNonLinCorr+calRandom.Uniform(-0.5, 0.5))/fCalPar[channel];
173 
174 // cout << (Int_t)channel << "\t" << adcVal << "\t" << adcNonLin
175 // << "\t" << adcNonLinCorr << "\t" << adcCalibrated << endl;
176 
177 // cout << adcVal << "\t" << ERNXyterCalibrator::Instance().GetCalibratedVal(channel, adcVal) << endl;
178 
179 // fOutEvent->AddHit(channel, adcVal);
180  fOutEvent->AddHit(channel, adcVal, adcNonLin, adcNonLinCorr, adcCalibrated);
181  }
182 // cout << "======== end of hit =========" << endl;
183 
184  fEvent++;
185 
186 // cout << "end of function ReadEvent" << endl;
187 
188  return 0;
189 }
190 
191 void ERNXyterTreeSource::Close() {
192  if (fFile){
193  fFile->Close();
194  delete fFile;
195  }
196 }
197 
198 void ERNXyterTreeSource::Reset() {
199 // cout << "begin of function Reset" << endl;
200 
201  fOutEvent->Clear();
202 
203 // cout << "end of function Reset" << endl;
204 }
205 
206 void ERNXyterTreeSource::SetInFile(TString path, TString treeName, TString branchName){
207  fPath = path;
208  fTreeName = treeName;
209  fBranchName = branchName;
210  cout << "Input file " << path << " with tree name " << fTreeName <<" and branch name " <<
211  fBranchName << " added to source ERNXyterTreeSource" << endl;
212 }
213 
214 void ERNXyterTreeSource::SetCalParameters(const char* p_filename)
215 {
216  fCalParFileName = p_filename;
217 }
218 
219 void ERNXyterTreeSource::SetNonLinGraphs(const char* graph_filename) {
220  fNonLinGraphsFileName = graph_filename;
221 }
222 
223 void ERNXyterTreeSource::ImportCalParameters() {
224 
225  if (fCalParFileName.Length()==0) {
226  Warning("ERNXyterTreeSource::ImportCalParameters",
227  "File with 1e calibration parameters was not set.");
228  return;
229  }
230 
231  Info("ERNXyterTreeSource::ImportCalParameters",
232  "Importing calibration parameters from file %s corrected for %f.",
233  fCalParFileName.Data(), f1ePosCorrection);
234 
235  std::ifstream infile(fCalParFileName.Data());
236  if (!infile.is_open()) {
237  Error("ERNXyterTreeSource::ImportCalParameters", "File %s was not open.", fCalParFileName.Data());
238  return;
239  }
240 
241  Int_t ch;
242  Float_t val;
243 
244  for (unsigned int i=0; i<64; i++) {
245  infile >> ch >> val;
246  fCalPar[ch] = val/f1ePosCorrection;
247  }
248 
249  infile.close();
250 }
251 
252 void ERNXyterTreeSource::ImportNonLinGraphs() {
253 // TString graphFile = graph_filename;
254  if (fNonLinGraphsFileName.Length()==0) {
255  Warning("ERNXyterTreeSource::ImportNonLinGraphs", "File with graphs for non-linearity correction was not set.");
256  return;
257  }
258 
259  Info("ERNXyterTreeSource::ImportNonLinGraphs", "Importing graphs for non-linearity correction from file %s.", fNonLinGraphsFileName.Data());
260 
261  ERNXyterCalibrator::Instance().ImportGraphs(fNonLinGraphsFileName);
262 }
263 
264 void ERNXyterTreeSource::PrintCalParameters() {
265  cout << "channel\tparameter" << endl;
266  for (Int_t i = 0; i<64; i++) {
267  cout << i << "\t" << fCalPar[i] << endl;
268  }
269 }