~tmohayai/maus/first_branch

« back to all changes in this revision

Viewing changes to src/map/MapCppTOFDigits/MapCppTOFDigits.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 "Utils/CppErrorHandler.hh"
 
19
#include "Utils/JsonWrapper.hh"
 
20
#include "Utils/TOFChannelMap.hh"
 
21
#include "Utils/DAQChannelMap.hh"
 
22
#include "Interface/Squeal.hh"
 
23
#include "Interface/dataCards.hh"
 
24
 
 
25
#include "src/map/MapCppTOFDigits/MapCppTOFDigits.hh"
 
26
 
 
27
bool MapCppTOFDigits::birth(std::string argJsonConfigDocument) {
 
28
  // Check if the JSON document can be parsed, else return error only
 
29
  _classname = "MapCppTOFDigits";
 
30
  StationKeys.push_back("tof0");
 
31
  StationKeys.push_back("tof1");
 
32
  StationKeys.push_back("tof2");
 
33
 
 
34
  if( SetConfiguration(argJsonConfigDocument) )
 
35
    return true;  // Sucessful completion
 
36
 
 
37
  return false;
 
38
}
 
39
 
 
40
 
 
41
bool MapCppTOFDigits::death()  { return true;}
 
42
 
 
43
std::string MapCppTOFDigits::process( std::string document ) {
 
44
 
 
45
  //  JsonCpp setup
 
46
  Json::FastWriter writer;
 
47
 
 
48
  // Check if the JSON document can be parsed, else return error only
 
49
  Json::Value root = JsonWrapper::StringToJson( document );
 
50
  Json::Value xEventType = JsonWrapper::GetProperty( root, "daq_event_type", JsonWrapper::stringValue );
 
51
 
 
52
  if(xEventType=="physics_event" || xEventType=="calibration_event" ) {
 
53
    Json::Value xDaqData = JsonWrapper::GetProperty( root, "daq_data", JsonWrapper::objectValue );  
 
54
 
 
55
    Json::Value xDocTrigReq      = JsonWrapper::GetProperty( xDaqData,
 
56
                                                             "trigger_request",
 
57
                                                             JsonWrapper::arrayValue);
 
58
    
 
59
    Json::Value xDocTrig         = JsonWrapper::GetProperty( xDaqData,
 
60
                                                             "trigger",   // CHECK THIS
 
61
                                                             JsonWrapper::arrayValue);
 
62
 
 
63
    for(unsigned int n_station = 0; n_station < StationKeys.size(); n_station++ ) {
 
64
      Json::Value xDocDetectorData = JsonWrapper::GetProperty( xDaqData,
 
65
                                                               StationKeys[n_station],
 
66
                                                               JsonWrapper::arrayValue);
 
67
 
 
68
      Json::Value xDocDigits = makeDigits( xDocDetectorData, xDocTrigReq, xDocTrig );
 
69
      root["digits"][StationKeys[n_station]] = xDocDigits;
 
70
    }
 
71
  }
 
72
  
 
73
  //std::cout<<root["digits"]<<std::endl;
 
74
  return writer.write(root);
 
75
}
 
76
 
 
77
bool MapCppTOFDigits::SetConfiguration(std::string json_configuration) {
 
78
  //  JsonCpp setup
 
79
  Json::Value configJSON = JsonWrapper::StringToJson(json_configuration);   //  this will contain the configuration
 
80
 
 
81
  Json::Value map_file_name = JsonWrapper::GetProperty( configJSON, "TOF_cabling_file", JsonWrapper::stringValue);
 
82
  char* pMAUS_ROOT_DIR = getenv("MAUS_ROOT_DIR");
 
83
 
 
84
  if(!pMAUS_ROOT_DIR) {
 
85
    Squeak::mout(Squeak::error) << "Could not find the $MAUS_ROOT_DIR environmental variable." << std::endl;
 
86
    Squeak::mout(Squeak::error) << "Did you try running: source env.sh ?" << std::endl;
 
87
    return false;
 
88
  }
 
89
  std::string xMapFile = std::string(pMAUS_ROOT_DIR) + map_file_name.asString();
 
90
  bool loaded = _map.InitFromFile(xMapFile);
 
91
  if (!loaded)
 
92
    return false;
 
93
 
 
94
  return true;
 
95
}
 
96
 
 
97
Json::Value MapCppTOFDigits::makeDigits( Json::Value xDocDetData, 
 
98
                                         Json::Value xDocTrigReq, 
 
99
                                         Json::Value xDocTrig) {
 
100
  Json::Value xDocDigits;
 
101
 
 
102
  if( xDocDetData.isArray() ) {
 
103
    int n_part_events = xDocDetData.size();
 
104
    for ( int PartEvent = 0; PartEvent < n_part_events; PartEvent++ ) {
 
105
      // Get the data, trigger and trigger request for this particle event.
 
106
      Json::Value xDocPartEvent_data = JsonWrapper::GetItem( xDocDetData, PartEvent, JsonWrapper::anyValue );
 
107
      Json::Value xDocPartEvent_trigger = JsonWrapper::GetItem( xDocTrig, PartEvent, JsonWrapper::anyValue );
 
108
      Json::Value xDocPartEvent_triggerReq = JsonWrapper::GetItem( xDocTrigReq, PartEvent, JsonWrapper::anyValue );
 
109
 
 
110
      if (xDocPartEvent_data.isMember("V1290") && xDocPartEvent_data.isMember("V1724")) {
 
111
        Json::Value xDocTdc = JsonWrapper::GetProperty(xDocPartEvent_data, 
 
112
                                                       "V1290", 
 
113
                                                       JsonWrapper::arrayValue );
 
114
 
 
115
        Json::Value xDocfAdc = JsonWrapper::GetProperty(xDocPartEvent_data,
 
116
                                                        "V1724",
 
117
                                                        JsonWrapper::arrayValue );
 
118
 
 
119
        int n_tdc_hits = xDocTdc.size();
 
120
        Json::Value xDocPmtHits;
 
121
        for ( int TdcHitCount = 0; TdcHitCount < n_tdc_hits; TdcHitCount++ ) {
 
122
          // Get the Tdc info from the particle event. This must be done before getting
 
123
          // info from any other equipment.
 
124
          Json::Value xDocTheDigit = getTdc(xDocTdc[TdcHitCount]);
 
125
 
 
126
          // Get flash ADC info and appent it to the digit. The info from the Tdc is 
 
127
          // passed as well in order to find the ascosiated adc values.
 
128
          getAdc(xDocfAdc, xDocTdc[TdcHitCount], xDocTheDigit);
 
129
 
 
130
          // Get the trigger leading and trailing times and add them to the digit.
 
131
          getTrig(xDocPartEvent_trigger, xDocTdc[TdcHitCount], xDocTheDigit);
 
132
 
 
133
          // Get the trigger request leading and trailing times and add them to the digit.
 
134
          getTrigReq(xDocPartEvent_triggerReq, xDocTdc[TdcHitCount], xDocTheDigit);
 
135
 
 
136
          xDocPmtHits.append(xDocTheDigit);
 
137
        }
 
138
        xDocDigits[PartEvent] = xDocPmtHits;
 
139
      }
 
140
    }
 
141
  }
 
142
  return xDocDigits;
 
143
}
 
144
 
 
145
Json::Value MapCppTOFDigits::getTdc(Json::Value xDocTdcHit){
 
146
  std::stringstream xConv;
 
147
  Json::Value xDocInfo;
 
148
 
 
149
  DAQChannelKey xTdcDaqKey;
 
150
  
 
151
  std::string xDaqKey_tdc_str = JsonWrapper::GetProperty( xDocTdcHit,
 
152
                                                          "channel_key",
 
153
                                                          JsonWrapper::stringValue).asString();
 
154
 
 
155
  xConv << xDaqKey_tdc_str;
 
156
  xConv >> xTdcDaqKey;
 
157
 
 
158
  TOFChannelKey* xTofTdcKey = _map.find(&xTdcDaqKey);
 
159
  
 
160
  if (xTofTdcKey) {
 
161
    xDocInfo["tof_key"]           = xTofTdcKey->str();
 
162
    xDocInfo["station"]           = xTofTdcKey->station();
 
163
    xDocInfo["plane"]             = xTofTdcKey->plane();
 
164
    xDocInfo["slab"]              = xTofTdcKey->slab();
 
165
    xDocInfo["pmt"]               = xTofTdcKey->pmt();
 
166
 
 
167
    xDocInfo["part_event_number"] = JsonWrapper::GetProperty( xDocTdcHit,
 
168
                                                              "part_event_number",
 
169
                                                              JsonWrapper::intValue );
 
170
 
 
171
    xDocInfo["phys_event_number"] = JsonWrapper::GetProperty( xDocTdcHit,
 
172
                                                              "phys_event_number",
 
173
                                                              JsonWrapper::intValue );
 
174
 
 
175
    xDocInfo["leading_time"]      = JsonWrapper::GetProperty( xDocTdcHit,
 
176
                                                              "leading_time",
 
177
                                                              JsonWrapper::intValue );
 
178
    
 
179
    xDocInfo["trailing_time"]     = JsonWrapper::GetProperty( xDocTdcHit,
 
180
                                                              "trailing_time",
 
181
                                                              JsonWrapper::intValue );
 
182
    
 
183
    xDocInfo["trigger_time_tag"]  = JsonWrapper::GetProperty( xDocTdcHit,
 
184
                                                              "trigger_time_tag",
 
185
                                                              JsonWrapper::intValue );
 
186
 
 
187
    xDocInfo["time_stamp"]        = JsonWrapper::GetProperty( xDocTdcHit,
 
188
                                                              "time_stamp",
 
189
                                                              JsonWrapper::intValue );
 
190
  }
 
191
  //std::cout << xDocInfo << std::endl;    
 
192
  return xDocInfo;
 
193
}
 
194
 
 
195
bool MapCppTOFDigits::getAdc(Json::Value xDocfAdc, Json::Value xDocTdcHit, Json::Value &xDocDigit) {
 
196
 
 
197
  int n_Adc_hits = xDocfAdc.size();
 
198
  std::string xTofKey_str = JsonWrapper::GetProperty( xDocDigit, 
 
199
                                                          "tof_key", 
 
200
                                                          JsonWrapper::stringValue).asString();
 
201
 
 
202
  TOFChannelKey xTdcTofKey(xTofKey_str);
 
203
 
 
204
  for ( int AdcHitCount = 0; AdcHitCount < n_Adc_hits; AdcHitCount++ ) {
 
205
    std::string xDaqKey_adc_str = JsonWrapper::GetProperty( xDocfAdc[AdcHitCount],
 
206
                                                       "channel_key",
 
207
                                                       JsonWrapper::stringValue).asString();
 
208
 
 
209
    DAQChannelKey xAdcDaqKey(xDaqKey_adc_str);
 
210
    TOFChannelKey* xAdcTofKey = _map.find(&xAdcDaqKey);
 
211
    
 
212
    if ( xTdcTofKey == *xAdcTofKey ){
 
213
      xDocDigit["charge_mm"] = JsonWrapper::GetProperty( xDocfAdc[AdcHitCount],
 
214
                                                        "charge_mm",
 
215
                                                        JsonWrapper::intValue );
 
216
      xDocDigit["charge_pm"] = JsonWrapper::GetProperty( xDocfAdc[AdcHitCount],
 
217
                                                        "charge_pm",
 
218
                                                        JsonWrapper::intValue );
 
219
      return true;
 
220
    }
 
221
  }
 
222
 
 
223
  return false;
 
224
}
 
225
 
 
226
bool MapCppTOFDigits::getTrig( Json::Value xDocTrig, Json::Value xDocTdcHit, Json::Value &xDocDigit ) {
 
227
  Json::Value xDocT = JsonWrapper::GetProperty( xDocTrig, "V1290", JsonWrapper::arrayValue);
 
228
  int HitGeo = JsonWrapper::GetProperty(xDocTdcHit , "geo", JsonWrapper::intValue).asInt();
 
229
  int n_count = xDocT.size();
 
230
  //loop over all of the triggers for this particle event.
 
231
  for ( int TrigCount = 0; TrigCount < n_count; TrigCount++ ){
 
232
    Json::Value Trig = JsonWrapper::GetItem( xDocT, TrigCount, JsonWrapper::objectValue);
 
233
 
 
234
    int TrGeo = JsonWrapper::GetProperty(Trig, "geo", JsonWrapper::intValue).asInt();
 
235
    // If the Geo number of the trigger request matches the Geo number for the TDC then add the 
 
236
    // trigger information to the digit.
 
237
    if ( TrGeo == HitGeo ){
 
238
      xDocDigit["trigger_leading_time"]  = JsonWrapper::GetProperty( Trig,
 
239
                                                                     "leading_time",
 
240
                                                                     JsonWrapper::intValue);
 
241
      xDocDigit["trigger_trailing_time"] = JsonWrapper::GetProperty( Trig,
 
242
                                                                     "trailing_time",
 
243
                                                                     JsonWrapper::intValue);
 
244
 
 
245
      return true;  // will break the loop  and return truewhen it finds the first trigger that matches.
 
246
    }
 
247
  }
 
248
 
 
249
  return false; 
 
250
}
 
251
 
 
252
bool MapCppTOFDigits::getTrigReq( Json::Value xDocTrigReq, Json::Value xDocTdcHit, Json::Value &xDocDigit ) {
 
253
  Json::Value xDocTR = JsonWrapper::GetProperty( xDocTrigReq, "V1290", JsonWrapper::arrayValue);
 
254
  int HitGeo = JsonWrapper::GetProperty(xDocTdcHit , "geo", JsonWrapper::intValue).asInt();
 
255
  int n_req_count = xDocTR.size();
 
256
 
 
257
  //loop over all of the trigger requests for this particle event.
 
258
  for ( int TrigReqCount = 0; TrigReqCount < n_req_count; TrigReqCount++ ){
 
259
    Json::Value TrigReq = JsonWrapper::GetItem( xDocTR, TrigReqCount, JsonWrapper::objectValue);
 
260
 
 
261
    int TrReqGeo = JsonWrapper::GetProperty(TrigReq, "geo", JsonWrapper::intValue).asInt();
 
262
    // If the Geo number of the trigger request matches the Geo number for the TDC then add the 
 
263
    // trigger request information to the digit.
 
264
    if ( TrReqGeo == HitGeo ){
 
265
      xDocDigit["trigger_request_leading_time"]  = JsonWrapper::GetProperty( TrigReq,
 
266
                                                                             "leading_time",
 
267
                                                                             JsonWrapper::intValue);
 
268
 
 
269
      xDocDigit["trigger_request_trailing_time"] = JsonWrapper::GetProperty( TrigReq,
 
270
                                                                             "trailing_time",
 
271
                                                                             JsonWrapper::intValue);
 
272
      return true;  // will break the loop when it finds the first request that matches. there may be
 
273
      // multiple requests per board but for now we only get the first one.
 
274
    }
 
275
  }
 
276
 
 
277
  return false;
 
278
}