~mdrews/maus/my-branch

« back to all changes in this revision

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

  • Committer: Durga Rajaram
  • Date: 2014-01-14 07:07:02 UTC
  • mfrom: (659.1.80 relcand)
  • Revision ID: durga@fnal.gov-20140114070702-2l1fuj1w6rraw7xe
Tags: MAUS-v0.7.6
MAUS-v0.7.6

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/DAQChannelMap.hh"
 
21
#include "Interface/dataCards.hh"
 
22
#include "src/common_cpp/Converter/DataConverters/CppJsonSpillConverter.hh"
 
23
#include "src/common_cpp/Converter/DataConverters/JsonCppSpillConverter.hh"
 
24
 
 
25
#include "src/map/MapCppEMRPlaneHits/MapCppEMRPlaneHits.hh"
 
26
 
 
27
namespace MAUS {
 
28
 
 
29
bool MapCppEMRPlaneHits::birth(std::string argJsonConfigDocument) {
 
30
  _classname = "MapCppEMRPlaneHits";
 
31
  char* pMAUS_ROOT_DIR = getenv("MAUS_ROOT_DIR");
 
32
 
 
33
  if (!pMAUS_ROOT_DIR) {
 
34
    Squeak::mout(Squeak::error)
 
35
    << "Could not find the $MAUS_ROOT_DIR environmental variable." << std::endl;
 
36
    Squeak::mout(Squeak::error) << "Did you try running: source env.sh ?" << std::endl;
 
37
    return false;
 
38
  }
 
39
 
 
40
  try {
 
41
    //  JsonCpp setup
 
42
    Json::Value configJSON;
 
43
    Json::Value map_file_name;
 
44
    Json::Value xEnable_V1731_Unpacking;
 
45
    Json::Value xEnable_DBB_Unpacking;
 
46
 
 
47
    configJSON = JsonWrapper::StringToJson(argJsonConfigDocument);
 
48
 
 
49
    map_file_name = JsonWrapper::GetProperty(configJSON,
 
50
                                             "EMR_cabling_file",
 
51
                                             JsonWrapper::stringValue);
 
52
 
 
53
    std::string xMapFile = std::string(pMAUS_ROOT_DIR) + map_file_name.asString();
 
54
    bool loaded = _emrMap.InitFromFile(xMapFile);
 
55
    if (!loaded)
 
56
      return false;
 
57
 
 
58
    // !!!!!!!!!!!!!!!!!!!!!!
 
59
    _trigger_window_lower = -400;
 
60
    _trigger_window_upper = 1000;
 
61
    // !!!!!!!!!!!!!!!!!!!!!!
 
62
 
 
63
    xEnable_V1731_Unpacking = JsonWrapper::GetProperty(configJSON,
 
64
                                                       "Enable_V1731_Unpacking",
 
65
                                                       JsonWrapper::booleanValue);
 
66
    if (!xEnable_V1731_Unpacking.asBool()) {
 
67
      Squeak::mout(Squeak::warning)
 
68
      << "WARNING in MapCppKLDigits::birth. The unpacking of the flashADC V1724 is disabled!!!"
 
69
      << " Are you shure you want this?"
 
70
      << std::endl;
 
71
    }
 
72
 
 
73
    xEnable_DBB_Unpacking   = JsonWrapper::GetProperty(configJSON,
 
74
                                                       "Enable_DBB_Unpacking",
 
75
                                                       JsonWrapper::booleanValue);
 
76
    if (!xEnable_DBB_Unpacking.asBool()) {
 
77
      Squeak::mout(Squeak::warning)
 
78
      << "WARNING in MapCppKLDigits::birth. The unpacking of the flashADC V1724 is disabled!!!"
 
79
      << " Are you shure you want this?"
 
80
      << std::endl;
 
81
    }
 
82
 
 
83
    return true;
 
84
  } catch(Exception exc) {
 
85
    CppErrorHandler::getInstance()->HandleExceptionNoJson(exc, _classname);
 
86
  } catch(std::exception exc) {
 
87
    CppErrorHandler::getInstance()->HandleStdExcNoJson(exc, _classname);
 
88
  }
 
89
 
 
90
  return false;
 
91
}
 
92
 
 
93
bool MapCppEMRPlaneHits::death() {
 
94
  return true;
 
95
}
 
96
 
 
97
string MapCppEMRPlaneHits::process(string document) {
 
98
 
 
99
  Json::Value spill_json;
 
100
  try {
 
101
    spill_json = JsonWrapper::StringToJson(document);
 
102
  } catch(...) {
 
103
    Json::Value errors;
 
104
    std::stringstream ss;
 
105
//     ss << squee.what() << std::endl;
 
106
    ss << _classname << " says: Failed to parse input document";
 
107
    errors["bad_json_document"] = ss.str();
 
108
    spill_json["errors"] = errors;
 
109
    return JsonWrapper::JsonToString(spill_json);
 
110
  }
 
111
 
 
112
  Data* spill_cpp;
 
113
  try {
 
114
    spill_cpp = JsonCppSpillConverter().convert(&spill_json);
 
115
  } catch(...) {
 
116
    Json::Value errors;
 
117
    std::stringstream ss;
 
118
//     ss << squee.what() << std::endl;
 
119
    ss << _classname << " says: Failed to convert the input document into Data";
 
120
    errors["bad_json_document"] = ss.str();
 
121
    spill_json["errors"] = errors;
 
122
    return JsonWrapper::JsonToString(spill_json);
 
123
  }
 
124
 
 
125
  process(spill_cpp);
 
126
 
 
127
  Json::Value* spill_json_out;
 
128
  try {
 
129
    spill_json_out = CppJsonSpillConverter().convert(spill_cpp);
 
130
  } catch(...) {
 
131
    Json::Value errors;
 
132
    std::stringstream ss;
 
133
//     ss << squee.what() << std::endl;
 
134
    ss << _classname << " says: Failed to convert the output Data into Json";
 
135
    errors["bad_cpp_data"] = ss.str();
 
136
    spill_json["errors"] = errors;
 
137
    return JsonWrapper::JsonToString(spill_json);
 
138
  }
 
139
 
 
140
//   std::cerr << (*spill_json_out)["recon_events"] << std::endl;
 
141
  document = JsonWrapper::JsonToString(*spill_json_out);
 
142
 
 
143
  delete spill_cpp;
 
144
  delete spill_json_out;
 
145
  return document;
 
146
}
 
147
 
 
148
void MapCppEMRPlaneHits::process(Data *data) {
 
149
 
 
150
  Spill *spill    = data->GetSpill();
 
151
  if (spill->GetDAQData() == NULL)
 
152
      return;
 
153
  EMRDaq emr_data = spill->GetDAQData()->GetEMRDaq();
 
154
  int nPartEvents = emr_data.GetV1731NumPartEvents();
 
155
//   cout << "nPartEvts: " << nPartEvents << endl;
 
156
  reset_data_tmp(nPartEvents);
 
157
 
 
158
  processFADC(emr_data, nPartEvents);
 
159
  processDBB(emr_data, nPartEvents);
 
160
 
 
161
//   arrange(emr_data);
 
162
  fill(spill, nPartEvents);
 
163
}
 
164
 
 
165
void MapCppEMRPlaneHits::processDBB(EMRDaq EMRdaq, int nPartTrigger) {
 
166
//   cout << "DBBArraySize: " << EMRdaq.GetDBBArraySize() << endl;
 
167
  int nDBBs = EMRdaq.GetDBBArraySize();
 
168
  for (int idbb = 0; idbb < nDBBs; idbb++) {
 
169
 
 
170
    DBBSpillData dbb = EMRdaq.GetDBBArrayElement(idbb);
 
171
    if ( dbb.GetTriggerCount() != nPartTrigger ) {
 
172
      Squeak::mout(Squeak::error)
 
173
      << "ERROR in  MapCppEMRPlaneHits::processDBB: number of triggers mismatch ("
 
174
      << dbb.GetTriggerCount() << "!=" << nPartTrigger << ")" << std::endl;
 
175
      return;
 
176
    }
 
177
 
 
178
    int xLDC    = dbb.GetLdcId();
 
179
    int xSpill  = dbb.GetSpillNumber();
 
180
    int xGeo    = dbb.GetDBBId();
 
181
    int nHits   = dbb.GetDBBHitsArraySize();
 
182
//     int nTr     = dbb.GetDBBTriggersArraySize();
 
183
 
 
184
    for (int ihit = 0; ihit < nHits; ihit++) {
 
185
      DBBHit this_hit = dbb.GetDBBHitsArrayElement(ihit);
 
186
      int xCh = this_hit.GetChannel();
 
187
      int lt  = this_hit.GetLTime();
 
188
      int tt  = this_hit.GetTTime();
 
189
 
 
190
      DAQChannelKey daq_key(xLDC, xGeo, xCh, 141, "emr");
 
191
//       cout << daq_key << endl;
 
192
      EMRChannelKey *emr_key = _emrMap.find(&daq_key);
 
193
      if (emr_key) {
 
194
        int xPlane = emr_key->plane();
 
195
//         int xOri   = emr_key->orientation();
 
196
        int xBar   = emr_key->bar();
 
197
//         cout << *emr_key << " --> lt: " << lt << "  tt: " << tt << endl;
 
198
 
 
199
        for (int ipe = 0; ipe < nPartTrigger; ipe++) {
 
200
          DBBHit this_trigger = dbb.GetDBBTriggersArrayElement(ipe);
 
201
          int tr_lt = this_trigger.GetLTime();
 
202
//           int tr_tt = this_trigger.GetTTime();
 
203
//           int xCh   = this_trigger.GetChannel();
 
204
 
 
205
          if (ihit == 0) { // Set the plane information only when processing the very first hit.
 
206
            _emr_events_tmp2[ipe][xPlane]._time  = tr_lt;
 
207
            _emr_events_tmp2[ipe][xPlane]._spill = xSpill;
 
208
          }
 
209
 
 
210
          int delta_t = lt - tr_lt;
 
211
          if (delta_t > _trigger_window_lower && delta_t < _trigger_window_upper) {
 
212
            EMRBarHit bHit;
 
213
            bHit.SetTot(tt-lt);
 
214
            bHit.SetDeltaT(delta_t - _trigger_window_lower);
 
215
//             cout << "*---> " << *emr_key << " --> trigger_Id: " << ipe
 
216
//                  << "  tot: " << tt-lt
 
217
//                  << "  delta: " << delta_t - _trigger_window_lower
 
218
//                  << "(" << delta_t << ")" << endl;
 
219
            _emr_events_tmp4[ipe][xPlane][xBar].push_back(bHit);
 
220
          }
 
221
        }
 
222
      }/* else {cout << "WARNING!!! unknow EMR DBB channel " << daq_key << endl;}*/
 
223
    }
 
224
  }
 
225
}
 
226
 
 
227
void MapCppEMRPlaneHits::processFADC(EMRDaq EMRdaq, int nPartTrigger) {
 
228
//   cout << "GetV1731NumPartEvents: " << EMRdaq.GetV1731NumPartEvents() << endl;
 
229
 
 
230
  for (int ipe = 0; ipe < nPartTrigger; ipe++) {
 
231
 
 
232
    V1731HitArray fADChits = EMRdaq.GetV1731PartEvent(ipe);
 
233
    int nHits = fADChits.size();
 
234
//     cout << "PartEvent " << ipe << "  --> " << nHits << " hits\n" << endl;
 
235
 
 
236
    for (int ihit = 0; ihit < nHits; ihit++) {
 
237
      V1731 fADChit = fADChits[ihit];
 
238
      int xLDC  = fADChit.GetLdcId();
 
239
      int xGeo  = fADChit.GetGeo();
 
240
      int xCh   = fADChit.GetChannel();
 
241
      int xArea = fADChit.GetPulseArea();
 
242
      int xPos  = fADChit.GetPositionMin();
 
243
 
 
244
      DAQChannelKey daq_key(xLDC, xGeo, xCh, 121, "emr");
 
245
//       cout << daq_key << endl;
 
246
      EMRChannelKey *emr_key = _emrMap.find(&daq_key);
 
247
      if (emr_key) {
 
248
        int xPlane = emr_key->plane();
 
249
        int xOri   = emr_key->orientation();
 
250
//         cout << ipe << "  " << *emr_key << " --> pos: "
 
251
//              << xPos << "  area: " << xArea << endl << endl;
 
252
        _emr_events_tmp2[ipe][xPlane]._orientation = xOri;
 
253
        _emr_events_tmp2[ipe][xPlane]._charge      = xArea;
 
254
        _emr_events_tmp2[ipe][xPlane]._deltat      = xPos;
 
255
      }/* else {cout << "WARNING!!! unknow EMR fADC channel " << daq_key << endl;}*/
 
256
    }
 
257
  }
 
258
}
 
259
 
 
260
void MapCppEMRPlaneHits::fill(Spill *spill, int nPartTrigger) {
 
261
  ReconEventPArray *recEvts =  spill->GetReconEvents();
 
262
  if (recEvts->size() == 0) {
 
263
    for (int ipe = 0; ipe < nPartTrigger; ipe++)
 
264
      recEvts->push_back(new ReconEvent);
 
265
  }
 
266
 
 
267
  for (int ipe = 0; ipe < nPartTrigger; ipe++) {
 
268
    EMREvent *evt = new EMREvent;
 
269
    EMRPlaneHitArray plArray;
 
270
 
 
271
    for (int ipl = 0; ipl < NUM_DBB_PLANES; ipl++) {
 
272
      EMRPlaneHit *plHit = new EMRPlaneHit;
 
273
      plHit->SetPlane(ipl);
 
274
      plHit->SetTrigger(ipe);
 
275
 
 
276
      EMRPlaneData xPlData = _emr_events_tmp2[ipe][ipl];
 
277
      int xOri     = xPlData._orientation;
 
278
      int xCharge  = xPlData._charge;
 
279
      int xTime    = xPlData._time;
 
280
      int xSpill   = xPlData._spill;
 
281
      int xDeltaT  = xPlData._deltat;
 
282
 
 
283
      plHit->SetOrientation(xOri);
 
284
      plHit->SetCharge(xCharge);
 
285
      plHit->SetTime(xTime);
 
286
      plHit->SetSpill(xSpill);
 
287
      plHit->SetDeltaT(xDeltaT);
 
288
 
 
289
      EMRBarArray barArray;
 
290
 
 
291
      for (int ibar = 0; ibar < NUM_DBB_CHANNELS; ibar++) {
 
292
        int nHits = _emr_events_tmp4[ipe][ipl][ibar].size();
 
293
        if ( nHits ) {
 
294
          EMRBar *bar = new EMRBar;
 
295
          bar->SetBar(ibar);
 
296
          bar->SetEMRBarHitArray(_emr_events_tmp4[ipe][ipl][ibar]);
 
297
          barArray.push_back(bar);
 
298
//           cout << "Ev: " << ipe << "  Pl: " << ipl
 
299
//                << "   Bar: " << ibar << "  Hits: " <<  nHits << endl;
 
300
        }
 
301
      }
 
302
 
 
303
      plHit->SetEMRBarArray(barArray);
 
304
      if ( barArray.size() || xCharge ) {
 
305
        plArray.push_back(plHit);
 
306
//         cout << "Ev: " << ipe << "  pl: " << ipl << "  --> pos: " << xPos << "  area: " << xArea
 
307
//              << "  nBarHits: " << barArray.size() << endl;
 
308
      } else {
 
309
        delete plHit;
 
310
      }
 
311
    }
 
312
 
 
313
    evt->SetEMRPlaneHitArray(plArray);
 
314
    recEvts->at(ipe)->SetEMREvent(evt);
 
315
  }
 
316
  spill->SetReconEvents(recEvts);
 
317
}
 
318
 
 
319
void MapCppEMRPlaneHits::reset_data_tmp(int nPartEvts) {
 
320
  _emr_events_tmp4.resize(0);
 
321
  _emr_events_tmp4.resize(nPartEvts);
 
322
  for (int ipe = 0; ipe < nPartEvts ;ipe++) {
 
323
    _emr_events_tmp4[ipe].resize(NUM_DBB_PLANES);  // number of planes
 
324
    for (int iplane = 0; iplane < NUM_DBB_PLANES; iplane++) {
 
325
      _emr_events_tmp4[ipe][iplane].resize(NUM_DBB_CHANNELS); // number of bars in a plane
 
326
    }
 
327
  }
 
328
 
 
329
  _emr_events_tmp2.resize(0);
 
330
  _emr_events_tmp2.resize(nPartEvts);
 
331
  for (int ipe = 0; ipe < nPartEvts ;ipe++) {
 
332
    _emr_events_tmp2[ipe].resize(NUM_DBB_PLANES);
 
333
  }
 
334
}
 
335
}