er  dev
ERUnpack.cxx
1 #include "ERUnpack.h"
2 
3 #include <iostream>
4 #include <vector>
5 
6 #include "FairLogger.h"
7 
8 //--------------------------------------------------------------------------------------------------
9 ERUnpack::ERUnpack(const TString& detector_name)
10  : FairUnpack(0,0,0,0,0),
11  detector_name_(detector_name)
12 {}
13 //--------------------------------------------------------------------------------------------------
14 Bool_t ERUnpack::Init(const SetupConfiguration* setup_configuration,
15  TChain& input_chain_of_events) {
16  if (inited_)
17  return kFALSE;
18  setup_configuration_ = setup_configuration;
19  Register();
20  ConnectToInputBranches(input_chain_of_events, this->InputBranchNames());
21  inited_ = true;
22  return kTRUE;
23 }
24 //--------------------------------------------------------------------------------------------------
25 void ERUnpack::ConnectToInputBranches(TChain& input_chain_of_events,
26  const std::vector<TString>& branch_names) {
27  if (!setup_configuration_)
28  LOG(FATAL) << "[ERUnpack] Setup configuration was not inited." << FairLogger::endl;
29  for (const auto& station_name : branch_names) {
30  const auto channel_count = setup_configuration_->GetChannelCount(detector_name_, station_name);
31  if (channel_count == -1) {
32  LOG(FATAL) << "[ERUnpack] Setup does not contain station " << station_name
33  << FairLogger::endl;
34  }
35  if (!input_chain_of_events.FindBranch(station_name)) {
36  LOG(FATAL) << "[ERUnpack] Input file does not contain branch for station "
37  << station_name << FairLogger::endl;
38  }
39  auto* signals = new short[channel_count];
40  input_chain_of_events.SetBranchAddress(station_name, signals);
41  signals_from_stations_[station_name] = std::make_pair(signals, channel_count);
42  }
43 }
44 //--------------------------------------------------------------------------------------------------
45 Bool_t ERUnpack::DoUnpack(Int_t* data, Int_t size) {
46  Reset();
47  UnpackSignalFromStations();
48  return kTRUE;
49 }
50 //--------------------------------------------------------------------------------------------------
51 void ERUnpack::Reset() {
52  for (auto& station_name_and_collection : digi_collections_) {
53  auto& digi_collection = station_name_and_collection.second;
54  digi_collection->Delete();
55  }
56 }
57 //--------------------------------------------------------------------------------------------------
58 void FillContainersWithSignals(const Signal* signals, const ERChannel channels_count, Channels& channels,
59  ChannelToSignal& channel_to_signal) {
60  for (ERChannel channel = 0; channel < channels_count; ++channel) {
61  if (signals[channel] == no_signal)
62  continue;
63  channels.push_back(channel);
64  channel_to_signal[channel] = signals[channel];
65  }
66 };
67 //--------------------------------------------------------------------------------------------------
68 void ERUnpack::UnpackStation(SignalsAndChannelCount signals_from_station,
69  ChannelToSignal& channel_to_signal) {
70  Channels channels;
71  FillContainersWithSignals(signals_from_station.first, signals_from_station.second,
72  channels, channel_to_signal);
73 }
74 //--------------------------------------------------------------------------------------------------
75 void ERUnpack::UnpackAmpTimeStation(const SignalsAndChannelCount signals_from_amplitude_station,
76  const SignalsAndChannelCount signals_from_time_station,
77  ChannelToAmpTime& channel_to_signals,
78  const bool skip_alone_channels/*= true*/) {
79  ChannelToSignal channels_to_time_signals, channels_to_amp_signals;
80  Channels amplitude_channels, time_channels;
81  FillContainersWithSignals(signals_from_time_station.first,
82  signals_from_time_station.second,
83  time_channels, channels_to_time_signals);
84  FillContainersWithSignals(signals_from_amplitude_station.first,
85  signals_from_amplitude_station.second,
86  amplitude_channels, channels_to_amp_signals);
87  // sort for intersection and difference algorithm
88  std::sort(time_channels.begin(), time_channels.end());
89  std::sort(amplitude_channels.begin(), amplitude_channels.end());
90  // found intersection in amplitude and time channels
91  Channels intersection_channels;
92  std::set_intersection(time_channels.begin(), time_channels.end(),
93  amplitude_channels.begin(), amplitude_channels.end(),
94  std::back_inserter(intersection_channels));
95  // found alone time and amplitude channels
96  Channels alone_time_channels, alone_amplitude_channels;
97  std::set_difference(time_channels.begin(), time_channels.end(),
98  intersection_channels.begin(), intersection_channels.end(),
99  std::inserter(alone_time_channels, alone_time_channels.begin()));
100  std::set_difference(amplitude_channels.begin(), amplitude_channels.end(),
101  intersection_channels.begin(), intersection_channels.end(),
102  std::inserter(alone_amplitude_channels, alone_amplitude_channels.begin()));
103  // save it
104  for (const auto channel : intersection_channels) {
105  channel_to_signals[channel] = std::make_pair(channels_to_amp_signals[channel],
106  channels_to_time_signals[channel]);
107  }
108  if (!skip_alone_channels) {
109  for (const auto channel : alone_time_channels) {
110  channel_to_signals[channel] = std::make_pair(no_signal, channels_to_time_signals[channel]);
111  }
112  for (const auto channel : alone_amplitude_channels) {
113  channel_to_signals[channel] = std::make_pair(channels_to_amp_signals[channel], no_signal);
114  }
115  }
116 }
117 //--------------------------------------------------------------------------------------------------
118 void ERUnpack::UnpackAmpTimeTACStation(SignalsAndChannelCount signals_from_amplitude_station,
119  SignalsAndChannelCount signals_from_time_station,
120  SignalsAndChannelCount signals_from_tac_station,
121  ChannelToAmpTimeTac& channel_to_signals,
122  bool skip_alone_channels/*= true*/) {
123  ChannelToSignal channels_to_time_signals, channels_to_amp_signals, channels_to_tac_signals;
124  Channels amp_channels, time_channels, tac_channels;
125  FillContainersWithSignals(signals_from_time_station.first,
126  signals_from_time_station.second,
127  time_channels, channels_to_time_signals);
128  FillContainersWithSignals(signals_from_amplitude_station.first,
129  signals_from_amplitude_station.second,
130  amp_channels, channels_to_amp_signals);
131  FillContainersWithSignals(signals_from_tac_station.first,
132  signals_from_tac_station.second,
133  tac_channels, channels_to_tac_signals);
134  // sort for intersection and difference algorithm
135  std::sort(time_channels.begin(), time_channels.end());
136  std::sort(amp_channels.begin(), amp_channels.end());
137  std::sort(tac_channels.begin(), tac_channels.end());
138  // find intersection in amplitude and time channels
139  Channels amp_time_intersection_channels;
140  std::set_intersection(time_channels.begin(), time_channels.end(),
141  amp_channels.begin(), amp_channels.end(),
142  std::back_inserter(amp_time_intersection_channels));
143  // intersect with tac
144  Channels intersection_channels;
145  std::set_intersection(amp_time_intersection_channels.begin(), amp_time_intersection_channels.end(),
146  tac_channels.begin(), tac_channels.end(),
147  std::back_inserter(intersection_channels));
148  // find alone time and amplitude channels
149  Channels time_dif_channels;
150  Channels amp_dif_channels;
151  Channels tac_dif_channels;
152  std::set_difference(time_channels.begin(), time_channels.end(),
153  intersection_channels.begin(), intersection_channels.end(),
154  std::inserter(time_dif_channels, time_dif_channels.begin()));
155  std::set_difference(amp_channels.begin(), amp_channels.end(),
156  intersection_channels.begin(), intersection_channels.end(),
157  std::inserter(amp_dif_channels, amp_dif_channels.begin()));
158  std::set_difference(tac_channels.begin(), tac_channels.end(),
159  intersection_channels.begin(), intersection_channels.end(),
160  std::inserter(tac_dif_channels, tac_dif_channels.begin()));
161  for (const auto channel : intersection_channels) {
162  channel_to_signals[channel] = std::make_tuple(channels_to_amp_signals[channel],
163  channels_to_time_signals[channel],
164  channels_to_tac_signals[channel]);
165  }
166  if (!skip_alone_channels){
167  for (const auto channel : time_dif_channels)
168  channel_to_signals[channel] = std::make_tuple(no_signal, channels_to_time_signals[channel], no_signal);
169  for (const auto channel : amp_dif_channels)
170  channel_to_signals[channel] = std::make_tuple(channels_to_amp_signals[channel], no_signal, no_signal);
171  for (const auto channel : tac_dif_channels)
172  channel_to_signals[channel] = std::make_tuple(no_signal, no_signal, channels_to_tac_signals[channel]);
173  }
174 }
175 //--------------------------------------------------------------------------------------------------
176 ClassImp(ERUnpack)