BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
BmnCscRaw2Digit.cxx
Go to the documentation of this file.
1#include "BmnCscRaw2Digit.h"
2
3// Boost
4#include <boost/program_options.hpp>
5// BmnRoot
6#include "BmnRawDataDecoder.h"
7
8namespace po = boost::program_options;
9
10BmnCscRaw2Digit::BmnCscRaw2Digit(Int_t period, Int_t run, TString MapFileName, BmnSetup bmnSetup)
11 : BmnAdcProcessor(period, run, "CSC", ADC_N_CHANNELS, ADC32_N_SAMPLES, bmnSetup)
13 , fMapFileName(MapFileName)
14{
16
21 MatchMaps();
22}
23
25{
27 for (auto& it : fOuterMap)
28 for (auto& inner : it.second)
29 delete inner.second;
30}
31
33{
34 uint32_t ser = 0;
35 int16_t ch_lo = 0;
36 int16_t ch_hi = 0;
37 uint16_t chan_shift = 0;
38 uint16_t mod = 0;
39 int16_t lay = 0; // in order it to be read as number instead of a char (although it should be int8_t)
40 uint16_t station = 0;
41 string prefix;
42
43 vector<string> channel_map;
44 vector<string> spec_thr;
45 Double_t final_thr = 0;
46 Double_t thr_dif = 0;
47 Int_t n_iters = 0;
48 Double_t cmod_cut = 0;
49
50 // Setup options.
51 po::options_description desc("Options");
52 desc.add_options()("CHANNEL_MAP.channels", po::value<vector<string>>(&channel_map)->multitoken(),
53 "ADC channel -> strip map")("SIGNAL_CONFIGURATION.spec_thr",
54 po::value<vector<string>>(&spec_thr)->multitoken(),
55 "ADC channel -> strip map")(
56 "SIGNAL_CONFIGURATION.Threshold", po::value<Double_t>(&final_thr)->default_value(30), "ADC final threshold")(
57 "SIGNAL_CONFIGURATION.ThresholdDif", po::value<Double_t>(&thr_dif)->default_value(15), "ADC final threshold")(
58 "SIGNAL_CONFIGURATION.NIterations", po::value<Int_t>(&n_iters)->default_value(5), "ADC final threshold")(
59 "SIGNAL_CONFIGURATION.CModCut", po::value<Double_t>(&cmod_cut)->default_value(0), "ADC final threshold");
60
61 // Load config file.
62 po::variables_map vm;
63 TString name = TString(getenv("VMCWORKDIR")) + TString("/input/") + fMapFileName;
64 LOG(info) << "name " << name;
65 LOG(info) << "Loading CSC Map: Period " << fPeriod << ", Run " << fRun << " " << name;
66 ifstream config_file(name.Data(), ifstream::in);
67 if (!config_file.is_open()) {
68 LOG(error) << "Error opening map-file (" << name << ")!";
69 return kBMNERROR;
70 }
71 po::store(po::parse_config_file(config_file, desc), vm);
72 config_file.close();
73 po::notify(vm);
74
75 // set thresholds
76 SetThreshold(final_thr, thr_dif, n_iters, cmod_cut);
77
78 // set specific thresholds
79 Int_t ch = 0;
80 Double_t thr = 0;
81 for (auto& str : spec_thr) {
82 istringstream ss(str);
83 ss >> std::hex >> ser >> std::dec >> ch >> thr;
84 fSpecThreshMap.insert(make_pair(make_pair(ser, ch), thr));
85 LOGF(debug, "Serial 0x%08X Channel %2d Threshold %3.3f", ser, ch, thr);
86 }
87
88 // set channel map
89 for (auto& str : channel_map) {
90 istringstream ss(str);
91 ss >> std::hex >> ser >> std::dec >> ch_lo >> ch_hi >> chan_shift >> station >> mod >> lay >> prefix;
92 BmnCscMapping* record = new BmnCscMapping();
93 record->layer = lay;
94 record->serial = ser;
95 record->chan_shift = chan_shift;
96 record->module = mod;
97 record->channel_low = ch_lo / GetNSamples();
98 record->channel_high = ch_hi / GetNSamples();
99 record->station = station;
100 record->prefix = prefix;
101 // fill local maps map
102 auto itLM = fLocalMapsMap.find(make_pair(prefix, mod));
103 if (itLM == fLocalMapsMap.end()) {
104 auto p = fLocalMapsMap.insert(make_pair(make_pair(prefix, mod), LocalCSC()));
105 p.first->second.shift_map.insert(make_pair(make_pair(mod, lay), chan_shift));
106 } else {
107 LocalCSC& lcsc = itLM->second;
108 lcsc.shift_map.insert(make_pair(make_pair(mod, lay), chan_shift));
109 }
110
111 // fill global map
112 auto it = fOuterMap.find(ser);
113 if (it == fOuterMap.end()) { // create inner channel map for the serial
114 InChanMapCSC inner;
115 inner.insert(make_pair(record->channel_low - 1, nullptr));
116 inner.insert(make_pair(record->channel_high, record));
117 fOuterMap.insert(make_pair(ser, move(inner)));
118 } else { // add range to the existing inner channel map
119 InChanMapCSC& inner = it->second;
120 auto innerItHi = inner.find(record->channel_high);
121 auto innerItLo = inner.find(record->channel_low - 1);
122 if (innerItHi == inner.end()) {
123 inner.insert(make_pair(record->channel_high, record));
124 } else {
125 if (innerItHi->second == nullptr) {
126 inner.erase(innerItHi);
127 inner.insert(make_pair(record->channel_high, record));
128 } else {
129 delete record;
130 // fprintf(stderr, "Wrong %s map! Overlapping intervals for %08X!\n", fDetName.Data(), ser);
131 // return kBMNERROR;
132 }
133 }
134 if (innerItLo == inner.end()) {
135 inner.insert(make_pair(record->channel_low - 1, nullptr));
136 }
137 }
138 }
139
140 return kBMNSUCCESS;
141}
142
144 int8_t* Layers,
145 int16_t* Strips,
146 int8_t& iLay,
147 uint16_t& ChanShift)
148{
149 ifstream inFile(FileName.data());
150 if (!inFile.is_open()) {
151 cout << "Error opening map-file (" << FileName << ")!" << endl;
152 return kBMNERROR;
153 }
154
155 int16_t iStrip(0);
156 uint16_t chan(0);
157 while (inFile >> chan) {
158 uint16_t chan2048 = chan + ChanShift;
159 // printf("\t\t strip %4d chan %4d\n", iStrip, chan2048);
160 Layers[chan2048] = iLay;
161 Strips[chan2048] = iStrip++;
162 }
163
164 return kBMNSUCCESS;
165}
166
168{
169 for (auto& map_el : fLocalMapsMap) {
170 const string& prefix = map_el.first.first;
171 LocalCSC& info = map_el.second;
172 fill_n(info.channel2layer, N_CSC_CHANNELS, -1);
173 fill_n(info.channel2strip, N_CSC_CHANNELS, -1);
174 for (auto& p : info.shift_map) {
175 uint16_t iMod = p.first.first;
176 int8_t iLay = p.first.second;
177 uint16_t chan_shift = p.second;
178 Int_t written_layer = ((iMod == 1) && (fPeriod < 8)) ? ((iLay + 2) % 4) : iLay;
179 string map_file_name = string(getenv("VMCWORKDIR")) + "/input/" + string(fDetName.Data()) + prefix + "m"
180 + to_string(iMod) + "l" + to_string(written_layer) + ".txt";
181 // cout << "map " << map_file_name << endl;
182 if (ReadLocalMap(map_file_name, info.channel2layer, info.channel2strip, iLay, chan_shift) == kBMNERROR) {
183 printf("Error reading %s\n", map_file_name.data());
184 return kBMNERROR;
185 }
186 }
187 }
188 return kBMNSUCCESS;
189}
190
192{
193 for (auto& gl : fOuterMap) {
194 // LOGF(info, "serial %08X ", gl.first);
195 for (auto& inner : gl.second) {
196 BmnCscMapping*& m = inner.second;
197 if (!m)
198 continue;
199 // LOGF(info, "int from %d serial %08X st %d mod %d",
200 // inner.first, m->serial, m->station, m->module);
201 auto lmIt = fLocalMapsMap.find(make_pair(m->prefix, m->module));
202 if (lmIt == fLocalMapsMap.end())
203 continue;
204 m->local_map = &(lmIt->second);
205 }
206 }
207}
208
210{
211 (this->*PrecalcEventModsImp)(adc);
212#ifdef BUILD_DEBUG
214#else
216#endif
217 ProcessAdc(digits, kFALSE);
218 return kBMNSUCCESS;
219}
220
222{
223 (this->*PrecalcEventModsImp)(adc);
224#ifdef BUILD_DEBUG
226#else
228#endif
229 ProcessAdc(nullptr, kTRUE);
230
231 return kBMNSUCCESS;
232}
233
235{
236 const Int_t kNStripsInBunch = BmnRawDataDecoder::samplesCsc;
237 // cout << "Csc: " << kNStripsInBunch << endl;
238 // const Int_t kNBunches = kNStrips / kNStripsInBunch;
239 const Int_t kNThresh = BmnRawDataDecoder::threshCsc;
240
241 for (size_t iCr = 0; iCr < GetNSerials(); ++iCr) {
242 auto it = fOuterMap.find(GetSerials()[iCr]);
243 if (it == fOuterMap.end())
244 continue;
245 for (uint16_t iCh = 0; iCh < GetNChannels(); ++iCh) {
246 InChanMapCSC& inner = it->second;
247 auto innerIt = inner.lower_bound(iCh);
248 if (innerIt == inner.end())
249 continue;
250 BmnCscMapping* rec = innerIt->second;
251 if (!rec)
252 continue;
253 uint16_t station = rec->station;
254 uint16_t module = rec->module;
255 for (uint16_t iSmpl = 0; iSmpl < GetNSamples(); ++iSmpl) {
256 // printf("%10s iCr %2d iCh%2d iSmpl %4d\n", fDetName.Data(), iCr,iCh,iSmpl);
257 if (GetNoisyChipChannels()[iCr][iCh][iSmpl] == kTRUE) {
258 int16_t strip = -1;
259 int8_t layer = -1;
260 MapStrip(rec, iCh, iSmpl, layer, strip);
261 if (strip < 0)
262 continue;
263 fNoisyChannels[station][module][layer][strip] = kTRUE;
264 }
265 }
266 }
267 }
268 for (Int_t iSt = 0; iSt < fCscStationSet->GetNStations(); ++iSt) {
269 auto* st = fCscStationSet->GetStation(iSt);
270 for (UInt_t iMod = 0; iMod < (UInt_t)st->GetNModules(); ++iMod) {
271 auto* mod = st->GetModule(iMod);
272 for (Int_t iLay = 0; iLay < mod->GetNStripLayers(); ++iLay) {
273 auto& lay = mod->GetStripLayer(iLay);
274 Int_t kNBunches = lay.GetNStrips() / kNStripsInBunch;
275 TH1F* prof = fSigProf[iSt][iMod][iLay];
276 for (Int_t iBunch = 0; iBunch < kNBunches; ++iBunch) {
277 Double_t mean = 0.0;
278 for (Int_t iStrip = 0; iStrip < kNStripsInBunch - 1; ++iStrip) {
279 Int_t strip = iStrip + iBunch * kNStripsInBunch;
280 if (fNoisyChannels[iSt][iMod][iLay][strip] == kTRUE)
281 continue;
282 // Double_t curr = prof->GetBinContent(strip);
283 Double_t next = prof->GetBinContent(strip + 1);
284 mean += next;
285 }
286 mean /= kNStripsInBunch;
287 for (Int_t iStrip = 0; iStrip < kNStripsInBunch - 1; ++iStrip) {
288 Int_t strip = iStrip + iBunch * kNStripsInBunch;
289 if (fNoisyChannels[iSt][iMod][iLay][strip] == kTRUE)
290 continue;
291 // Double_t curr = prof->GetBinContent(strip);
292 Double_t next = prof->GetBinContent(strip + 1);
293 // if (kNThresh * meanDiff < next - curr)
294 if (kNThresh * mean < Abs(next - mean))
295 fNoisyChannels[iSt][iMod][iLay][strip] = kTRUE;
296 }
297 }
298 }
299 }
300 }
301 for (size_t iCr = 0; iCr < GetNSerials(); ++iCr) {
302 auto it = fOuterMap.find(GetSerials()[iCr]);
303 if (it == fOuterMap.end())
304 continue;
305 for (uint16_t iCh = 0; iCh < GetNChannels(); ++iCh) {
306 InChanMapCSC& inner = it->second;
307 auto innerIt = inner.lower_bound(iCh);
308 if (innerIt == inner.end())
309 continue;
310 BmnCscMapping* rec = innerIt->second;
311 if (!rec)
312 continue;
313 uint16_t station = rec->station;
314 uint16_t module = rec->module;
315 for (uint16_t iSmpl = 0; iSmpl < GetNSamples(); ++iSmpl) {
316 int16_t strip = -1;
317 int8_t layer = -1;
318 MapStrip(rec, iCh, iSmpl, layer, strip);
319 if (strip < 0)
320 continue;
321 // printf("s %2d m %2d l %2d s %4d\n", station, module, layer, strip);
322 // printf(" iCr %2d iCh %2d iSmpl %4d\n", iCr,iCh, iSmpl);
323 if (fNoisyChannels[station][module][layer][strip] == kTRUE)
324 GetNoisyChipChannels()[iCr][iCh][iSmpl] = kTRUE;
325 }
326 }
327 }
328 return kBMNSUCCESS;
329}
330
331inline void BmnCscRaw2Digit::MapStrip(BmnCscMapping* cscM, uint16_t iCh, uint16_t iSmpl, int8_t& layer, int16_t& strip)
332{
333 Int_t ch2048 = iCh * GetNSamples() + iSmpl;
334 // printf("s %2d m %2d ch2048 %4d\n", station ,module, ch2048);
335 layer = cscM->local_map->channel2layer[ch2048];
336 strip = cscM->local_map->channel2strip[ch2048];
337 // printf("s %2d m %2d l %2d ch %4d s %4d\n", station, module, layer, ch2048, strip);
338 return;
339}
340
342{
343 for (size_t iCr = 0; iCr < GetNSerials(); ++iCr) {
344 auto it = fOuterMap.find(GetSerials()[iCr]);
345 if (it == fOuterMap.end())
346 continue;
347 for (uint16_t iCh = 0; iCh < GetNChannels(); ++iCh) {
348 auto innerIt = it->second.lower_bound(iCh);
349 if (innerIt == it->second.end())
350 continue;
351 BmnCscMapping* rec = innerIt->second;
352 if (!rec)
353 continue;
354 Double_t cs = fCMode[iCr][iCh] - fSMode[iCr][iCh];
355 uint16_t station = rec->station;
356 uint16_t module = rec->module;
357 // LocalCSC* localMaps = rec->local_map;
358 // uint16_t sampleShift = iCh * GetNSamples();
359 // spec threshold
360 Double_t SpecThr(0);
361 auto itThr = fSpecThreshMap.find(make_pair(GetSerials()[iCr], iCh));
362 if (itThr != fSpecThreshMap.end())
363 SpecThr = itThr->second;
364 for (uint16_t iSmpl = 0; iSmpl < GetNSamples(); ++iSmpl) {
365 if ((GetNoisyChipChannels()[iCr][iCh][iSmpl] == kTRUE) || (fAdc[iCr][iCh][iSmpl] == 0))
366 continue;
367 // uint16_t ch2048 = sampleShift + iSmpl;
368 // int16_t strip = localMaps->channel2strip[ch2048];
369 // int8_t layer = localMaps->channel2layer[ch2048];
370 int16_t strip = -1;
371 int8_t layer = -1;
372 MapStrip(rec, iCh, iSmpl, layer, strip);
373 // cout << "DECODER INFO: " << station << " " << module << " "
374 // << layer << " " << strip << endl;
375 // if ((station == 4) && (module==6))
376 // printf("%s st %2d mod %2d l %2d strip %4d iCr %2d ser %08X ch %2d iSmpl %3d adc
377 // %3.3f\n",
378 // fDetName.Data(), station, module, layer, strip, iCr,
379 // GetSerials()[iCr], iCh, iSmpl, fAdc[iCr][iCh][iSmpl]);
380 if (strip < 0)
381 continue;
382 Double_t sig = fAdc[iCr][iCh][iSmpl] - fPedVal[iCr][iCh][iSmpl] + cs;
383 sig = -sig;
384 // Double_t Asig = TMath::Abs(sig);
385 Double_t thr = Max(SpecThr, Max(FinalThr, 3.5 * GetPedestalsRMS()[iCr][iCh][iSmpl]));
386 if ((!GetApplyThreshold()) || (sig > thr)) {
387 if (doFill) {
388 fSigProf[station][module][layer]->Fill(strip);
389 } else {
390 BmnCSCDigit* resDig = new ((*digs)[digs->GetEntriesFast()])
391 BmnCSCDigit(station, module, layer, strip, sig, GetPedestalsRMS()[iCr][iCh][iSmpl]);
392 resDig->SetIsGoodDigit(kTRUE);
393 }
394 }
395 }
396 }
397 }
398}
__m128 m
Definition P4_F32vec4.h:27
bool Bool_t
int Int_t
constexpr Bool_t kFALSE
double Double_t
constexpr Bool_t kTRUE
BmnStatus
Define status code.
Definition BmnEnums.h:93
@ kBMNERROR
Error.
Definition BmnEnums.h:95
@ kBMNSUCCESS
Success.
Definition BmnEnums.h:94
void DeleteStripNoiseArrays(SST &ss)
void InitStripNoiseArrays(SST &ss)
map< PlMapKey, Double_t > fSpecThreshMap
Double_t *** GetPedestalsRMS()
BmnAdcProcessor(uint32_t period, uint32_t run, TString det, uint16_t nCh, const uint16_t nSmpl, BmnSetup setup=kBMNSETUP)
void SetThreshold(Double_t final_thr, Double_t thr_dif=-1, Double_t n_iters=-1, Double_t cmod_cut=-1)
Float_t *** fPedVal
Bool_t **** fNoisyChannels
void GrabSerialsFromMap(unordered_map< UInt_t, cl > m)
const uint16_t GetNChannels()
const uint16_t GetNSamples()
Bool_t *** GetNoisyChipChannels()
void(BmnAdcProcessor::* PrecalcEventModsImp)(TClonesArray *adc)
vector< uint32_t > & GetSerials()
BmnStatus ReadLocalMap(string &FileName, int8_t *Layers, int16_t *Strips, int8_t &iLay, uint16_t &ChanShift)
BmnStatus ReadLocalMaps()
BmnStatus FillNoisyChannels()
BmnCscRaw2Digit(Int_t period, Int_t run, TString MapFileName, BmnSetup bmnSetup=kBMNSETUP)
BmnStatus ReadMapFile()
void ProcessAdc(TClonesArray *csc, Bool_t doFill)
unordered_map< UInt_t, InChanMapCSC > fOuterMap
BmnStatus FillProfiles(TClonesArray *adc)
map< LocalCSCMapKey, LocalCSC > fLocalMapsMap
BmnStatus FillEvent(TClonesArray *adc, TClonesArray *csc)
void MapStrip(BmnCscMapping *cscM, uint16_t iCh, uint16_t iSmpl, int8_t &layer, int16_t &strip)
unique_ptr< BmnCSCStationSet > fCscStationSet
static double threshCsc
void SetIsGoodDigit(Bool_t tmp)
virtual Double_t GetBinContent(Int_t bin) const
Int_t GetEntriesFast() const
const char * Data() const
#define ADC_N_CHANNELS
#define ADC32_N_SAMPLES
map< Int_t, BmnCscMapping * > InChanMapCSC
#define N_CSC_CHANNELS
Double_t Abs(Double_t d)
Double_t Max(Double_t a, Double_t b)
LocalCSC * local_map
uint16_t chan_shift
int16_t channel2strip[N_CSC_CHANNELS]
int8_t channel2layer[N_CSC_CHANNELS]
map< pair< uint16_t, int8_t >, uint16_t > shift_map