~blotsd/maus/dev

« back to all changes in this revision

Viewing changes to src/common_cpp/Utils/TOFCalibrationMap.cc

  • Committer: Chris Rogers
  • Date: 2011-10-25 15:58:49 UTC
  • mfrom: (656.1.17 maus)
  • Revision ID: chris.rogers@stfc.ac.uk-20111025155849-lm8hf1hylivttr61
Revert to 0.0.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of MAUS: http://micewww.pp.rl.ac.uk:8080/projects/maus
 
2
 *
 
3
 * MAUS is free software: you can redistribute it and/or modify
 
4
 * it under the terms of the GNU General Public License as published by
 
5
 * the Free Software Foundation, either version 3 of the License, or
 
6
 * (at your option) any later version.
 
7
 *
 
8
 * MAUS is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with MAUS.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 */
 
17
 
 
18
#include "Utils/TOFCalibrationMap.hh"
 
19
 
 
20
TOFCalibrationMap::~TOFCalibrationMap() {
 
21
  _Pkey.clear();
 
22
  _Tkey.clear();
 
23
  _twPar.resize(0);
 
24
  _t0.resize(0);
 
25
  _reff.resize(0);
 
26
}
 
27
 
 
28
bool TOFCalibrationMap::InitializeFromCards(Json::Value configJSON) {
 
29
  // Fill the vector containing all TOF channel keys.
 
30
  this->MakeTOFChannelKeys();
 
31
 
 
32
  // Get the calibration text files from the Json document.
 
33
  Json::Value t0_file = JsonWrapper::GetProperty(configJSON,
 
34
                                                 "TOF_T0_calibration_file",
 
35
                                                 JsonWrapper::stringValue);
 
36
 
 
37
  Json::Value tw_file = JsonWrapper::GetProperty(configJSON,
 
38
                                                 "TOF_TW_calibration_file",
 
39
                                                 JsonWrapper::stringValue);
 
40
 
 
41
  Json::Value trigger_file = JsonWrapper::GetProperty(configJSON,
 
42
                                                      "TOF_Trigger_calibration_file",
 
43
                                                      JsonWrapper::stringValue);
 
44
 
 
45
  // Check what needs to be done.
 
46
  _do_timeWalk_correction = JsonWrapper::GetProperty(configJSON,
 
47
                                                     "Enable_timeWalk_correction",
 
48
                                                     JsonWrapper::booleanValue).asBool();
 
49
  _do_triggerDelay_correction = JsonWrapper::GetProperty(configJSON,
 
50
                                                         "Enable_triggerDelay_correction",
 
51
                                                         JsonWrapper::booleanValue).asBool();
 
52
  _do_t0_correction = JsonWrapper::GetProperty(configJSON,
 
53
                                               "Enable_t0_correction",
 
54
                                               JsonWrapper::booleanValue).asBool();
 
55
 
 
56
  char* pMAUS_ROOT_DIR = getenv("MAUS_ROOT_DIR");
 
57
  if (!pMAUS_ROOT_DIR) {
 
58
    Squeak::mout(Squeak::error)
 
59
    << "Could not find the $MAUS_ROOT_DIR environmental variable." << std::endl;
 
60
    Squeak::mout(Squeak::error) << "Did you try running: source env.sh ?" << std::endl;
 
61
    return false;
 
62
  }
 
63
 
 
64
  std::string xMapT0File = std::string(pMAUS_ROOT_DIR) + t0_file.asString();
 
65
  std::string xMapTWFile = std::string(pMAUS_ROOT_DIR) + tw_file.asString();
 
66
  std::string xMapTriggerFile = std::string(pMAUS_ROOT_DIR) + trigger_file.asString();
 
67
 
 
68
  // Load the calibration constants.
 
69
  bool loaded = this->Initialize(xMapT0File, xMapTWFile, xMapTriggerFile);
 
70
  if (!loaded)
 
71
    return false;
 
72
 
 
73
  return true;
 
74
}
 
75
 
 
76
bool TOFCalibrationMap::Initialize(std::string t0File,
 
77
                                   std::string twFile,
 
78
                                   std::string triggerFile) {
 
79
  bool status = LoadT0File(t0File) &&
 
80
                LoadTWFile(twFile) &&
 
81
                LoadTriggerFile(triggerFile);
 
82
 
 
83
  return status;
 
84
}
 
85
 
 
86
int TOFCalibrationMap::MakeTOFChannelKeys() {
 
87
 /** Makes one TOFChannelKey for each channel of the TOF detector.
 
88
  * The size of _t0, _reff and _twPar is set here.
 
89
  * ATTENTION : The detector configuration is HARDCODED !!!!
 
90
  * TO BE IMPROVED !!!!
 
91
  */
 
92
 
 
93
  int nStation = 3;
 
94
  int nPlanes = 2;
 
95
  int nSlabs[] = {10, 7, 10};
 
96
 
 
97
  for (int st = 0; st < nStation; st++) {
 
98
    stringstream detector;
 
99
    detector << "tof" << st;
 
100
    for (int pl = 0; pl < nPlanes; pl++)
 
101
      for (int sl = 0; sl < nSlabs[st]; sl++)
 
102
        for (int pmt = 0; pmt < 2; pmt++)
 
103
          _Pkey.push_back(TOFChannelKey(st, pl, sl, pmt, detector.str()));
 
104
  }
 
105
 
 
106
  int nChannels = _Pkey.size();
 
107
  _t0.resize(nChannels);
 
108
  _reff.resize(nChannels);
 
109
  _twPar.resize(nChannels);
 
110
 
 
111
  return nChannels;
 
112
}
 
113
 
 
114
bool TOFCalibrationMap::LoadT0File(std::string t0File) {
 
115
  std::ifstream stream(t0File.c_str());
 
116
  if (!stream) {
 
117
    Squeak::mout(Squeak::error)
 
118
    << "Error in TOFCalibrationMap::LoadT0File : Can't open TOF calibration file."
 
119
    << t0File << std::endl;
 
120
    return false;
 
121
  }
 
122
 
 
123
  int reff;
 
124
  double p0;
 
125
  TOFChannelKey key;
 
126
  try {
 
127
    while (!stream.eof()) {
 
128
      stream >> key >> p0 >> reff;
 
129
 
 
130
      int n = FindTOFChannelKey(key);
 
131
      _t0[n] = p0;
 
132
      _reff[n] = reff;
 
133
      // std::cout << key << " pos:" << n << "  t0:" << p0 << "  reff:" << reff << std::endl;
 
134
    }
 
135
  } catch(Squeal e) {
 
136
    Squeak::mout(Squeak::error)
 
137
    << "Error in TOFCalibrationMap::LoadT0File : Error during loading. " << std::endl
 
138
    << e.GetMessage() << std::endl;
 
139
    return false;
 
140
  }
 
141
 
 
142
  return true;
 
143
}
 
144
 
 
145
bool TOFCalibrationMap::LoadTWFile(std::string twFile) {
 
146
  std::ifstream stream(twFile.c_str());
 
147
  if (!stream) {
 
148
    Squeak::mout(Squeak::error)
 
149
    << "Error in TOFCalibrationMap::LoadTWFile : Can't open TOF calibration file. "
 
150
    << twFile << std::endl;
 
151
    return false;
 
152
  }
 
153
 
 
154
  double p0, p1, p2, p3;
 
155
  TOFChannelKey key;
 
156
  try {
 
157
    while (!stream.eof()) {
 
158
      stream >> key >> p0 >> p1 >> p2 >> p3;
 
159
 
 
160
      int n = FindTOFChannelKey(key);
 
161
      _twPar[n].resize(4);
 
162
      _twPar[n][0] = p0;
 
163
      _twPar[n][1] = p1;
 
164
      _twPar[n][2] = p2;
 
165
      _twPar[n][3] = p3;
 
166
      // std::cout<< key << " pos:" << n << "  p0:" << p0 << "  p1:" << p1 << std::endl;
 
167
    }
 
168
  } catch(Squeal e) {
 
169
    Squeak::mout(Squeak::error)
 
170
    << "Error in TOFCalibrationMap::LoadTWFile : Error during loading. " << std::endl
 
171
    << e.GetMessage() << std::endl;
 
172
    return false;
 
173
  }
 
174
 
 
175
  return true;
 
176
}
 
177
 
 
178
bool TOFCalibrationMap::LoadTriggerFile(std::string triggerFile) {
 
179
  std::ifstream stream(triggerFile.c_str());
 
180
  if (!stream) {
 
181
    Squeak::mout(Squeak::error)
 
182
    << "Error in TOFCalibrationMap::LoadTriggerFile. Can't open TOF calibration file. "
 
183
    << triggerFile << std::endl;
 
184
    return false;
 
185
  }
 
186
  TOFPixelKey Pkey;
 
187
  double dt;
 
188
  try {
 
189
    while (!stream.eof()) {
 
190
      stream >> Pkey >> dt;
 
191
 
 
192
      _Tkey.push_back(Pkey);
 
193
      _Trt0.push_back(dt);
 
194
      // std::cout<< Pkey << "  dt:" << dt << std::endl;
 
195
    }
 
196
  } catch(Squeal e) {
 
197
    Squeak::mout(Squeak::error)
 
198
    << "Error in TOFCalibrationMap::LoadTriggerFile. Error during loading. " << std::endl
 
199
    << e.GetMessage() << std::endl;
 
200
    return false;
 
201
  }
 
202
  // Use the last readed pixel key to set the number of the trigger station.
 
203
  _triggerStation = Pkey.station();
 
204
 
 
205
  return true;
 
206
}
 
207
 
 
208
int TOFCalibrationMap::FindTOFChannelKey(TOFChannelKey key) {
 
209
  for (unsigned int i = 0; i < _Pkey.size(); ++i )
 
210
    if (_Pkey.at(i) == key)
 
211
      return i;
 
212
 
 
213
  return NOCALIB;
 
214
}
 
215
 
 
216
int TOFCalibrationMap::FindTOFPixelKey(TOFPixelKey key) {
 
217
  for (unsigned int i = 0; i < _Tkey.size(); ++i )
 
218
    if  (_Tkey.at(i) == key)
 
219
      return i;
 
220
 
 
221
  return NOCALIB;
 
222
}
 
223
 
 
224
double TOFCalibrationMap::T0(TOFChannelKey key, int &r) {
 
225
  if (!_do_t0_correction)
 
226
    return 0.;
 
227
 
 
228
  int n = FindTOFChannelKey(key);
 
229
 
 
230
  if (n != NOCALIB) {
 
231
    r = _reff[n];
 
232
    if ( _t0[n] ) return _t0[n];
 
233
  }
 
234
 
 
235
  // std::cout << "TOFCalibrationMap -> No T0 calibration for " << key << std::endl;
 
236
  return NOCALIB;
 
237
}
 
238
 
 
239
double TOFCalibrationMap::TriggerT0(TOFPixelKey key) {
 
240
  if (!_do_triggerDelay_correction)
 
241
    return 0.;
 
242
 
 
243
  int n = FindTOFPixelKey(key);
 
244
  if (n != NOCALIB)
 
245
    return _Trt0[n];
 
246
 
 
247
  // std::cout << "TOFCalibrationMap -> No Trigger calibration for " << key << std::endl;
 
248
  return n;
 
249
}
 
250
 
 
251
double TOFCalibrationMap::TW(TOFChannelKey key, int adc) {
 
252
  if (!_do_timeWalk_correction)
 
253
    return 0.;
 
254
 
 
255
  int n = FindTOFChannelKey(key);
 
256
  // See equation 46 in MICE Note 251 "TOF Detectors Time Calibration".
 
257
  if (n != NOCALIB) {
 
258
    double x = adc + _twPar[n][0];
 
259
    double x2 = x*x;
 
260
    double p1 = _twPar[n][1];
 
261
    double p2 = _twPar[n][2]/x;
 
262
    double p3 =_twPar[n][3]/x2;
 
263
    double dt_tw = p1 + p2 + p3;
 
264
    if (_twPar[n][0] && _twPar[n][1] && _twPar[n][2] && _twPar[n][3])
 
265
      return dt_tw;
 
266
  }
 
267
 
 
268
  // std::cout << "TOFCalibrationMap -> No TW calibration for " << key << std::endl;
 
269
  return NOCALIB;
 
270
}
 
271
 
 
272
double TOFCalibrationMap::dT(TOFChannelKey Pkey, TOFPixelKey TrKey, int adc) {
 
273
  // See equations 37-40 and 45 in MICE Note 251 "TOF Detectors Time Calibration".
 
274
  int reffSlab;
 
275
  double tw = TW(Pkey, adc);
 
276
  double t0 = T0(Pkey, reffSlab);
 
277
  double trt0 = TriggerT0(TrKey);
 
278
  // std::cout << "TOFCalibrationMap -> "<< Pkey << " " << TrKey << "  tw = " << tw;
 
279
  // std::cout << "  t0 = " << t0 << "  trt0 = " << trt0 << std::endl;
 
280
  if (tw == NOCALIB || t0 == NOCALIB || trt0 == NOCALIB) {
 
281
    return NOCALIB;
 
282
  }
 
283
 
 
284
  double dt = t0 - tw - trt0;
 
285
 
 
286
  // If this measurement is in the trigger station we need one additional correction.
 
287
  if (Pkey.station() == _triggerStation) {
 
288
    if (Pkey.plane() == 0) {
 
289
      TOFPixelKey refTr(_triggerStation, Pkey.slab(), reffSlab, Pkey.detector());
 
290
      if (TriggerT0(refTr) == NOCALIB)
 
291
        return NOCALIB;
 
292
 
 
293
      dt += TriggerT0(refTr);
 
294
      // std::cout << refTr << "  dt = " << TriggerT0(refTr) << std::endl;
 
295
    } else {
 
296
      TOFPixelKey refTr(_triggerStation, reffSlab, Pkey.slab(), Pkey.detector());
 
297
      if (TriggerT0(refTr) == NOCALIB)
 
298
        return NOCALIB;
 
299
 
 
300
      dt += TriggerT0(refTr);
 
301
       // std::cout << refTr << "  dt = " << TriggerT0(refTr) << std::endl;
 
302
    }
 
303
  }
 
304
 
 
305
  // std::cout << "TOFCalibrationMap -> dT = " << dt << std::endl;
 
306
  return dt*1e-3;
 
307
}
 
308
 
 
309
void TOFCalibrationMap::Print() {
 
310
 
 
311
  std::cout << "====================== TofCalibrationMap =========================" << std::endl;
 
312
  std::cout << " Name : " << _name << std::endl;
 
313
  std::cout << " Trigger in TOF" << _triggerStation << std::endl;
 
314
  std::cout << " Number of channels : " << _Pkey.size() << std::endl;
 
315
  std::cout << " Number of calibrated pixels in the trigger station : ";
 
316
  std::cout << _Tkey.size() << std::endl;
 
317
 
 
318
  for (unsigned int i = 0; i < _Pkey.size(); i++) {
 
319
    std::cout << _Pkey[i] << " T0 :" << _t0[i] << ", " << _reff[i];
 
320
    std::cout << "  TW:  "<< _twPar[i][0] << ", " << _twPar[i][1];
 
321
    std::cout << ", " << _twPar[i][2] << ", " << _twPar[i][3] << std::endl;
 
322
  }
 
323
  for (unsigned int i = 0; i < _Tkey.size(); i++)
 
324
    std::cout << _Tkey[i] << "  " << _Trt0[i] << std::endl;
 
325
 
 
326
  std::cout<< "===================================================================" << std::endl;
 
327
}
 
328
 
 
329
TOFPixelKey::TOFPixelKey(string keyStr) throw(Squeal) {
 
330
  std::stringstream xConv;
 
331
  try {
 
332
    xConv << keyStr;
 
333
    xConv >> (*this);
 
334
  }catch(Squeal e) {
 
335
    throw(Squeal(Squeal::recoverable,
 
336
                 std::string("corrupted TOF Pixel Key"),
 
337
                 "TOFPixelKey::TOFPixelKey(std::string)"));
 
338
  }
 
339
}
 
340
 
 
341
bool TOFPixelKey::operator==( TOFPixelKey const key ) {
 
342
  if ( _station == key._station &&
 
343
       _slabX == key._slabX &&
 
344
       _slabY == key._slabY &&
 
345
       _detector == key._detector) {
 
346
    return true;
 
347
  } else {
 
348
    return false;
 
349
  }
 
350
}
 
351
 
 
352
bool TOFPixelKey::operator!=( TOFPixelKey const key ) {
 
353
  if ( _station == key._station &&
 
354
       _slabX == key._slabX &&
 
355
       _slabY == key._slabY &&
 
356
       _detector == key._detector) {
 
357
    return false;
 
358
  } else {
 
359
    return true;
 
360
  }
 
361
}
 
362
 
 
363
ostream& operator<<( ostream& stream, TOFPixelKey key ) {
 
364
  stream << "TOFPixelKey " << key._station;
 
365
  stream << " " << key._slabX;
 
366
  stream << " " << key._slabY;
 
367
  stream << " " << key._detector;
 
368
  return stream;
 
369
}
 
370
 
 
371
istream& operator>>( istream& stream, TOFPixelKey &key ) throw(Squeal) {
 
372
  string xLabel;
 
373
  stream >> xLabel >> key._station >> key._slabX >> key._slabY >> key._detector;
 
374
 
 
375
  if (xLabel != "TOFPixelKey") {
 
376
    throw(Squeal(Squeal::recoverable,
 
377
                 std::string("corrupted TOF Pixel Key"),
 
378
                 "istream& operator>>(istream& stream, TOFPixelKey)"));
 
379
  }
 
380
 
 
381
  return stream;
 
382
}
 
383
 
 
384
string TOFPixelKey::str() {
 
385
  stringstream xConv;
 
386
  xConv << (*this);
 
387
  return xConv.str();
 
388
}
 
389
 
 
390
 
 
391
 
 
392