er  dev
ERNDUnpack.cxx
1 #include "ERNDUnpack.h"
2 
3 #include <map>
4 #include <tuple>
5 
6 #include "TVector3.h"
7 
8 #include "FairRootManager.h"
9 #include "FairLogger.h"
10 
11 #include "DetEventFull.h"
12 #include "DetEventStation.h"
13 #include "DetMessage.h"
14 
15 #include "ERDetectorList.h"
16 #include "ERNDDigi.h"
17 #include "ERSupport.h"
18 
19 //--------------------------------------------------------------------------------------------------
20 ERNDUnpack::ERNDUnpack(TString detName, TString ampStation, TString timeStation, TString tacStation,
21  TString ampCalFile, TString timeCalFile, TString tacCalFile,
22  ChannelMapping* channelsMapping /*=nullptr*/,
23  Bool_t skipAloneChannels/*= kTRUE*/) :
24  ERUnpack(detName),
25  fAmpStation(ampStation),
26  fTimeStation(timeStation),
27  fTACStation(tacStation),
28  fChannelMapping(channelsMapping),
29  fSkipAloneChannels(skipAloneChannels)
30 {
31  if (ampCalFile != "")
32  fAmpCalTable = ReadCalFile(ampCalFile);
33  if (timeCalFile != "")
34  fTimeCalTable = ReadCalFile(timeCalFile);
35  if (tacCalFile != "")
36  fTACCalTable = ReadCalFile(tacCalFile);
37 }
38 //--------------------------------------------------------------------------------------------------
39 void ERNDUnpack::Register() {
40  FairRootManager* ioman = FairRootManager::Instance();
41  if ( ! ioman ) Fatal("Init", "No FairRootManager");
42  digi_collections_["NDDigis"] = new TClonesArray("ERNDDigi", 10);
43  ioman->Register("NDDigi", "ND", digi_collections_["NDDigis"], kTRUE /* save to file */);
44  if (!CheckSetup())
45  Fatal("Init", "Error in ERNDUnpack setup checking !");
46 }
47 //--------------------------------------------------------------------------------------------------
48 std::vector<TString> ERNDUnpack::InputBranchNames() const {
49  return {fAmpStation, fTimeStation, fTACStation};
50 }
51 //--------------------------------------------------------------------------------------------------
52 void ERNDUnpack::UnpackSignalFromStations() {
53  ChannelToAmpTimeTac channel_to_signals;
54  UnpackAmpTimeTACStation(signals_from_stations_[fAmpStation],
55  signals_from_stations_[fTimeStation],
56  signals_from_stations_[fTACStation],
57  channel_to_signals,
58  fSkipAloneChannels);
59  for (auto itValue : channel_to_signals) {
60  ERChannel channel = itValue.first;
61  float amp, time, tac;
62  std::tie(amp, time, tac) = itValue.second;
63  ApplyCalibrations(channel, amp /*[MeV]*/, time, tac);
64  AddNDDigi(amp,time,tac, GetChannelNumber(channel, fChannelMapping));
65  }
66 }
67 //--------------------------------------------------------------------------------------------------
68 void ERNDUnpack::ApplyCalibrations(const ERChannel channel, float& amp, float& time, float& tac) {
69  const auto applyCalibration = [this, channel](float& value, const TMatrixD* table) {
70  if (!table)
71  return;
72  if (channel >= table->GetNrows()){
73  LOG(FATAL) << "Channel " << channel << " not found in amplitude calibration table of detector "
74  << detector_name_ << FairLogger::endl;
75  }
76  value = value*(*table)[channel][1] + (*table)[channel][0];
77  };
78  applyCalibration(amp, fAmpCalTable);
79  applyCalibration(time, fTimeCalTable);
80  applyCalibration(tac, fTACCalTable);
81 }
82 //--------------------------------------------------------------------------------------------------
83 void ERNDUnpack::AddNDDigi(const float edep, const float time, const float tac,
84  const ERChannel channelNb) {
85  auto* digiCollection = digi_collections_["NDDigis"];
86  new((*digiCollection) [digiCollection->GetEntriesFast()])
87  ERNDDigi(channelNb, edep, -1. /*lightYield*/, time, -1. /*neutronProb*/, tac);
88 }
89 //--------------------------------------------------------------------------------------------------
90 Bool_t ERNDUnpack::CheckSetup() {
91  const auto stationsInConfig = setup_configuration_->GetStationList(detector_name_);
92  const auto stationInConfig = [stationsInConfig, this](const TString stationName,
93  const TString type) {
94  if (stationsInConfig.find(stationName) == stationsInConfig.end()){
95  LOG(FATAL) << type << " station " << stationName << " of ND " << detector_name_
96  << " not found in setup configuration file" << FairLogger::endl;
97  return kFALSE;
98  }
99  return kTRUE;
100  };
101  if (!stationInConfig(fAmpStation, "Amplitude"))
102  return kFALSE;
103  if (!stationInConfig(fTimeStation, "Time"))
104  return kFALSE;
105  if (!stationInConfig(fTACStation, "TAC"))
106  return kFALSE;
107  return kTRUE;
108 }