er  dev
ERTelescopeUnpack.cxx
1 #include "ERTelescopeUnpack.h"
2 
3 #include <fstream>
4 
5 #include "TClonesArray.h"
6 
7 #include "FairRootManager.h"
8 #include "FairLogger.h"
9 
10 #include "DetEventFull.h"
11 #include "DetEventStation.h"
12 #include "DetMessage.h"
13 
14 #include "ERSupport.h"
15 #include "ERDigi.h"
16 
17 //--------------------------------------------------------------------------------------------------
18 ERTelescopeUnpack::ERTelescopeUnpack(TString detName):
19  ERUnpack(detName) { }
20 //--------------------------------------------------------------------------------------------------
21 ERTelescopeUnpack::~ERTelescopeUnpack(){
22  for (auto itStation : fStations){
23  if (itStation.second)
24  delete itStation.second;
25  }
26 }
27 //--------------------------------------------------------------------------------------------------
28 void ERTelescopeUnpack::Register() {
29  if (fStations.size() == 0)
30  LOG(FATAL) << "No Stations in ERTelescopeUnpack !" << FairLogger::endl;
31  if (!CheckSetup())
32  LOG(FATAL) << "Error in ERTelescopeUnpack setup checking !" << FairLogger::endl;
33  if (!ReadCalFiles())
34  LOG(FATAL) << "Problem in ReadCalFiles!" << FairLogger::endl;
35  FormAllBranches();
36  DumpStationsInfo();
37 }
38 //--------------------------------------------------------------------------------------------------
39 std::vector<TString> ERTelescopeUnpack::InputBranchNames() const {
40  std::vector<TString> station_names;
41  for (const auto& it_station : fStations) {
42  auto* station = it_station.second;
43  for (const auto& station_name : {station->ampStName, station->timeStName,
44  station->ampStName2, station->timeStName2}) {
45  if (station_name != "") {
46  station_names.push_back(station_name);
47  }
48  }
49  }
50  return station_names;
51 }
52 //--------------------------------------------------------------------------------------------------
53 void ERTelescopeUnpack::CreateDigisFromRawStations(
54  const TString& er_station, const TString& er_branch_name,
55  const TString& amp_station, const TString& time_station,
56  TMatrixD* amp_cal_table, TMatrixD* time_cal_table,
57  const ChannelMapping* channel_mapping, bool skip_alone_channels) {
58  ChannelToAmpTime channel_to_signals;
59  UnpackAmpTimeStation(signals_from_stations_[amp_station],
60  signals_from_stations_[time_station],
61  channel_to_signals,
62  skip_alone_channels);
63  for (const auto it : channel_to_signals){
64  const auto raw_channel = it.first;
65  const auto raw_amp = it.second.first;
66  const auto raw_time = it.second.second;
67  const auto amp_and_time = ApplyCalibration(raw_channel, raw_amp, raw_time,
68  amp_cal_table, time_cal_table);
69  AddDigi(amp_and_time.first, amp_and_time.second,
70  GetChannelNumber(it.first, channel_mapping), er_branch_name);
71  }
72 }
73 //--------------------------------------------------------------------------------------------------
74 void ERTelescopeUnpack::UnpackSignalFromStations() {
75  for (auto itStation : fStations) {
76  LOG(DEBUG) << itStation.first << " unpacking ..." << FairLogger::endl;
77  const auto* station = itStation.second;
78  CreateDigisFromRawStations(itStation.first, station->bName, station->ampStName, station->timeStName,
79  station->ampCalTable, station->timeCalTable, itStation.second->channelsMapping1,
80  station->skipAloneChannels);
81  if (station->sideCount == 2) {
82  CreateDigisFromRawStations(itStation.first, station->bName2, station->ampStName2, station->timeStName2,
83  station->ampCalTable2, station->timeCalTable2, itStation.second->channelsMapping2,
84  station->skipAloneChannels);
85  }
86  }
87 }
88 //--------------------------------------------------------------------------------------------------
89 void ERTelescopeUnpack::AddDigi(float edep, float time, ERChannel stripNb, TString digiBranchName)
90 {
91  new((*digi_collections_[digiBranchName])
92  [digi_collections_[digiBranchName]->GetEntriesFast()])ERDigi(edep, time, stripNb);
93 }
94 //--------------------------------------------------------------------------------------------------
95 TString ERTelescopeUnpack::FormBranchName(TString type, Int_t sideCount, TString stName,
96  TString XY, TString XYside) {
97  TString branch_name;
98  if (type == "CsI") {
99  branch_name.Form("TelescopeDigi_%s_%s",detector_name_.Data(),stName.Data());
100  } else {
101  if (sideCount == 1) {
102  branch_name.Form("TelescopeDigi_%s_SingleSi_%s_%s",detector_name_.Data(),stName.Data(),
103  XYside.Data());
104  } else {
105  branch_name.Form("TelescopeDigi_%s_DoubleSi_%s_%s_%s",detector_name_.Data(), stName.Data(),
106  XY.Data(), XYside.Data());
107  }
108  }
109  return branch_name;
110 }
111 //--------------------------------------------------------------------------------------------------
112 void ERTelescopeUnpack::AddSingleSiStation(TString name, TString ampStName, TString timeStName,
113  TString ampCalFile, TString timeCalFile, TString XYside,
114  ChannelMapping* channelsMapping/* = nullptr*/,
115  Bool_t skipAloneChannels/* = kTRUE*/){
116  ERTelescopeStation* st = new ERTelescopeStation( "Si", 1, ampStName, timeStName, "", "", ampCalFile, timeCalFile, "", "","", XYside,
117  channelsMapping, nullptr, skipAloneChannels);
118  fStations[name] = st;
119 }
120 //--------------------------------------------------------------------------------------------------
121 void ERTelescopeUnpack::AddDoubleSiStation(TString name, TString ampStName, TString timeStName,
122  TString ampStName2, TString timeStName2, TString ampCalFile, TString timeCalFile,
123  TString ampCalFile2, TString timeCalFile2, TString XY,
124  ChannelMapping* channelsMapping1/* = nullptr*/,
125  ChannelMapping* channelsMapping2/* = nullptr*/,
126  Bool_t skipAloneChannels/* = kTRUE*/){
127  ERTelescopeStation* st = new ERTelescopeStation( "Si", 2, ampStName, timeStName, ampStName2, timeStName2, ampCalFile, timeCalFile,
128  ampCalFile2, timeCalFile2, XY, "", channelsMapping1, channelsMapping2, skipAloneChannels);
129  fStations[name] = st;
130 }
131 //--------------------------------------------------------------------------------------------------
132 void ERTelescopeUnpack::AddCsIStation(TString name,TString ampStName, TString timeStName, TString ampCalFile, TString timeCalFile,
133  ChannelMapping* channelsMapping /* = nullptr*/, Bool_t skipAloneChannels/* = kTRUE*/){
134  ERTelescopeStation* st = new ERTelescopeStation( "CsI", -1, ampStName, timeStName, "", "", ampCalFile, timeCalFile, "", "", "", "",
135  channelsMapping, nullptr, skipAloneChannels);
136  fStations[name] = st;
137 }
138 //--------------------------------------------------------------------------------------------------
139 void ERTelescopeUnpack::FormAllBranches(){
140  FairRootManager* ioman = FairRootManager::Instance();
141  if ( ! ioman ) Fatal("Init", "No FairRootManager");
142  for (auto itStation : fStations){
143  if( itStation.second->sideCount == 2){
144  if (itStation.second->XY == "XY"){
145  itStation.second->bName = FormBranchName("Si",2,itStation.first,"XY","X");
146  itStation.second->bName2 = FormBranchName("Si",2,itStation.first,"XY","Y");
147  }
148  else{
149  itStation.second->bName = FormBranchName("Si",2,itStation.first,"XY","Y");
150  itStation.second->bName2 = FormBranchName("Si",2,itStation.first,"XY","X");
151  }
152  }
153  else {
154  itStation.second->bName = FormBranchName(itStation.second->type,
155  itStation.second->sideCount,
156  itStation.first,"",
157  itStation.second->XYside);
158  }
159  }
160  for (auto itStation : fStations){
161  TString bName = itStation.second->bName;
162  if (itStation.second->type == "Si") {
163  digi_collections_[bName] = new TClonesArray("ERDigi", 10);
164  ioman->Register(bName,detector_name_, digi_collections_[bName], kTRUE);
165  if (itStation.second->sideCount == 2){
166  TString bName2 = itStation.second->bName2;
167  digi_collections_[bName2] = new TClonesArray("ERDigi", 10);
168  ioman->Register(bName2,detector_name_, digi_collections_[bName2], kTRUE);
169  }
170  }
171  if (itStation.second->type == "CsI") {
172  digi_collections_[bName] = new TClonesArray("ERDigi", 10);
173  ioman->Register(bName,detector_name_, digi_collections_[bName], kTRUE);
174  }
175  }
176 }
177 //--------------------------------------------------------------------------------------------------
178 void ERTelescopeUnpack::DumpStationsInfo(){
179  LOG(INFO) << "!!! Stations info: " << FairLogger::endl;
180  for (auto itStation : fStations){
181  LOG(INFO) << "\t" << itStation.first << FairLogger::endl;
182  LOG(INFO) << "\t\ttype : " << itStation.second->type << FairLogger::endl <<
183  "\t\tsideCount : " << itStation.second->sideCount << FairLogger::endl <<
184  "\t\tampStName : " << itStation.second->ampStName << FairLogger::endl <<
185  "\t\ttimeStName : " << itStation.second->timeStName << FairLogger::endl <<
186  "\t\tampStName2 : " << itStation.second->ampStName2 << FairLogger::endl <<
187  "\t\ttimeStName2 : " << itStation.second->timeStName2 << FairLogger::endl <<
188  "\t\tXY : " << itStation.second->XY << FairLogger::endl <<
189  "\t\tXYside : " << itStation.second->XYside << FairLogger::endl <<
190  "\t\tbName : " << itStation.second->bName << FairLogger::endl <<
191  "\t\tbName2 : " << itStation.second->bName2 << FairLogger::endl;
192  if (itStation.second->ampCalTable){
193  LOG(INFO) << "\t\tampCalFile : " << itStation.second->ampCalFile << FairLogger::endl;
194  LOG(INFO) << "\t\tampCalTable : " << FairLogger::endl;
195  itStation.second->ampCalTable->Print();
196  }
197  if (itStation.second->timeCalTable){
198  LOG(INFO) << "\t\ttimeCalFile : " << itStation.second->timeCalFile << FairLogger::endl;
199  LOG(INFO) << "\t\ttimeCalTable : " << FairLogger::endl;
200  itStation.second->timeCalTable->Print();
201  }
202  if (itStation.second->ampCalTable2){
203  LOG(INFO) << "\t\tampCalFile2 : " << itStation.second->ampCalFile2 << FairLogger::endl;
204  LOG(INFO) << "\t\tampCalTable2 : " << FairLogger::endl;
205  itStation.second->ampCalTable2->Print();
206  }
207  if (itStation.second->timeCalTable2){
208  LOG(INFO) << "\t\ttimeCalFile2 : " << itStation.second->timeCalFile2 << FairLogger::endl;
209  LOG(INFO) << "\t\ttimeCalTable2 : " << FairLogger::endl;
210  itStation.second->timeCalTable2->Print();
211  }
212  }
213 }
214 //--------------------------------------------------------------------------------------------------
215 ERTelescopeStation::ERTelescopeStation(TString _type, Int_t _sideCount, TString _ampStName, TString _timeStName,
216  TString _ampStName2, TString _timeStName2, TString _ampCalFile, TString _timeCalFile,
217  TString _ampCalFile2, TString _timeCalFile2, TString _XY, TString _XYside,
218  ChannelMapping* _channelsMapping1, ChannelMapping* _channelsMapping2,
219  Bool_t _skipAloneChannels):
220  type(_type),
221  sideCount(_sideCount),
222  ampStName(_ampStName),
223  timeStName(_timeStName),
224  ampStName2(_ampStName2),
225  timeStName2(_timeStName2),
226  ampCalFile(_ampCalFile),
227  timeCalFile(_timeCalFile),
228  ampCalFile2(_ampCalFile2),
229  timeCalFile2(_timeCalFile2),
230  XY(_XY),
231  XYside(_XYside),
232  bName(""),
233  bName2(""),
234  channelsMapping1(_channelsMapping1),
235  channelsMapping2(_channelsMapping2),
236  skipAloneChannels(_skipAloneChannels)
237 {
238 }
239 //--------------------------------------------------------------------------------------------------
240 Bool_t ERTelescopeUnpack::ReadCalFiles(){
241  for (auto itStation : fStations){
242 
243  if (itStation.second->ampCalFile != ""){
244  itStation.second->ampCalTable = ReadCalFile(itStation.second->ampCalFile);
245  if (!itStation.second->ampCalTable)
246  return kFALSE;
247  }
248 
249  if (itStation.second->timeCalFile != ""){
250  itStation.second->timeCalTable = ReadCalFile(itStation.second->timeCalFile);
251  if (!itStation.second->timeCalTable)
252  return kFALSE;
253  }
254 
255  if (itStation.second->sideCount == 2 && itStation.second->ampCalFile2 != ""){
256  itStation.second->ampCalTable2 = ReadCalFile(itStation.second->ampCalFile2);
257  if (!itStation.second->ampCalTable2)
258  return kFALSE;
259  }
260 
261  if (itStation.second->sideCount == 2 && itStation.second->timeCalFile2 != ""){
262  itStation.second->timeCalTable2 = ReadCalFile(itStation.second->timeCalFile2);
263  if (!itStation.second->timeCalTable2)
264  return kFALSE;
265  }
266 
267  }
268  return kTRUE;
269 }
270 //--------------------------------------------------------------------------------------------------
271 std::pair<float, float> ERTelescopeUnpack::
272 ApplyCalibration(ERChannel channel, Signal amp, Signal time,
273  TMatrixD* ampCalTable, TMatrixD* timeCalTable) {
274  std::pair<float, float> amp_and_time = {static_cast<float>(amp) , static_cast<float>(time)};
275  if (ampCalTable) {
276  if (channel >= ampCalTable->GetNrows()){
277  LOG(FATAL) << "Channel " << channel << " not found in amplitude calibration table of detector "
278  << detector_name_ << FairLogger::endl;
279  }
280  amp_and_time.first = amp_and_time.first * static_cast<float>((*ampCalTable)[channel][1]) + static_cast<float>((*ampCalTable)[channel][0]);
281  }
282  if (timeCalTable) {
283  if (channel >= timeCalTable->GetNrows()){
284  LOG(FATAL) << "Channel " << channel << " not found in time calibration table of detector "
285  << detector_name_ << FairLogger::endl;
286  }
287  amp_and_time.second = amp_and_time.second * static_cast<float>((*timeCalTable)[channel][1]) + static_cast<float>((*timeCalTable)[channel][0]);
288  }
289  return amp_and_time;
290 }
291 //--------------------------------------------------------------------------------------------------
292 Bool_t ERTelescopeUnpack::CheckSetup() {
293  const std::map<TString, unsigned short> stationsInConfig = setup_configuration_->GetStationList(detector_name_);
294  for (auto itStation : fStations){
295  ERTelescopeStation* station = itStation.second;
296  if (stationsInConfig.find(station->ampStName) == stationsInConfig.end()){
297  LOG(FATAL) << "Amplitude station " << station->ampStName <<
298  " of telescope " << detector_name_ <<
299  " not found in setup configuration file" << FairLogger::endl;
300  return kFALSE;
301  }
302  if (station->timeStName != ""){
303  if (stationsInConfig.find(station->timeStName) == stationsInConfig.end()){
304  LOG(FATAL) << "Time station " << station->timeStName <<
305  " of telescope " << detector_name_ <<
306  " not found in setup configuration file" << FairLogger::endl;
307  return kFALSE;
308  }
309  }
310  if (station->sideCount == 2){
311  if (stationsInConfig.find(station->ampStName2) == stationsInConfig.end()){
312  LOG(FATAL) << "Amplitude station " << station->ampStName2 <<
313  " of telescope " << detector_name_ <<
314  " not found in setup configuration file" << FairLogger::endl;
315  return kFALSE;
316  }
317  if (station->timeStName2 != ""){
318  if (stationsInConfig.find(station->timeStName2) == stationsInConfig.end()){
319  LOG(FATAL) << "Time station " << station->timeStName2 <<
320  " of telescope " << detector_name_ <<
321  " not found in setup configuration file" << FairLogger::endl;
322  return kFALSE;
323  }
324  }
325  }
326  }
327  for (auto itConfSt : stationsInConfig){
328  TString stationName = itConfSt.first;
329  Bool_t found = kFALSE;
330  for (auto itStation : fStations){
331  ERTelescopeStation* station = itStation.second;
332  if (station->ampStName == stationName ||
333  station->timeStName == stationName ||
334  station->ampStName2 == stationName ||
335  station->timeStName2 == stationName)
336  LOG(WARNING) << "Station " << stationName << " in setup file, but not defined to unpack!" << FairLogger::FairLogger::endl;
337  }
338  }
339  return kTRUE;
340 }
341 //--------------------------------------------------------------------------------------------------
342 ClassImp(ERTelescopeUnpack)
Definition: ERDigi.h:15