StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
l3EmcCalibration.cxx
1 #include "l3EmcCalibration.h"
2 
3 #include <fcntl.h>
4 #include <unistd.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <sys/mman.h>
8 #include <errno.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <fstream>
12 #include <string>
13 #include <math.h>
14 #include <rtsLog.h>
15 //#include "l3Log.h"
16 
17 using namespace std;
18 
19 
20 l3EmcTowerInfo::l3EmcTowerInfo() {
21  set(0., 0., 0., 0., 0., 1.0, -1, -1);
22 }
23 
24 
25 l3EmcCalibration::l3EmcCalibration(int nTow) {
26  nTowers = nTow;
27 
28  tower = new l3EmcTowerInfo[nTowers];
29  daq2id = new int[nTowers];
30 }
31 
32 l3EmcCalibration::~l3EmcCalibration() {
33  delete[] tower;
34  delete[] daq2id;
35 
36 }
37 
38 int l3EmcCalibration::loadMap(const char* filename)
39 {
40 
41  LOG(ERR, "Binary maps are no longer supported. Where did you find one?",0,0,0,0,0);
42 
43  return -1;
44 
45 }
46 
47 
48 //#define OLDLOAD
49 #ifdef OLDLOAD
50 int l3EmcCalibration::loadTextMap(const char* filename) {
51 
52  ifstream txtmap(filename);
53 
54  string s;
55 
56  enum colDesc { col_phi, col_eta,
57  col_ped, col_threshold, col_gain,
58  col_id, col_daqId, col_ignore };
59 
60 
61 
62  txtmap >> s;
63  if (s != "EmcCalib") {
64  LOG(ERR, "No EMC calibration map in text format in %s\n", filename,0,0,0,0);
65  return -1;
66  }
67 
68  txtmap >> s;
69  if (s != "columns:") {
70  LOG(ERR,"No EMC calibration map in text format in %s\n", filename,0,0,0,0);
71  return -1;
72  }
73 
74  int nCols;
75  txtmap >> nCols;
76  //l3Log("EMC calibration map with %d cols\n", nCols);
77 
78  colDesc *colTarget = new colDesc[nCols];
79 
80  // This should be determined by parsing the header
81  colTarget[0] = col_id;
82  colTarget[1] = col_daqId;
83  colTarget[2] = col_phi;
84  colTarget[3] = col_eta;
85  colTarget[4] = col_ped;
86  colTarget[5] = col_gain;
87  colTarget[6] = col_threshold;
88 
89 
90  for (int i=0; i<nTowers; i++) {
91 
92  float phi=0.;
93  float eta=0.;
94  float ped=0.;
95  float gain=1.0;
96  int id=-1;
97  int daqId=-1;
98  float threshold=0;
99 
100  float ignore;
101 
102  for (int col=0; col<nCols; col++) {
103 
104  switch (colTarget[col]) {
105 
106  case col_phi:
107  txtmap >> phi ;
108  break;
109 
110  case col_eta:
111  txtmap >> eta ;
112  break;
113 
114  case col_ped:
115  txtmap >> ped ;
116  break;
117 
118  case col_gain:
119  txtmap >> gain ;
120  break;
121 
122  case col_threshold:
123  txtmap >> threshold ;
124  break;
125 
126  case col_id:
127  txtmap >> id ;
128  id--;
129  break;
130 
131  case col_daqId:
132  txtmap >> daqId ;
133  break;
134 
135  case col_ignore:
136  txtmap >> ignore ;
137  break;
138 
139  }
140  }
141 
142 // cout << " " << phi << " " << eta << " "
143 // << ped << " " << threshold << " " << gain << " "
144 // << id << " " << daqId << endl;
145 
146  if ( (id < 0) || (id >= 4800) ) {
147  LOG(ERR, "%s contains info for tower %i!!!\n",filename,id,0,0,0);
148  return -1;
149  }
150 
151 
152  tower[id].set(phi, eta, ped, gain, id, daqId);
153  daq2id[daqId] = id;
154  }
155 
156  return 0;
157 }
158 #else
159 
160 int l3EmcCalibration::loadTextMap(const char* filename)
161 {
162  //l3Log("Reading %s", filename);
163 
164  // LOG(NOTE, "Loading calibration map %s",filename,0,0,0,0);
165 
166  //#ifdef JEFFDUMB
167 
168  string s;
169 
170  ifstream txtmap(filename);
171 
172  string type;
173  txtmap >> type;
174 
175  txtmap >> s;
176  if (s != "columns:") {
177  LOG(ERR, "No EMC calibration found in %s\n", filename,0,0,0,0);
178  return -1;
179  }
180 
181  colDef_t colDef;
182  colDef.set(0,-1,-1,-1,-1,-1,-1,-1,-1);
183 
184  //int nCols;
185  txtmap >> colDef.nCols;
186 
187  if (type == "EmcCalib") {
188 
189  colDef.set(7,0,1,2,3,-1,-1,4,5);
190 
191  } else {
192  txtmap >> s;
193  if (s != "format:") return -1;
194 
195  for (int i=0; i<colDef.nCols; i++) {
196  txtmap >> s;
197 
198  if (s=="id") colDef.id = i;
199  if (s=="daq") colDef.daqId = i;
200  if (s=="phi") colDef.phi = i;
201  if (s=="eta") colDef.eta = i;
202  if (s=="etamin") colDef.etamin = i;
203  if (s=="etamax") colDef.etamax = i;
204  if (s=="ped") colDef.ped = i;
205  if (s=="gain") colDef.gain = i;
206  }
207 
208  }
209 
210 
211  int nRead = readCalib(&txtmap, colDef);
212 
213 
214  if(nRead == nTowers)
215  return 0;
216  else
217  return -1;
218  //#endif
219  //return 0;
220 }
221 
222 #endif
223 
224 int l3EmcCalibration::readCalib(ifstream *from, colDef_t colDef)
225 {
226 
227  int nTwr = 0;
228  //while(1) {
229  for (int t=0; t<nTowers; t++) {
230  float phi=0.0, eta=0.0, etamin=-999.0, etamax=-999.0;
231  float ped=0.0, gain=1.0;
232  int id=-1, daqId=-1;
233 
234  string dummy;
235 
236  //l3Log("asd %d\n", colDef.nCols);
237 
238  for (int i=0; i<colDef.nCols; i++) {
239  if (i == colDef.id) *from >> id; else
240  if (i == colDef.daqId) *from >> daqId; else
241  if (i == colDef.eta) *from >> eta; else
242  if (i == colDef.etamin) *from >> etamin; else
243  if (i == colDef.etamax) *from >> etamax; else
244  if (i == colDef.phi) *from >> phi; else
245  if (i == colDef.ped) *from >> ped; else
246  if (i == colDef.gain) *from >> gain; else
247  *from >> dummy;
248  }
249 
250 
251  if(from->eof()) break;
252 
253  // BTOW maps do not have etamin/max, so let's calculate it
254  if (nTowers == 4800) {
255  if(etamin == -999.)
256  etamin = floor(eta*20.)/20.;
257 
258  if(etamax == -999.)
259  etamax = ceil(eta*20.)/20.;
260  }
261 
262  tower[id-1].set(phi, eta, etamin, etamax, ped, gain, id, daqId);
263  daq2id[daqId] = id-1;
264  nTwr++;
265  }
266 
267 
268  return nTwr;
269 }
270 
271 
272 
273 
274 
275 int l3EmcCalibration::saveTextMap(const char* filename)
276 {
277  ofstream txtmap(filename);
278 
279  txtmap << "EmcCalib" << endl
280  << "columns: 7" << endl;
281 
282  for (int i=0; i<nTowers; i++) {
283  txtmap << tower[i].getID() << " "
284  << tower[i].getDaqID() << " "
285  << tower[i].getPhi() << " "
286  << tower[i].getEta() << " "
287  << tower[i].getPed() << " "
288  << tower[i].getGain() << " "
289  << 0.0 << endl;
290  }
291 
292  return 0;
293 }