~durga/maus/rel709

« back to all changes in this revision

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

  • Committer: Adam Dobbs
  • Date: 2014-12-11 13:26:05 UTC
  • mfrom: (659.1.96 release-candidate)
  • Revision ID: phuccj@gmail.com-20141211132605-6g6j2927elggi2q6
Tags: MAUS-v0.9.2
MAUS-v0.9.2

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/EMRCalibrationMap.hh"
 
19
#include "Utils/EMRChannelMap.hh"
 
20
 
 
21
namespace MAUS {
 
22
 
 
23
EMRCalibrationMap::EMRCalibrationMap() {
 
24
}
 
25
 
 
26
EMRCalibrationMap::~EMRCalibrationMap() {
 
27
  _Ckey.clear();
 
28
  _eps_MA.resize(0);
 
29
  _eps_SA.resize(0);
 
30
}
 
31
 
 
32
bool EMRCalibrationMap::InitializeFromCards(Json::Value configJSON) {
 
33
  // Fetch variables
 
34
  _number_of_planes = configJSON["EMRnumberOfPlanes"].asInt();
 
35
  _number_of_bars = configJSON["EMRnumberOfBars"].asInt();
 
36
  _fom = configJSON["EMRfom"].asString();
 
37
 
 
38
  // Fill the vector containing all EMR channel keys.
 
39
  this->MakeEMRChannelKeys();
 
40
 
 
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;
 
47
    return false;
 
48
  }
 
49
 
 
50
  std::string calibFile = std::string(pMAUS_ROOT_DIR)
 
51
                        + configJSON["EMR_calibration_file"].asString();
 
52
 
 
53
  // Load calibration file
 
54
  bool loaded;
 
55
  loaded = this->Initialize(calibFile);
 
56
 
 
57
  if (!loaded)
 
58
    return false;
 
59
 
 
60
  return true;
 
61
}
 
62
 
 
63
bool EMRCalibrationMap::Initialize(std::string calibFile) {
 
64
 
 
65
  bool status = Load(calibFile);
 
66
  return status;
 
67
}
 
68
 
 
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"));
 
73
 
 
74
  int nChannels = _Ckey.size();
 
75
  _eps_MA.resize(nChannels);
 
76
  _eps_SA.resize(nChannels);
 
77
 
 
78
  return nChannels;
 
79
}
 
80
 
 
81
bool EMRCalibrationMap::Load(std::string calibFile) {
 
82
 
 
83
  // Check the calibration file
 
84
  FILE *file = fopen(calibFile.c_str(), "r");
 
85
  if ( file == NULL ) {
 
86
    throw(Exception(Exception::recoverable,
 
87
          "Could not find EMR calibration map",
 
88
          "EMRCalibrationMap::Load"));
 
89
  }
 
90
 
 
91
  // Fill the arrays of correction factors
 
92
  char line[100];
 
93
  try {
 
94
    while (fgets(line, 100, file)) {
 
95
      char pmt[10], fom[10];
 
96
      int plane(-9), bar(-9);
 
97
      double epsilon(-1.0);
 
98
      sscanf(line, "%s %s %d %d %lf", pmt, fom, &plane, &bar, &epsilon);
 
99
      int n = FindEMRChannelKey(EMRChannelKey(plane, plane%2, bar, "emr"));
 
100
 
 
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;
 
104
      }
 
105
    }
 
106
 
 
107
    fclose(file);
 
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;
 
112
    return false;
 
113
  }
 
114
 
 
115
  return true;
 
116
}
 
117
 
 
118
int EMRCalibrationMap::FindEMRChannelKey(EMRChannelKey key) const {
 
119
  for (unsigned int i = 0; i < _Ckey.size(); i++ )
 
120
    if (_Ckey.at(i) == key)
 
121
      return i;
 
122
 
 
123
  return NOCALIB;
 
124
}
 
125
 
 
126
double EMRCalibrationMap::Eps(EMRChannelKey key, const char *pmt) const {
 
127
 
 
128
  int n = FindEMRChannelKey(key);
 
129
 
 
130
  double epsilon = 1.0;
 
131
 
 
132
  if (n != NOCALIB) {
 
133
    if (strcmp(pmt, "MA") == 0) epsilon = _eps_MA[n];
 
134
    else if (strcmp(pmt, "SA") == 0)
 
135
      epsilon = _eps_SA[n];
 
136
    else
 
137
      Squeak::mout(Squeak::error) << "Wrong PMT ID" << std::endl;
 
138
 
 
139
    if ( epsilon ) return epsilon;
 
140
  }
 
141
 
 
142
  // std::cout << "EMRCalibrationMap -> No " << pmt << " calibration for " << key << std::endl;
 
143
  return NOCALIB;
 
144
}
 
145
 
 
146
void EMRCalibrationMap::Print() {
 
147
  std::cout << "====================== EMRCalibrationMap =========================" << std::endl;
 
148
  std::cout << " Number of channels : " << _Ckey.size() << std::endl;
 
149
 
 
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;
 
153
  }
 
154
 
 
155
  std::cout<< "===================================================================" << std::endl;
 
156
}
 
157
}