1
/* This file is part of MAUS: http://micewww.pp.rl.ac.uk:8080/projects/maus
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.
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.
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/>.
18
#include "Utils/TOFCalibrationMap.hh"
20
TOFCalibrationMap::~TOFCalibrationMap() {
28
bool TOFCalibrationMap::InitializeFromCards(Json::Value configJSON) {
29
// Fill the vector containing all TOF channel keys.
30
this->MakeTOFChannelKeys();
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);
37
Json::Value tw_file = JsonWrapper::GetProperty(configJSON,
38
"TOF_TW_calibration_file",
39
JsonWrapper::stringValue);
41
Json::Value trigger_file = JsonWrapper::GetProperty(configJSON,
42
"TOF_Trigger_calibration_file",
43
JsonWrapper::stringValue);
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();
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;
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();
68
// Load the calibration constants.
69
bool loaded = this->Initialize(xMapT0File, xMapTWFile, xMapTriggerFile);
76
bool TOFCalibrationMap::Initialize(std::string t0File,
78
std::string triggerFile) {
79
bool status = LoadT0File(t0File) &&
81
LoadTriggerFile(triggerFile);
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 !!!!
95
int nSlabs[] = {10, 7, 10};
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()));
106
int nChannels = _Pkey.size();
107
_t0.resize(nChannels);
108
_reff.resize(nChannels);
109
_twPar.resize(nChannels);
114
bool TOFCalibrationMap::LoadT0File(std::string t0File) {
115
std::ifstream stream(t0File.c_str());
117
Squeak::mout(Squeak::error)
118
<< "Error in TOFCalibrationMap::LoadT0File : Can't open TOF calibration file."
119
<< t0File << std::endl;
127
while (!stream.eof()) {
128
stream >> key >> p0 >> reff;
130
int n = FindTOFChannelKey(key);
133
// std::cout << key << " pos:" << n << " t0:" << p0 << " reff:" << reff << std::endl;
136
Squeak::mout(Squeak::error)
137
<< "Error in TOFCalibrationMap::LoadT0File : Error during loading. " << std::endl
138
<< e.GetMessage() << std::endl;
145
bool TOFCalibrationMap::LoadTWFile(std::string twFile) {
146
std::ifstream stream(twFile.c_str());
148
Squeak::mout(Squeak::error)
149
<< "Error in TOFCalibrationMap::LoadTWFile : Can't open TOF calibration file. "
150
<< twFile << std::endl;
154
double p0, p1, p2, p3;
157
while (!stream.eof()) {
158
stream >> key >> p0 >> p1 >> p2 >> p3;
160
int n = FindTOFChannelKey(key);
166
// std::cout<< key << " pos:" << n << " p0:" << p0 << " p1:" << p1 << std::endl;
169
Squeak::mout(Squeak::error)
170
<< "Error in TOFCalibrationMap::LoadTWFile : Error during loading. " << std::endl
171
<< e.GetMessage() << std::endl;
178
bool TOFCalibrationMap::LoadTriggerFile(std::string triggerFile) {
179
std::ifstream stream(triggerFile.c_str());
181
Squeak::mout(Squeak::error)
182
<< "Error in TOFCalibrationMap::LoadTriggerFile. Can't open TOF calibration file. "
183
<< triggerFile << std::endl;
189
while (!stream.eof()) {
190
stream >> Pkey >> dt;
192
_Tkey.push_back(Pkey);
194
// std::cout<< Pkey << " dt:" << dt << std::endl;
197
Squeak::mout(Squeak::error)
198
<< "Error in TOFCalibrationMap::LoadTriggerFile. Error during loading. " << std::endl
199
<< e.GetMessage() << std::endl;
202
// Use the last readed pixel key to set the number of the trigger station.
203
_triggerStation = Pkey.station();
208
int TOFCalibrationMap::FindTOFChannelKey(TOFChannelKey key) {
209
for (unsigned int i = 0; i < _Pkey.size(); ++i )
210
if (_Pkey.at(i) == key)
216
int TOFCalibrationMap::FindTOFPixelKey(TOFPixelKey key) {
217
for (unsigned int i = 0; i < _Tkey.size(); ++i )
218
if (_Tkey.at(i) == key)
224
double TOFCalibrationMap::T0(TOFChannelKey key, int &r) {
225
if (!_do_t0_correction)
228
int n = FindTOFChannelKey(key);
232
if ( _t0[n] ) return _t0[n];
235
// std::cout << "TOFCalibrationMap -> No T0 calibration for " << key << std::endl;
239
double TOFCalibrationMap::TriggerT0(TOFPixelKey key) {
240
if (!_do_triggerDelay_correction)
243
int n = FindTOFPixelKey(key);
247
// std::cout << "TOFCalibrationMap -> No Trigger calibration for " << key << std::endl;
251
double TOFCalibrationMap::TW(TOFChannelKey key, int adc) {
252
if (!_do_timeWalk_correction)
255
int n = FindTOFChannelKey(key);
256
// See equation 46 in MICE Note 251 "TOF Detectors Time Calibration".
258
double x = adc + _twPar[n][0];
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])
268
// std::cout << "TOFCalibrationMap -> No TW calibration for " << key << std::endl;
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".
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) {
284
double dt = t0 - tw - trt0;
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)
293
dt += TriggerT0(refTr);
294
// std::cout << refTr << " dt = " << TriggerT0(refTr) << std::endl;
296
TOFPixelKey refTr(_triggerStation, reffSlab, Pkey.slab(), Pkey.detector());
297
if (TriggerT0(refTr) == NOCALIB)
300
dt += TriggerT0(refTr);
301
// std::cout << refTr << " dt = " << TriggerT0(refTr) << std::endl;
305
// std::cout << "TOFCalibrationMap -> dT = " << dt << std::endl;
309
void TOFCalibrationMap::Print() {
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;
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;
323
for (unsigned int i = 0; i < _Tkey.size(); i++)
324
std::cout << _Tkey[i] << " " << _Trt0[i] << std::endl;
326
std::cout<< "===================================================================" << std::endl;
329
TOFPixelKey::TOFPixelKey(string keyStr) throw(Squeal) {
330
std::stringstream xConv;
335
throw(Squeal(Squeal::recoverable,
336
std::string("corrupted TOF Pixel Key"),
337
"TOFPixelKey::TOFPixelKey(std::string)"));
341
bool TOFPixelKey::operator==( TOFPixelKey const key ) {
342
if ( _station == key._station &&
343
_slabX == key._slabX &&
344
_slabY == key._slabY &&
345
_detector == key._detector) {
352
bool TOFPixelKey::operator!=( TOFPixelKey const key ) {
353
if ( _station == key._station &&
354
_slabX == key._slabX &&
355
_slabY == key._slabY &&
356
_detector == key._detector) {
363
ostream& operator<<( ostream& stream, TOFPixelKey key ) {
364
stream << "TOFPixelKey " << key._station;
365
stream << " " << key._slabX;
366
stream << " " << key._slabY;
367
stream << " " << key._detector;
371
istream& operator>>( istream& stream, TOFPixelKey &key ) throw(Squeal) {
373
stream >> xLabel >> key._station >> key._slabX >> key._slabY >> key._detector;
375
if (xLabel != "TOFPixelKey") {
376
throw(Squeal(Squeal::recoverable,
377
std::string("corrupted TOF Pixel Key"),
378
"istream& operator>>(istream& stream, TOFPixelKey)"));
384
string TOFPixelKey::str() {