BmnRoot
Loading...
Searching...
No Matches
BmnMscRaw2Digit.cxx
Go to the documentation of this file.
1#include "BmnMscRaw2Digit.h"
2
3#include "ElogRecord.h"
4#include "FairLogger.h"
5#include "UniParser.h"
6#include "UniRun.h"
7#include "UniSearchCondition.h"
8
9BmnMscRaw2Digit::BmnMscRaw2Digit(Int_t period, Int_t run, TString MapFile, TTree* spillTree, TTree* digiSpillTree)
10{
11 fBmnSetup = MapFile.Contains("SRC") ? kSRCSETUP : kBMNSETUP;
12 fPeriodId = period;
13 fRunId = run;
14 SetRawSpillTree(spillTree);
15 SetDigSpillTree(digiSpillTree);
16 ReadChannelMap(MapFile);
17 if ((fBmnSetup == kSRCSETUP) && (fPeriodId == 7)) { // @TODO extend for BM@N
19 fBmnSetup == kSRCSETUP ? "$VMCWORKDIR/database/uni_db/macros/parse_schemes/spill_run7/SRC_Data.txt"
20 : "$VMCWORKDIR/database/uni_db/macros/parse_schemes/spill_run7/summary_corr_v2.txt",
21 fBmnSetup == kSRCSETUP
22 ? "$VMCWORKDIR/database/uni_db/macros/parse_schemes/spill_run7/spill_run7_src_full.xslt"
23 : "$VMCWORKDIR/database/uni_db/macros/parse_schemes/spill_run7/spill_run7_bmn_full.xslt");
24 isValidSpillLog = (status == kBMNSUCCESS);
25 }
26}
27
29{
30 fMapFileName = TString(getenv("VMCWORKDIR")) + TString("/input/") + mappingFile;
31 LOG(info) << "Reading MSC16 channel mapping file " << fMapFileName << "...";
32 fMapFile.open((fMapFileName).Data());
33 if (!fMapFile.is_open()) {
34 fprintf(stderr, "Error opening map-file (%s)!\n", fMapFileName.Data());
35 return kBMNERROR;
36 }
37 TString dummy;
38 TString name;
39 UInt_t ser;
40 UShort_t slot, bt, btnbusy, l0, tp, bc1, bc2, bc3, BC1H, BC1BP, BC1xBC2, BC1nBusy, IntTrig, SRCTrig, TrignBusy;
41
42 fMapFile >> dummy >> dummy >> dummy >> dummy >> dummy >> dummy >> dummy >> dummy >> dummy >> dummy >> dummy >> dummy
43 >> dummy >> dummy >> dummy >> dummy;
44 fMapFile >> dummy;
45 while (!fMapFile.eof()) {
46 fMapFile >> hex >> ser >> dec >> slot >> bt >> btnbusy >> l0 >> tp >> bc1 >> bc2 >> bc3 >> BC1H >> BC1BP
47 >> BC1xBC2 >> BC1nBusy >> IntTrig >> SRCTrig >> TrignBusy;
48 if (!fMapFile.good())
49 break;
50 MscMap record;
51 record.serial = ser;
52 record.slot = slot;
53 record.BT = bt;
54 record.BTnBusy = btnbusy;
55 record.L0 = l0;
56 record.TriggerProtection = tp;
57 record.BC1 = bc1;
58 record.BC2 = bc2;
59 record.BC3 = bc3;
60 record.BC1H = BC1H;
61 record.BC1BP = BC1BP;
62 record.BC1xBC2 = BC1xBC2;
63 record.BC1nBusy = BC1nBusy;
64 record.IntTrig = IntTrig;
65 record.SRCTrig = SRCTrig;
66 record.TrignBusy = TrignBusy;
67 fMap.push_back(record);
68 }
69 fMapFile.close();
70 if (fMap.size() == 0) {
71 fprintf(stderr, "Map file is empty!\n");
72 return kBMNERROR;
73 }
74 return kBMNSUCCESS;
75}
76
77BmnStatus BmnMscRaw2Digit::ParseTxtSpillLog(TString LogName, TString SchemeName)
78{
79 TString beam = "";
80 TString target = "";
81 Int_t log_shift_bmn = 6;
82 Int_t log_shift_src = -3;
83 fLogShift = fBmnSetup == kBMNSETUP ? log_shift_bmn : log_shift_src;
84
85 map<TDatime, vector<Int_t>> temp_spill_map;
86 UniParser parser;
87 vector<structParseValue*> parse_values;
88 vector<structParseSchema> vecElements;
89 int res_code = parser.ParseTxt2Struct(LogName, SchemeName, parse_values, vecElements, fVerbose);
90 if (res_code != 0) {
91 cout << endl << "Error: parser error (" << res_code << ")" << endl;
92 return kBMNERROR;
93 }
94
95 if (parse_values.size() < 1) {
96 cout << endl << "Error: there are no lines to parse" << endl;
97 return kBMNERROR;
98 }
99
100 // TObjArray* pRunArray = NULL;
101 // select only with a given condition, e.g. target particle
102 TObjArray arrayConditions;
103 arrayConditions.SetOwner(kTRUE);
104
105 UniSearchCondition* searchCondition =
107 arrayConditions.Add((TObject*)searchCondition);
108 if (target != "") {
109 searchCondition =
111 target); // one can choose any condition: beam, energy...
112 arrayConditions.Add((TObject*)searchCondition);
113 }
114 if (beam != "") {
115 searchCondition =
117 arrayConditions.Add((TObject*)searchCondition);
118 }
119
120 int sum_size = parse_values[0]->arrValues.size();
121
122 for (size_t ind = 0; ind < parse_values.size(); ind++) {
123 structParseValue* st = parse_values.at(ind);
124 vector<Int_t> vals;
125 for (Int_t iValue = 0; iValue < sum_size; iValue++)
126 vals.push_back(boost::any_cast<Int_t>(st->arrValues[iValue]));
127 st->dtSpillEnd.Set(st->dtSpillEnd.Convert() - fLogShift);
128 temp_spill_map.insert(pair<TDatime, vector<Int_t>>(st->dtSpillEnd, vals));
129 }
130
131 vector<Long64_t> total_columns;
132 for (int i_total = 0; i_total < sum_size; i_total++)
133 total_columns.push_back(0);
134 // get run time
135 UniRun* pRun = UniRun::GetRun(fPeriodId, fRunId);
136 if (pRun == NULL) {
137 cout << endl << "Error: no experimental run was found " << fPeriodId << " : " << fRunId << endl;
138 return kBMNERROR;
139 }
140 dtStart = pRun->GetStartDatetime();
141 TDatime* dateEnd = pRun->GetEndDatetime();
142 if (dateEnd == NULL) {
143 cout << "Error: no end datetime in the database for this run" << endl;
144 delete pRun;
145 return kBMNERROR;
146 }
147 dtEnd = *dateEnd;
148 delete pRun;
149 // fSpillMapIter = spill_map.lower_bound(dtStart);
150 // check for presence in ELOG
151 TObjArray* recs = ElogRecord::GetRecords(fPeriodId, fRunId);
152 if (recs == NULL) {
153 fprintf(stderr, "ELOG Error!\n");
154 return kBMNERROR;
155 } else if (recs->GetEntries() == 0) {
156 fprintf(stderr, "Run %d not found in ELOG!\n", fRunId);
157 return kBMNERROR;
158 }
159 printf("Run %d %s", fRunId, dtStart.AsSQLString());
160 printf(" - %s\n", dtEnd.AsSQLString());
161 spill_map.insert(temp_spill_map.lower_bound(dtStart), temp_spill_map.upper_bound(dtEnd));
162
163 return kBMNSUCCESS;
164}
165
167{
168 if (rh) {
169 rh->SetBT(fBT);
170 rh->SetBTnBusy(fBTnBusy);
171 rh->SetBTAccepted(fBTAccepted);
172 rh->SetAccepted(fAccepted);
173 rh->SetL0(fL0);
174 rh->SetRawMSC(move(fBoardSums));
175 // printf("map len %lu\n",fBoardSums.size());
176 }
177}
178
179BmnStatus BmnMscRaw2Digit::SumEvent(TClonesArray* msc, BmnEventHeader* hdr, BmnSpillHeader* shdr, UInt_t& nPedEvBySpill)
180{
181 // UInt_t iEv = 0; // hdr->GetEventId();
182 // for (Int_t iAdc = 0; iAdc < msc->GetEntriesFast(); ++iAdc) {
183 // BmnMSCDigit<UChar_t>* dig = (BmnMSCDigit<UChar_t>*)msc->At(iAdc);
184 // // fVerbose = 1;
185 // if (fVerbose)
186 // printf("iAdc %d Spill %u last event %u Curent evId %u \n", iAdc, iSpill, dig->GetLastEventId(), iEv);
187 // auto it = fBoardSums.find(dig->GetSerial());
188 // if (it == fBoardSums.end()) {
189 // vector<uint64_t> v(dig->GetValue().begin(), dig->GetValue().end());
190 // fBoardSums.insert(make_pair(dig->GetSerial(), move(v)));
191 // } else {
192 // for (uint8_t i = 0; i < MSC_N_COUNTERS; i++)
193 // it->second[i] += dig->GetValue()[i];
194 // }
195 // }
196 return kBMNSUCCESS;
197}
198
199// for period 7
200
201BmnStatus BmnMscRaw2Digit::SumEvent7(TClonesArray* msc, BmnEventHeader* hdr, BmnSpillHeader* sh, UInt_t& nPedEvBySpill)
202{
203 sh->Clear();
204 if (fRawSpillTree->GetEntriesFast() <= iSpill)
205 return kBMNERROR;
206 BmnTrigInfo& ti = hdr->GetTrigInfo();
207 // if (msc->GetEntriesFast() == 0)
208 // fTempTI = BmnTrigInfo(hdr->GetTrigInfo());
209 UInt_t iEv = hdr->GetEventId();
210 for (Int_t iAdc = 0; iAdc < msc->GetEntriesFast(); ++iAdc) {
211 BmnMSCDigit<UInt_t>* dig = (BmnMSCDigit<UInt_t>*)msc->At(iAdc);
212 if (dig->GetLastEventId() > iEv)
213 return kBMNFINISH;
214 if ((dig->GetLastEventId() < iEv) && (dig->GetLastEventId() > 0)) {
215 fprintf(stderr, "Spill %u last event %u lost! Curent evId %u \n", iSpill, dig->GetLastEventId(), iEv);
216 fRawSpillTree->GetEntry(++iSpill);
217 ++iSpillMap;
218 nPedEvBySpill = 0;
219 return kBMNERROR;
220 }
221 fVerbose = 1;
222 if (fVerbose)
223 printf("Spill %u last event %u Curent evId %u \n", iSpill, dig->GetLastEventId(), iEv);
224 auto& arr = dig->GetValue();
225 UInt_t serial = dig->GetSerial();
226 for (auto& mRec : fMap) {
227 if (mRec.serial == serial) {
228 fBT += arr[mRec.BT];
229 fBTnBusy += arr[mRec.BTnBusy];
230 fProtection += arr[mRec.TriggerProtection];
231 fL0 += arr[mRec.L0];
232 UInt_t AcceptedReal = ti.GetTrigAccepted() - nPedEvBySpill;
233 if ((fBmnSetup == kSRCSETUP) && (fPeriodId == 7) && (isValidSpillLog)) {
234 if (iSpillMap < spill_map.size()) {
235 map<TDatime, vector<Int_t>>::iterator SpillMapIter = spill_map.begin();
236 advance(SpillMapIter, iSpillMap);
237 if (fVerbose)
238 printf(" spill %s\n", SpillMapIter->first.AsSQLString());
239 vector<Int_t> v = SpillMapIter->second;
241 Double_t BT = v[15];
242 Double_t DAQ_Busy = v[21];
243 Double_t DAQ_Trigger = v[22];
244 if (DAQ_Trigger - nPedEvBySpill != 0)
245 fBTAccepted += BT * (DAQ_Busy - nPedEvBySpill) / (DAQ_Trigger - nPedEvBySpill);
246 if (fVerbose)
247 printf("BT %f DAQ_Busy %f DAQ_Trigger %f flux %f\n", BT, DAQ_Busy, DAQ_Trigger,
248 fBTAccepted);
249 }
250 } else { // BM@N setup or U40 is present
251 UInt_t den = AcceptedReal + ti.GetTrigBefo() + ti.GetTrigAfter();
252 if (den > 0)
253 fBTAccepted += arr[mRec.BTnBusy] * AcceptedReal / (Double_t)den;
254 }
255 fAccepted += AcceptedReal;
256 if (fDigSpillTree) {
257 // BM@N MSC16
258 sh->SetBC1(arr[mRec.BC1]);
259 sh->SetBC2(arr[mRec.BC2]);
260 sh->SetBC3(arr[mRec.BC3]);
261 sh->SetBT(arr[mRec.BT]);
262 sh->SetBTnBusy(arr[mRec.BTnBusy]);
263 sh->SetL0(arr[mRec.L0]);
264 sh->SetProt(arr[mRec.TriggerProtection]);
265 // // SRC MSC16
266 // sh->SetBC1H(arr[mRec.BC1H]);
267 // sh->SetBC1BP(arr[mRec.BC1BP]);
268 // sh->SetBC1xBC2(arr[mRec.BC1xBC2]);
269 // sh->SetBC1nBusy(arr[mRec.BC1nBusy]);
270 // sh->SetIntTrig(arr[mRec.IntTrig]);
271 // sh->SetSRCTrig(arr[mRec.SRCTrig]);
272 // sh->SetTrignBusy(arr[mRec.TrignBusy]);
273 // U40
274 sh->SetAccepted(AcceptedReal);
275 sh->SetAfter(ti.GetTrigAfter());
276 sh->SetBefo(ti.GetTrigBefo());
277 sh->SetCand(ti.GetTrigCand());
278 sh->SetAll(ti.GetTrigAll());
279 sh->SetAvail(ti.GetTrigAvail());
280 sh->SetRjct(ti.GetTrigRjct());
281
282 sh->SetLastEventId(iEv);
283 sh->SetPeriodId(hdr->GetPeriodId());
284 sh->SetStopTS(hdr->GetEventTimeTS());
285 fDigSpillTree->Fill();
286 }
287
288 if (fVerbose) {
289 printf("iSpill %4u last EvId %7u pedestals %d\n", iSpill, dig->GetLastEventId(), nPedEvBySpill);
290 hdr->GetEventTimeTS().Print();
291 printf(ANSI_COLOR_BLUE " MSC16:" ANSI_COLOR_RESET "\t"
292 "BC1 %7u, BC2 %7u, BC3 %4u, BeamTrigger %7u, L0 %7u, "
293 "TrigProtection %7u, BT&Busy %7u\n"
294 "\tBC1H %7u, BC1BP %7u, BC1xBC2 %4u, BC1nBusy %7u, IntTrig %7u, "
295 "SRCTrig %7u, TrignBusy %7u\n",
296 arr[mRec.BC1], arr[mRec.BC2], arr[mRec.BC3], arr[mRec.BT], arr[mRec.L0],
297 arr[mRec.TriggerProtection], arr[mRec.BTnBusy], arr[mRec.BC1H], arr[mRec.BC1BP],
298 arr[mRec.BC1xBC2], arr[mRec.BC1nBusy], arr[mRec.IntTrig], arr[mRec.SRCTrig],
299 arr[mRec.TrignBusy]); // BM@N
300 printf(ANSI_COLOR_BLUE
301 " U40VE:" ANSI_COLOR_RESET
302 "\tcand %7u, acc %7u, before %4u, after %6u, rjct %6u, all %7u, avail %7u\n\n",
304 ti.GetTrigRjct(), ti.GetTrigAll(), ti.GetTrigAvail());
305 }
306 ++iSpill;
307 ++iSpillMap;
308 Int_t r = fRawSpillTree->GetEntry(iSpill);
309 // printf("Get entry %u returned %d\n", iSpill, r);
310 if (r <= 0) {
311 // fprintf(stderr, "Spill %u read error!\n", iSpill);
312 return kBMNFINISH;
313 }
314 nPedEvBySpill = 0;
315 break;
316 }
317 }
318 }
319 return kBMNSUCCESS;
320}
__m128 v
Definition P4_F32vec4.h:1
BmnStatus
Definition BmnEnums.h:24
@ kBMNFINISH
Definition BmnEnums.h:28
@ kBMNERROR
Definition BmnEnums.h:26
@ kBMNSUCCESS
Definition BmnEnums.h:25
@ kSRCSETUP
Definition BmnEnums.h:91
@ kBMNSETUP
Definition BmnEnums.h:90
#define ANSI_COLOR_RESET
Definition BmnMath.h:18
#define ANSI_COLOR_BLUE
Definition BmnMath.h:17
UInt_t GetPeriodId()
UInt_t GetEventId()
TTimeStamp GetEventTimeTS()
BmnTrigInfo & GetTrigInfo()
UInt_t GetSerial() const
Definition BmnMSCDigit.h:60
UInt_t GetLastEventId() const
Definition BmnMSCDigit.h:70
std::array< IntType, MSC_N_COUNTERS > & GetValue()
Definition BmnMSCDigit.h:64
BmnStatus SumEvent7(TClonesArray *msc, BmnEventHeader *hdr, BmnSpillHeader *sh, UInt_t &nPedEvBySpill)
BmnStatus ParseTxtSpillLog(TString LogName, TString SchemeName)
BmnStatus SumEvent(TClonesArray *msc, BmnEventHeader *hdr, BmnSpillHeader *sh, UInt_t &nPedEvBySpill)
void FillRunHeader(DigiRunHeader *rh)
void SetRawSpillTree(TTree *tree)
BmnStatus ReadChannelMap(TString mappingFile)
void SetDigSpillTree(TTree *tree)
void SetBC3(UInt_t _v)
void SetBC2(UInt_t _v)
void SetBT(UInt_t _v)
void SetLastEventId(UInt_t event_id)
void SetAccepted(UInt_t _v)
void SetBC1(UInt_t _v)
void SetBTnBusy(UInt_t _v)
void SetAvail(UInt_t _v)
void SetStopTS(TTimeStamp ts)
void SetProt(UInt_t _v)
void SetAll(UInt_t _v)
void SetBefo(UInt_t _v)
void SetL0(UInt_t _v)
void SetCand(UInt_t _v)
void SetRjct(UInt_t _v)
void SetPeriodId(UInt_t period_id)
void SetAfter(UInt_t _v)
UInt_t GetTrigAvail()
Definition BmnTrigInfo.h:75
UInt_t GetTrigAfter()
Definition BmnTrigInfo.h:51
UInt_t GetTrigRjct()
Definition BmnTrigInfo.h:59
UInt_t GetTrigAll()
Definition BmnTrigInfo.h:67
UInt_t GetTrigBefo()
Definition BmnTrigInfo.h:43
UInt_t GetTrigAccepted()
Definition BmnTrigInfo.h:35
UInt_t GetTrigCand()
Definition BmnTrigInfo.h:27
void SetBTnBusy(ULong64_t cntr)
void SetAccepted(ULong64_t cntr)
void SetBTAccepted(Double_t cntr)
void SetL0(ULong64_t cntr)
void SetRawMSC(unordered_map< UInt_t, vector< uint64_t > > v)
void SetBT(ULong64_t cntr)
static TObjArray * GetRecords(int period_number, int run_number, bool findPreviousRun=false)
get array of ElogRecord-s for a given or a previous run from the database
static UniRun * GetRun(int period_number, int run_number)
get run from the database
Definition UniRun.cxx:177
TDatime * GetEndDatetime()
get end datetime of the current run
Definition UniRun.h:105
TDatime GetStartDatetime()
get start datetime of the current run
Definition UniRun.h:103
-clang-format
UShort_t L0
UShort_t BC1nBusy
UShort_t BTnBusy
(BeamTrigger & !Busy)
UInt_t serial
UShort_t BT
BeamTrigger index.
UShort_t TrignBusy
Trigger * !Busy.
UShort_t BC3
UShort_t BC1xBC2
UShort_t TriggerProtection
UShort_t IntTrig
UShort_t SRCTrig
UShort_t BC1BP
BC1 (before protection)
UShort_t BC1
UShort_t slot
UShort_t BC1H
BC1 (high threshold)
UShort_t BC2
TDatime dtSpillEnd
Definition UniParser.h:92
vector< boost::any > arrValues
Definition UniParser.h:93