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/EMRCalibrationMap.hh"
19
#include "Utils/EMRChannelMap.hh"
23
EMRCalibrationMap::EMRCalibrationMap() {
26
EMRCalibrationMap::~EMRCalibrationMap() {
32
bool EMRCalibrationMap::InitializeFromCards(Json::Value configJSON) {
34
_number_of_planes = configJSON["EMRnumberOfPlanes"].asInt();
35
_number_of_bars = configJSON["EMRnumberOfBars"].asInt();
36
_fom = configJSON["EMRfom"].asString();
38
// Fill the vector containing all EMR channel keys.
39
this->MakeEMRChannelKeys();
41
// Find calibration file
42
char* pMAUS_ROOT_DIR = getenv("MAUS_ROOT_DIR");
43
if (!pMAUS_ROOT_DIR) {
44
Squeak::mout(Squeak::error)
45
<< "Could not find the $MAUS_ROOT_DIR environmental variable." << std::endl;
46
Squeak::mout(Squeak::error) << "Did you try running: source env.sh ?" << std::endl;
50
std::string calibFile = std::string(pMAUS_ROOT_DIR)
51
+ configJSON["EMR_calibration_file"].asString();
53
// Load calibration file
55
loaded = this->Initialize(calibFile);
63
bool EMRCalibrationMap::Initialize(std::string calibFile) {
65
bool status = Load(calibFile);
69
int EMRCalibrationMap::MakeEMRChannelKeys() {
70
for (int iPlane = 0; iPlane < _number_of_planes; iPlane++)
71
for (int iBar = -1; iBar < _number_of_bars; iBar++) // NB: average (-1), test channels (0)
72
_Ckey.push_back(EMRChannelKey(iPlane, iPlane%2, iBar, "emr"));
74
int nChannels = _Ckey.size();
75
_eps_MA.resize(nChannels);
76
_eps_SA.resize(nChannels);
81
bool EMRCalibrationMap::Load(std::string calibFile) {
83
// Check the calibration file
84
FILE *file = fopen(calibFile.c_str(), "r");
86
throw(Exception(Exception::recoverable,
87
"Could not find EMR calibration map",
88
"EMRCalibrationMap::Load"));
91
// Fill the arrays of correction factors
94
while (fgets(line, 100, file)) {
95
char pmt[10], fom[10];
96
int plane(-9), bar(-9);
98
sscanf(line, "%s %s %d %d %lf", pmt, fom, &plane, &bar, &epsilon);
99
int n = FindEMRChannelKey(EMRChannelKey(plane, plane%2, bar, "emr"));
101
if (strcmp(fom, _fom.c_str()) == 0 && n != NOCALIB) {
102
if (strcmp(pmt, "MA") == 0) _eps_MA[n] = epsilon;
103
if (strcmp(pmt, "SA") == 0) _eps_SA[n] = epsilon;
108
} catch (MAUS::Exception e) {
109
Squeak::mout(Squeak::error)
110
<< "Error in EMRCalibrationMap::Load : Error during loading. " << std::endl
111
<< e.GetMessage() << std::endl;
118
int EMRCalibrationMap::FindEMRChannelKey(EMRChannelKey key) const {
119
for (unsigned int i = 0; i < _Ckey.size(); i++ )
120
if (_Ckey.at(i) == key)
126
double EMRCalibrationMap::Eps(EMRChannelKey key, const char *pmt) const {
128
int n = FindEMRChannelKey(key);
130
double epsilon = 1.0;
133
if (strcmp(pmt, "MA") == 0) epsilon = _eps_MA[n];
134
else if (strcmp(pmt, "SA") == 0)
135
epsilon = _eps_SA[n];
137
Squeak::mout(Squeak::error) << "Wrong PMT ID" << std::endl;
139
if ( epsilon ) return epsilon;
142
// std::cout << "EMRCalibrationMap -> No " << pmt << " calibration for " << key << std::endl;
146
void EMRCalibrationMap::Print() {
147
std::cout << "====================== EMRCalibrationMap =========================" << std::endl;
148
std::cout << " Number of channels : " << _Ckey.size() << std::endl;
150
for (unsigned int i = 0; i < _Ckey.size(); i++) {
151
std::cout << _Ckey[i] << " MA :" << _eps_MA[i] << std::endl;
152
std::cout << _Ckey[i] << " SA :" << _eps_SA[i] << std::endl;
155
std::cout<< "===================================================================" << std::endl;