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