BmnRoot
Loading...
Searching...
No Matches
BmnSiBTRaw2Digit.cxx
Go to the documentation of this file.
1// Boost
2#include <boost/program_options.hpp>
3// BmnRoot
4#include "BmnSiBTRaw2Digit.h"
5
6namespace po = boost::program_options;
7
8BmnSiBTRaw2Digit::BmnSiBTRaw2Digit(Int_t period, Int_t run, TString MapFileName, BmnSetup bmnSetup)
10{
11 fSetup = bmnSetup;
12
13 fMapFileName = MapFileName;
14 ReadMapFile();
15 ReadLocalMaps();
16 InitArrays();
17
18 fSiBTStationSet = BmnSiBTStationSet::Create(period, fSetup);
19 InitNoiseArrays(fSiBTStationSet);
20}
21
23{
24 DeleteNoiseArrays(fSiBTStationSet);
25 for (auto& it : fOuterMap)
26 for (auto& inner : it.second)
27 delete inner.second;
28}
29
30BmnStatus BmnSiBTRaw2Digit::ReadLocalMap(string& FileName, StripMap& Strips)
31{
32 ifstream inFile(FileName.data());
33 if (!inFile.is_open()) {
34 cout << "Error opening map-file (" << FileName << ")!" << endl;
35 return kBMNERROR;
36 }
37 Int_t iStrip(0);
38 Int_t chan(0);
39 while (!inFile.eof()) {
40 inFile >> chan;
41 if (!inFile.good())
42 break;
43 Strips[chan] = iStrip++;
44 // printf("\t\t strip %4d chan %4d\n", Strips[chan], chan);
45 }
46 return kBMNSUCCESS;
47}
48
49BmnStatus BmnSiBTRaw2Digit::ReadLocalMaps()
50{
51 for (auto& map_el : fGlobalMap) {
52 BmnSiBTMapping& info = map_el.second;
53 auto it = fLocalMaps.find(info.channel_name);
54 if (it != fLocalMaps.end()) {
55 info.strips = &(it->second);
56 } else {
57 string map_file_name =
58 string(getenv("VMCWORKDIR")) + "/input/" + string(fDetName.Data()) + "_" + info.channel_name + ".txt";
59 StripMap strips(fNSamples, 0);
60 LOGF(info, "Reading: %s", map_file_name.data());
61 ReadLocalMap(map_file_name, strips);
62 fLocalMaps[info.channel_name] = move(strips);
63 info.strips = &(fLocalMaps[info.channel_name]);
64 }
65 }
66 return kBMNSUCCESS;
67}
68
69BmnStatus BmnSiBTRaw2Digit::ReadMapFile()
70{
71 UInt_t ser = 0;
72 Short_t ch_lo = 0;
73 Int_t strip_shift = 0;
74 Char_t ch_name;
75 Short_t mod = 0;
76 Short_t lay = 0;
77 Short_t station = 0;
78 string dummy;
79
80 vector<string> channel_map;
81 vector<string> spec_thr;
82 Double_t final_thr = 0;
83 Double_t thr_dif = 0;
84 Int_t n_iters = 0;
85 Double_t cmod_cut = 0;
86
87 // Setup options.
88 po::options_description desc("Options");
89 desc.add_options()("CHANNEL_MAP.channels", po::value<vector<string>>(&channel_map)->multitoken(),
90 "ADC channel -> strip map")("SIGNAL_CONFIGURATION.spec_thr",
91 po::value<vector<string>>(&spec_thr)->multitoken(),
92 "ADC channel -> strip map")(
93 "SIGNAL_CONFIGURATION.Threshold", po::value<Double_t>(&final_thr)->default_value(950), "ADC final threshold")(
94 "SIGNAL_CONFIGURATION.ThresholdDif", po::value<Double_t>(&thr_dif)->default_value(50), "ADC final threshold")(
95 "SIGNAL_CONFIGURATION.NIterations", po::value<Int_t>(&n_iters)->default_value(6), "ADC final threshold")(
96 "SIGNAL_CONFIGURATION.CModCut", po::value<Double_t>(&cmod_cut)->default_value(0), "ADC final threshold");
97
98 // Load config file.
99 po::variables_map vm;
100 TString name = TString(getenv("VMCWORKDIR")) + TString("/input/") + fMapFileName;
101 LOG(info) << "Loading SiBT Map: Period " << fPeriod << ", Run " << fRun << " " << name;
102 ifstream config_file(name.Data(), ifstream::in);
103 if (!config_file.is_open()) {
104 LOG(error) << "Error opening map-file (" << name << ")!";
105 return kBMNERROR;
106 }
107 po::store(po::parse_config_file(config_file, desc), vm);
108 config_file.close();
109 po::notify(vm);
110
111 // set thresholds
112 SetThreshold(final_thr, thr_dif, n_iters, cmod_cut);
113
114 // set specific thresholds
115 Int_t ch = 0;
116 Double_t thr = 0;
117 for (auto& str : spec_thr) {
118 istringstream ss(str);
119 ss >> std::hex >> ser >> std::dec >> ch >> thr;
120 fSpecThreshMap.insert(make_pair(make_pair(ser, ch), thr));
121 LOGF(debug, "Serial 0x%08X Channel %2d Threshold %3.3f", ser, ch, thr);
122 }
123
124 // set channel map
125 for (auto& str : channel_map) {
126 istringstream ss(str);
127 ss >> std::hex >> ser >> std::dec >> ch_lo >> strip_shift >> ch_name >> mod >> lay >> station;
128 fGlobalMap.insert(make_pair(make_pair(ser, ch_lo), BmnSiBTMapping{.layer = lay,
129 .serial = ser,
130 .module = mod,
131 .channel = ch_lo,
132 .strip_shift = strip_shift,
133 .channel_name = ch_name,
134 .station = station,
135 .strips = nullptr}));
136 GrabNewSerial(ser);
137 }
138 return kBMNSUCCESS;
139}
140
141BmnStatus BmnSiBTRaw2Digit::FillEvent(TClonesArray* adc, TClonesArray* digits)
142{
143 (this->*PrecalcEventModsImp)(adc);
144#ifdef BUILD_DEBUG
146#else
148#endif
149 ProcessAdc(digits, kFALSE);
150 return kBMNSUCCESS;
151}
152
154{
155 (this->*PrecalcEventModsImp)(adc);
156#ifdef BUILD_DEBUG
158#else
160#endif
161 ProcessAdc(nullptr, kTRUE);
162
163 return kBMNSUCCESS;
164}
165
167{
168 const Int_t kNStripsInBunch = GetNSamples();
169 // const Int_t kNBunches = kNStrips / kNStripsInBunch;
170 Int_t kNThresh = 3;
171 // repeat noisy channels in the physical terms (station/module/layer)
172 for (Int_t iCr = 0; iCr < GetNSerials(); ++iCr) {
173 for (Int_t iCh = 0; iCh < GetNChannels(); ++iCh) {
174 auto it = fGlobalMap.find(make_pair(GetSerials()[iCr], iCh));
175 if (it == fGlobalMap.end())
176 continue;
177
178 // InChanMapSiBT & inner = it->second;
179 // auto innerIt = inner.lower_bound(iCh);
180 // if (innerIt == inner.end())
181 // continue;
182 BmnSiBTMapping& rec = it->second;
183 for (Short_t iSmpl = 0; iSmpl < GetNSamples(); ++iSmpl) {
184 if (GetNoisyChipChannels()[iCr][iCh][iSmpl] == kTRUE) {
185 Int_t iStrip = MapStrip(rec, iSmpl);
186 fNoisyChannels[rec.station][rec.module][rec.layer][iStrip] = kTRUE;
187 }
188 }
189 }
190 }
191 // mark noisy
192 for (Int_t iSt = 0; iSt < fSiBTStationSet->GetNStations(); ++iSt) {
193 auto* st = fSiBTStationSet->GetStation(iSt);
194 for (Int_t iMod = 0; iMod < st->GetNModules(); ++iMod) {
195 auto* mod = st->GetModule(iMod);
196 for (Int_t iLay = 0; iLay < mod->GetNStripLayers(); ++iLay) {
197 TH1F* prof = fSigProf[iSt][iMod][iLay];
198 // printf("SigProf %s_%d_%d_%d\n", fDetName.Data(), iSt, iMod, iLay);
199 auto& lay = mod->GetStripLayer(iLay);
200 Int_t kNBunches = lay.GetNStrips() / kNStripsInBunch;
201 for (Int_t iBunch = 0; iBunch < kNBunches; ++iBunch) {
202 Double_t mean = 0.0;
203 for (Int_t iStrip = 0; iStrip < kNStripsInBunch; ++iStrip) {
204 Int_t strip = iStrip + iBunch * kNStripsInBunch;
205 if (fNoisyChannels[iSt][iMod][iLay][strip] == kTRUE)
206 continue;
207 Double_t curr = prof->GetBinContent(strip + 1);
208 // Double_t next = prof->GetBinContent(strip);
209 mean += curr;
210 }
211 mean /= kNStripsInBunch;
212 for (Int_t iStrip = 0; iStrip < kNStripsInBunch; ++iStrip) {
213 Int_t strip = iStrip + iBunch * kNStripsInBunch;
214 if (fNoisyChannels[iSt][iMod][iLay][strip] == kTRUE)
215 continue;
216 Double_t curr = prof->GetBinContent(strip + 1);
217 // Double_t next = prof->GetBinContent(strip);
218 // if (kNThresh * meanDiff < next - curr)
219 if ((kNThresh * Abs(mean) < Abs(curr - mean)) /* || (kNThresh * meanDiff < -next + curr)*/) {
220 LOGF(debug2, "profile noise on iSt %d iMod %d iLay %d strip %d", iSt, iMod, iLay, strip);
221 fNoisyChannels[iSt][iMod][iLay][strip] = kTRUE;
222 }
223 }
224 }
225 }
226 }
227 }
228 // repeat noisy channels back into the electronics terms
229 for (Int_t iCr = 0; iCr < GetNSerials(); ++iCr) {
230 for (Int_t iCh = 0; iCh < GetNChannels(); ++iCh) {
231 auto it = fGlobalMap.find(make_pair(GetSerials()[iCr], iCh));
232 if (it == fGlobalMap.end())
233 continue;
234 BmnSiBTMapping& rec = it->second;
235 for (Short_t iSmpl = 0; iSmpl < GetNSamples(); ++iSmpl) {
236 Int_t iStrip = MapStrip(rec, iSmpl);
237 if (fNoisyChannels[rec.station][rec.module][rec.layer][iStrip] == kTRUE)
238 GetNoisyChipChannels()[iCr][iCh][iSmpl] = kTRUE;
239 }
240 }
241 }
242 return kBMNSUCCESS;
243}
244
245void BmnSiBTRaw2Digit::ProcessAdc(TClonesArray* digs, Bool_t doFill)
246{
247 for (Int_t iCr = 0; iCr < GetNSerials(); ++iCr) {
248 for (Int_t iCh = 0; iCh < GetNChannels(); ++iCh) {
249 auto it = fGlobalMap.find(make_pair(GetSerials()[iCr], iCh));
250 if (it == fGlobalMap.end())
251 continue;
252 // spec threshold
253 Double_t SpecThr(0);
254 auto itThr = fSpecThreshMap.find(make_pair(GetSerials()[iCr], iCh));
255 if (itThr != fSpecThreshMap.end())
256 SpecThr = itThr->second;
257 BmnSiBTMapping& rec = it->second;
258 Short_t station = rec.station;
259 Short_t module = rec.module;
260 Short_t layer = rec.layer;
261 Double_t cs = fCMode[iCr][iCh] - fSMode[iCr][iCh];
262 for (Short_t iSmpl = 0; iSmpl < GetNSamples(); ++iSmpl) {
263 if ((GetNoisyChipChannels()[iCr][iCh][iSmpl]
264 == kTRUE))
265 LOGF(debug2, "noisy %4d %4d %4d", iCr, iCh, iSmpl);
266 Int_t strip = MapStrip(rec, iSmpl);
267 // printf("%c layer %d iCh %3d iSmpl %2d, iStrip %2d\n", rec.channel_name, rec.layer,
268 // iCh, iSmpl, strip);
269 Double_t sig = fAdc[iCr][iCh][iSmpl] - fPedVal[iCr][iCh][iSmpl] + cs;
270 if (layer == 1)
271 sig = -sig;
272 // Double_t Asig = TMath::Abs(sig);
273 Double_t thr = (SpecThr > 0) ? SpecThr : FinalThr;
274 thr = Max(thr, 3.5 * GetPedestalsRMS()[iCr][iCh][iSmpl]);
275 if ((!GetApplyThreshold()) || (sig > thr)) {
276 // printf("st %d mod %d l %d strip %d ped %2.3f sig %2.3f thr %2.3f PedestalsRMS
277 // %2.3f\n",
278 // station, module, layer, strip, fPedVal[iCr][iCh][iSmpl], sig, thr,
279 // GetPedestalsRMS()[iCr][iCh][iSmpl]);
280 if (doFill) {
281 fSigProf[station][module][layer]->Fill(strip);
282 } else {
283 BmnSiBTDigit* resDig = new ((*digs)[digs->GetEntriesFast()])
284 BmnSiBTDigit(station, module, layer, strip, sig, GetPedestalsRMS()[iCr][iCh][iSmpl]);
285 resDig->SetIsGoodDigit(kTRUE);
286 }
287 }
288 }
289 }
290 }
291}
const float thr
int MapStrip(size_t &iChar, size_t &iCh)
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 GrabNewSerial(UInt_t serial)
const Int_t fNSamples
void InitNoiseArrays(SST &ss)
Bool_t *** GetNoisyChipChannels()
void DeleteNoiseArrays(SST &ss)
void(BmnAdcProcessor::* PrecalcEventModsImp)(TClonesArray *adc)
BmnStatus FillNoisyChannels()
BmnSiBTRaw2Digit(Int_t period, Int_t run, TString MapFileName, BmnSetup bmnSetup=kBMNSETUP)
BmnStatus FillEvent(TClonesArray *adc, TClonesArray *sts)
BmnStatus FillProfiles(TClonesArray *adc)
static unique_ptr< BmnSiBTStationSet > Create(Int_t period, Int_t stp=0)
void SetIsGoodDigit(Bool_t tmp)
vector< Int_t > StripMap
#define ADC_SIBT_N_SAMPLES
#define ADC_N_CHANNELS
-clang-format
name
Definition setup.py:7