~mice-lcr/maus/end-of-emr-run

« back to all changes in this revision

Viewing changes to src/map/MapCppTOFSpacePoints/MapCppTOFSpacePoints.cc

First working version of the TOF reconstruction

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 <vector>
 
19
#include <string>
 
20
 
 
21
#include "src/common_cpp/Utils/JsonWrapper.hh"
 
22
#include "src/common_cpp/Utils/CppErrorHandler.hh"
 
23
#include "Interface/Squeak.hh"
 
24
#include "Interface/Squeal.hh"
 
25
#include "Interface/dataCards.hh"
 
26
 
 
27
#include "src/map/MapCppTOFSpacePoints/MapCppTOFSpacePoints.hh"
 
28
 
 
29
bool MapCppTOFSpacePoints::birth(std::string argJsonConfigDocument) {
 
30
  // Check if the JSON document can be parsed, else return error only
 
31
  _classname = "MapCppTOFSpacePoints";
 
32
 
 
33
  if (SetConfiguration(argJsonConfigDocument))
 
34
    return true;  // Sucessful completion
 
35
  else
 
36
    return false;
 
37
}
 
38
 
 
39
bool MapCppTOFSpacePoints::death()  {return true;}
 
40
 
 
41
std::string MapCppTOFSpacePoints::process(std::string document) {
 
42
  //  JsonCpp setup
 
43
  Json::FastWriter writer;
 
44
  Json::Value root;
 
45
  Json::Value xEventType;
 
46
  // Check if the JSON document can be parsed, else return error only
 
47
  try {
 
48
    root = JsonWrapper::StringToJson(document);
 
49
    xEventType = JsonWrapper::GetProperty(root,
 
50
                                          "daq_event_type",
 
51
                                          JsonWrapper::stringValue);
 
52
  }catch(Squeal e) {
 
53
    Squeak::mout(Squeak::error)
 
54
    << "Error in  MapCppTOFSlabHits::process. Bad json document."
 
55
    << std::endl;
 
56
    Json::Value errors;
 
57
    std::stringstream ss;
 
58
    ss << _classname << " says:" << e.GetMessage();
 
59
    errors["bad_json_document"] = ss.str();
 
60
    root["errors"] = errors;
 
61
    return writer.write(root);
 
62
  }
 
63
 
 
64
  if (xEventType== "physics_event" || xEventType == "calibration_event") {
 
65
    Json::Value xSlabHits = JsonWrapper::GetProperty(root, "slab_hits", JsonWrapper::objectValue);
 
66
    StationKeys.push_back("tof1");
 
67
    StationKeys.push_back("tof0");
 
68
    StationKeys.push_back("tof2");
 
69
    for (unsigned int i=0; i<_trigger_pixels.size(); i++)
 
70
      delete _trigger_pixels[i];
 
71
 
 
72
   _trigger_pixels.resize(0);
 
73
    // Loop over each station.
 
74
    for (unsigned int n_station = 0; n_station < StationKeys.size(); n_station++) {
 
75
 
 
76
      Json::Value xDocDetectorSlabHits = JsonWrapper::GetProperty(xSlabHits,
 
77
                                                                StationKeys[n_station],
 
78
                                                                JsonWrapper::arrayValue);
 
79
 
 
80
      if (xDocDetectorSlabHits.isArray()) {
 
81
        int n_part_events = xDocDetectorSlabHits.size();
 
82
        _trigger_pixels.resize(n_part_events);
 
83
 
 
84
        // Loop over the particle events.
 
85
        for (int PartEvent = 0; PartEvent < n_part_events; PartEvent++) {
 
86
          Json::Value xDocPartEvent = JsonWrapper::GetItem(xDocDetectorSlabHits,
 
87
                                                           PartEvent,
 
88
                                                           JsonWrapper::anyValue);
 
89
 
 
90
          Json::Value xDocSpacePoints = makeSpacePoints(xDocPartEvent);
 
91
          root["space_points"][StationKeys[n_station]][PartEvent] = xDocSpacePoints;
 
92
        }
 
93
      }
 
94
    }
 
95
  }
 
96
  // std::cout << root["space_points"] << std::endl;
 
97
  return writer.write(root);
 
98
}
 
99
 
 
100
bool MapCppTOFSpacePoints::SetConfiguration(std::string json_configuration) {
 
101
  //  JsonCpp setup
 
102
  Json::Value configJSON = JsonWrapper::StringToJson(json_configuration);
 
103
  // this will contain the configuration
 
104
 
 
105
  _map.SetTriggerStation(1);
 
106
  bool loaded = _map.InitializeFromCards(json_configuration);
 
107
  if (!loaded)
 
108
    return false;
 
109
 
 
110
  return true;
 
111
}
 
112
 
 
113
Json::Value MapCppTOFSpacePoints::makeSpacePoints(Json::Value xDocPartEvent) {
 
114
 
 
115
  Json::Value xDocSpacePoints;
 
116
 
 
117
  if (xDocPartEvent.isArray()) {
 
118
    int n_slab_hits = xDocPartEvent.size();
 
119
     std::cout << xDocPartEvent << std::endl;
 
120
 
 
121
    // Create a map of all slab hits.
 
122
    std::vector<int> xPlane0Hits;
 
123
    std::vector<int> xPlane1Hits;
 
124
 
 
125
    // Loop ovew the slab hits.
 
126
    for (int SlHit = 0; SlHit < n_slab_hits; SlHit++) {
 
127
      // Get the slab hit.
 
128
      Json::Value xThisSlabHit =
 
129
      JsonWrapper::GetItem(xDocPartEvent, SlHit, JsonWrapper::objectValue);
 
130
 
 
131
      int xPlane  = JsonWrapper::GetProperty(xThisSlabHit,
 
132
                                             "plane",
 
133
                                             JsonWrapper::intValue).asInt();
 
134
 
 
135
      switch (xPlane) {
 
136
        case 0 :
 
137
          xPlane0Hits.push_back(SlHit);
 
138
          break;
 
139
        case 1 :
 
140
          xPlane1Hits.push_back(SlHit);
 
141
          break;
 
142
      }
 
143
 
 
144
      Json::Value xDocSpacePoint;
 
145
      if (xPlane0Hits.size() == 1 && xPlane1Hits.size() == 1) {
 
146
 
 
147
        Json::Value xDocSlabHit0 = JsonWrapper::GetItem(xDocPartEvent,
 
148
                                                       xPlane0Hits[0],
 
149
                                                       JsonWrapper::objectValue);
 
150
 
 
151
        Json::Value xDocSlabHit1 = JsonWrapper::GetItem(xDocPartEvent,
 
152
                                                       xPlane1Hits[0],
 
153
                                                       JsonWrapper::objectValue);
 
154
 
 
155
        xDocSpacePoint = fillSpacePoint(xDocSlabHit0, xDocSlabHit1);
 
156
      }
 
157
      unsigned int n = 0;
 
158
      xDocSpacePoints[n] = xDocSpacePoint;
 
159
      //xDocSpacePoints.append(xDocSpacePoint);
 
160
    }
 
161
  }
 
162
  
 
163
  return xDocSpacePoints;
 
164
}
 
165
 
 
166
Json::Value MapCppTOFSpacePoints::fillSpacePoint(Json::Value xDocSlabHit0,
 
167
                                                 Json::Value xDocSlabHit1) {
 
168
 
 
169
  Json::Value xDocSpacePoint;
 
170
 
 
171
  xDocSpacePoint["part_event_number"] = xDocSlabHit0["part_event_number"];
 
172
  xDocSpacePoint["phys_event_number"] = xDocSlabHit0["phys_event_number"];
 
173
 
 
174
  // Get the two channel keys and make the pixel key.
 
175
  std::string keyStr_Slab0_digit0 = JsonWrapper::GetProperty(xDocSlabHit0["pmt0"],
 
176
                                                             "tof_key",
 
177
                                                             JsonWrapper::stringValue).asString();
 
178
  TOFChannelKey xKey_Slab0_digit0(keyStr_Slab0_digit0);
 
179
 
 
180
 
 
181
  std::string keyStr_Slab1_digit0 = JsonWrapper::GetProperty(xDocSlabHit1["pmt0"],
 
182
                                                             "tof_key",
 
183
                                                             JsonWrapper::stringValue).asString();
 
184
  TOFChannelKey xKey_Slab1_digit0(keyStr_Slab1_digit0);
 
185
 
 
186
  TOFPixelKey *xSPKey = new TOFPixelKey(xKey_Slab0_digit0.station(),
 
187
                                        xKey_Slab0_digit0.slab(),
 
188
                                        xKey_Slab1_digit0.slab(),
 
189
                                        xKey_Slab1_digit0.detector());
 
190
 
 
191
  int nPartEvent = xDocSlabHit0["part_event_number"].asInt();
 
192
  if (xSPKey->station() == 1) _trigger_pixels[nPartEvent] = xSPKey;
 
193
  std::cout << nPartEvent << std::endl;
 
194
  // Fill the information in the Space point.
 
195
  xDocSpacePoint["slab0"]     = xKey_Slab0_digit0.slab();
 
196
  xDocSpacePoint["station"]   = xKey_Slab0_digit0.station();
 
197
  xDocSpacePoint["detector"]  = xKey_Slab0_digit0.detector();
 
198
  xDocSpacePoint["slab1"]     = xKey_Slab1_digit0.slab();
 
199
  xDocSpacePoint["pixel_key"] = xSPKey->str();
 
200
 
 
201
  if (_trigger_pixels[nPartEvent]) {
 
202
  double time_Slab0_digit0, time_Slab0_digit1, time_Slab1_digit0, time_Slab1_digit1;
 
203
  // Process Digit0 of Slab0
 
204
  calibratePmtHit(*(_trigger_pixels[nPartEvent]), xDocSlabHit0["pmt0"], time_Slab0_digit0);
 
205
  
 
206
 // Process Digit1 of Slab0
 
207
  calibratePmtHit(*(_trigger_pixels[nPartEvent]), xDocSlabHit0["pmt1"], time_Slab0_digit1);
 
208
  
 
209
// Process Digit0 of Slab1
 
210
  calibratePmtHit(*(_trigger_pixels[nPartEvent]), xDocSlabHit1["pmt0"], time_Slab1_digit0);
 
211
    
 
212
 // Process Digit1 of Slab1
 
213
  calibratePmtHit(*(_trigger_pixels[nPartEvent]), xDocSlabHit1["pmt1"], time_Slab1_digit1);
 
214
  
 
215
  // Now calculate the time.
 
216
  double time = 
 
217
  (time_Slab0_digit0 + time_Slab0_digit1 + time_Slab1_digit0 + time_Slab1_digit1)/4.;
 
218
 
 
219
  xDocSpacePoint["time"] = time;
 
220
   std::cout << *xSPKey << " t = " << time << std::endl;
 
221
  }
 
222
  // std::cout << xDocSpacePoint << std::endl;
 
223
  return xDocSpacePoint;
 
224
}
 
225
 
 
226
bool MapCppTOFSpacePoints::calibratePmtHit(TOFPixelKey xTriggerPixelKey,
 
227
                                           Json::Value &xPmtHit, 
 
228
                                           double &time) {
 
229
 
 
230
  std::string keyStr = JsonWrapper::GetProperty(xPmtHit,
 
231
                                                "tof_key",
 
232
                                                JsonWrapper::stringValue).asString();
 
233
  TOFChannelKey xChannelKey(keyStr);
 
234
  
 
235
  double raw_time  = JsonWrapper::GetProperty(xPmtHit,
 
236
                                              "raw_time",
 
237
                                              JsonWrapper::realValue).asDouble();
 
238
 
 
239
  int charge = JsonWrapper::GetProperty(xPmtHit,
 
240
                                        "charge",
 
241
                                        JsonWrapper::intValue).asInt();
 
242
 
 
243
  double dT = _map.dT(xChannelKey, xTriggerPixelKey, charge);
 
244
  time = raw_time - dT;
 
245
  xPmtHit["time"] = time;
 
246
  // std::cout << xChannelKey << " t = " << raw_time << " - " << dT << " = " << time << std::endl;
 
247
 
 
248
  return true;
 
249
}
 
250
 
 
251
 
 
252
 
 
253