ATLAS Offline Software
Loading...
Searching...
No Matches
LCE_CellList.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
5//Dear emacs, this is -*-c++-*-
6
7
8
14#include "LArCafJobs/CellInfo.h"
17#include "LArCafJobs/CaloId.h"
18
20
21#include "TROOT.h"
22#include "TApplication.h"
23#include "TSystem.h"
24#include <fstream>
25#include <vector>
26#include <string>
27#include <set>
28#include <iostream>
29
30#if ROOT_VERSION_CODE < ROOT_VERSION(6,0,0)
31#include "Cintex/Cintex.h"
32#endif
33
34using namespace LArSamples;
35using namespace std;
36
38
39public:
40
41 struct thrCounter_t {
42 public:
43 unsigned onlId;
44 unsigned nSeen=0;
45 unsigned nAboveSigNoise=0;
46 unsigned nAboveAbsE=0;
47 unsigned nAboveQ=0;
48 unsigned bc_status=0;
49 double Esum=0.0;
50
51 short caloid=0;
52 short FT=0;
53 short slot=0;
54 short channel=0;
55 short layer=0;
56
57 //Flags:
58 bool EventEnergyCut=false;
59 bool MeanCellHitCut=false;
60 std::set<unsigned> LBs;
61
62 thrCounter_t(const unsigned id) :
63 onlId(id) {};
64
65 };
66
67
68 void setFlaggingThresholds(const float hitsPerLB, const unsigned upperCountThr, const double lowerEThr, const unsigned lowerCountThr, const double upperEThr);
69 void setListingThresholds(const unsigned minNSeen, const unsigned minAboveSigNoise);
70
71
72 void readDefectLBList(const char* LBfile);
73
74 inline bool checkBadLBList(const unsigned lumiBlock) const {
75 return (m_badLBs.find(lumiBlock)!=m_badLBs.end());
76}
77
78 std::vector<LCE_CellList::thrCounter_t> buildList(const char* inputfile, const float nSigma, const float Ethr, const float QThr, unsigned& nLBsSeen) const;
79
80 void writeList(const char* filename, const std::vector<LCE_CellList::thrCounter_t>& celllist) const;
81
82 void addFlags(std::vector<LCE_CellList::thrCounter_t>& celllist, const unsigned nLBsSeen) const;
83
84 bool applySelection(const LCE_CellList::thrCounter_t& counter) const;
85
86 std::string partitionName(const short caloId, const short slot) const;
87
88 void printThresholds() const;
89
90private:
91 std::set<unsigned> m_badLBs;
92
93 //Flagging thresholds
97 double m_LowerCellEnergyThreshold=1000.0; //1 GeV
98 double m_UpperCellEnergyThreshold=50000.0; //50GeV
99
100 //Listing thresholds
101 unsigned m_minNSeen=10;
103
104};
105
106
107void LCE_CellList::setFlaggingThresholds(const float hitsPerLB, const unsigned upperCountThr, const double lowerEThr, const unsigned lowerCountThr, const double upperEThr){
108 m_hitCountPerLBThreshold=hitsPerLB;
109 m_UpperCountThreshold = upperCountThr;
110 m_LowerCountThreshold = lowerCountThr;
111 m_LowerCellEnergyThreshold = lowerEThr;
112 m_UpperCellEnergyThreshold = upperEThr;
113 }
114
115
116void LCE_CellList::setListingThresholds(const unsigned minNSeen, const unsigned minAboveSigNoise) {
117 m_minNSeen=minNSeen;
118 m_minAboveSigNoise=minAboveSigNoise;
119}
120
121
123 printf ("Listing Thresholds:\n");
124 printf ("\tMin number of appearences in LCE ntuple: %u\n",m_minNSeen);
125 printf ("\tMin mumber of events with E> n Sigma Noise: %u\n", m_minAboveSigNoise);
126 printf ("Flagging Thresholds:\n");
127 printf ("\tMin number of events > sigNoise: %.3f * nLumiBlocks\n",m_hitCountPerLBThreshold);
128 printf ("\tUpper count threshold for event energy cut %u\n", m_UpperCountThreshold);
129 printf ("\tLower count threshold for event energy cut %u\n", m_LowerCountThreshold);
130 printf ("\tUpper mean energy threshold for event energy cut %.2f MeV\n", m_UpperCellEnergyThreshold);
131 printf ("\tLower mean energy threshold for event energy cut %.2f MeV\n",m_LowerCellEnergyThreshold);
132}
133
134
135
136void LCE_CellList::readDefectLBList(const char* LBfile) {
137
138 if (!m_badLBs.empty())
139 printf("Appending to already-existing list of bad lumi-blocks of size %zu\n",m_badLBs.size());
140
141
142 std::ifstream infile(LBfile);
143 std::string line;
144
145 // assume single-line format with coma-separated LBs (from python)
146 std::getline(infile,line,'\n');
147 if (line.empty()) {
148 printf("No bad LBs found in file %s\n" ,(const char*)LBfile);
149 return;
150 }
151
152 for (size_t pos=0;pos!=std::string::npos;pos=line.find(',',pos)) {
153 if (pos) pos++; //Jump over comma if not the first iteration
154 //std::cout << "Parsing " << line.c_str()+pos << std::endl;
155 unsigned LB=std::atoi(line.c_str()+pos);
156 m_badLBs.insert(LB);
157 }
158
159 printf("Number of bad lumi-blocks: %d\n",(int)m_badLBs.size());
160
161 return;
162}
163
164
165std::vector< LCE_CellList::thrCounter_t> LCE_CellList::buildList(const char* inputfile, const float nSigma, const float Ethr, const float Qthr, unsigned& nLBsSeen) const {
166
167 std::vector<thrCounter_t> retvec;
168
169 std::set<unsigned> nLBsSeenSet;
170
172 const unsigned nchannels = tuple->nChannels();
173
174 retvec.reserve(nchannels);
175
176 for (unsigned ichan=0;ichan<nchannels;++ichan) {
177 const LArSamples::History* hist = tuple->cellHistory(ichan);
178 if (!hist) continue;
179 const LArSamples::CellInfo* cellInfo = hist->cellInfo();
180 const int nEvents=hist->nData();
181
182 thrCounter_t cnt(cellInfo->onlid());
183 cnt.FT=cellInfo->feedThrough();
184 cnt.slot=cellInfo->slot();
185 cnt.channel=cellInfo->channel();
186 cnt.caloid=cellInfo->calo();
187 cnt.layer=cellInfo->layer();
188
189 for (int iEvent=0;iEvent<nEvents;++iEvent) {
190 const LArSamples::Data* data = hist->data(iEvent);
191 const LArSamples::EventData* Evdata = data->eventData();
192 if(!Evdata) continue;
193 unsigned lumiBlock = Evdata->lumiBlock();
194 if (checkBadLBList(lumiBlock)) continue; //skip bad LBs
195
196
197 const double energy= data->energy();
198 const double noise = data->noise();
199 const double quality = data->quality();
200
201 cnt.nSeen++;
202 if (energy > nSigma * noise) cnt.nAboveSigNoise++;
203 if (energy > Ethr) cnt.nAboveAbsE++;
204 if (quality > Qthr) cnt.nAboveQ++;
205 if (!cnt.bc_status) cnt.bc_status=data->status();
206 cnt.Esum+=energy;
207 cnt.LBs.insert(lumiBlock);
208 nLBsSeenSet.insert(lumiBlock);
209 }//End loop over events
210
211 if (cnt.nSeen>0) retvec.emplace_back(cnt);
212 }//end loop over channels
213
214 nLBsSeen=nLBsSeenSet.size();
215 std::cout << "Evaluated a total of " << nLBsSeen << "LBs" << std::endl;
216
217 delete tuple;
218 return retvec;
219
220}
221
222void LCE_CellList::addFlags(std::vector<LCE_CellList::thrCounter_t>& celllist, const unsigned nLBsSeen) const {
223 for (thrCounter_t& counter : celllist) {
224 counter.MeanCellHitCut=(counter.nAboveAbsE> m_hitCountPerLBThreshold*nLBsSeen);
225 counter.EventEnergyCut= ((counter.nAboveSigNoise > m_UpperCountThreshold && (counter.Esum/counter.nAboveSigNoise) > m_LowerCellEnergyThreshold) ||
226 (counter.nAboveSigNoise > m_LowerCountThreshold && (counter.Esum/counter.nAboveSigNoise > m_UpperCellEnergyThreshold)));
227
228 }
229
230}
231
232
233
235 return counter.nSeen > m_minNSeen && counter.nAboveSigNoise > m_minAboveSigNoise;
236}
237
238
239void LCE_CellList::writeList(const char* textfilename, const std::vector<LCE_CellList::thrCounter_t>& cellList) const {
240 FILE* pFile = fopen (textfilename , "w");
241 if (!pFile) {
242 std::cerr << "Cannot open output file " << textfilename << "\n";
243 exit(1);
244 }
245 // 0 1 2 3 4 5 6 7 8 9 10
246 fprintf(pFile,"onlid // partition // FT // Slot // channel // nAboveSigNoise // nAboveAbsE // MeanE [GeV] // fracQ4k // nLBs // Algoflag\n");
247
248 for (const auto& cnt : cellList) {
249 if (applySelection(cnt)) {
250 unsigned flag=0;
251 if (cnt.EventEnergyCut) {
252 flag=1;
253 if (cnt.MeanCellHitCut) flag=2;
254 }
255 //Expected format (by WDE):
256 //online id Partition FT Slot Chan nEvt>10Sig nEvt>1GeV, MeanE, fracQ4k, nLBs,
257 // 0 1 2 3 4 5 6 7 8 9 10
258 fprintf(pFile,"0x%8.8x \t %7s \t %i \t %i \t %i \t %i \t %i \t %.3f \t %.3f \t %u \t %u",
259 cnt.onlId, partitionName(cnt.caloid,cnt.layer).c_str(),
260 cnt.FT, cnt.slot, cnt.channel, cnt.nAboveSigNoise, cnt.nAboveAbsE,
261 cnt.Esum/(1000.0*cnt.nSeen), (float)cnt.nAboveQ/cnt.nSeen,
262 (unsigned)cnt.LBs.size(), flag );
263
264
265 fprintf(pFile,"\n");
266 }
267 }
268 fclose(pFile);
269 return;
270}
271
272
273std::string LCE_CellList::partitionName(const short caloId, const short layer) const {
274
275 std::string name;
276 std::string slayer=std::to_string(layer);
277 std::string side;
278
279 switch (caloId) {
280 case EMB_C:
281 name="EMB";
282 side="C";
283 if (layer==0) slayer="P";
284 break;
285 case EMB_A:
286 name="EMB";
287 side="A";
288 if (layer==0) slayer="P";
289 break;
290
291 case EMEC_INNER_C:
292 case EMEC_OUTER_C:
293 name="EMEC";
294 side="C";
295 if (layer==0) slayer="P";
296 break;
297
298 case EMEC_INNER_A:
299 case EMEC_OUTER_A:
300 name="EMEC";
301 side="A";
302 if (layer==0) slayer="P";
303 break;
304
305 case HEC_A:
306 name="HEC";
307 side="A";
308 break;
309
310 case HEC_C:
311 name="HEC";
312 side="C";
313 break;
314
315 case FCAL_A:
316 name="FCAL";
317 side="A";
318 break;
319
320 case FCAL_C:
321 name="FCAL";
322 side="C";
323 break;
324
325 default:
326 name="UNKNOWN";
327 slayer="";
328 }
329
330 return name+slayer+side;
331}
332
333 /* FCAL_C = -5, HEC_C = -4, EMEC_INNER_C = -3, EMEC_OUTER_C = -2, EMB_C = -1,
334 UNKNOWN_CALO = 0,
335 EMB_A = 1, EMEC_OUTER_A = 2, EMEC_INNER_A = 3, HEC_A = 4, FCAL_A = 5,*/
336
337
338
339int main ATLAS_NOT_THREAD_SAFE (int argc, char** argv) {
340
341 if (argc<3 || (argc>1 && (!strcmp(argv[1],"-h") || !strcmp(argv[1],"--help")))) {
342 std::cout << "Syntax:" << std::endl;
343 std::cout << "RunLCE.exe inFile outFile <defectLBfile>" << std::endl;
344 return -1;
345 }
346
347 const char* inputFile=argv[1];
348 const char* outputFile=argv[2];
349
350 char* defectsLBFileName=nullptr;
351 if (argc>3)
352 defectsLBFileName=argv[3];
353
354 TROOT root ("root", "root");
355#if ROOT_VERSION_CODE < ROOT_VERSION(6,0,0)
356 ROOT::Cintex::Cintex::Enable();
357 #endif
358
359 gSystem->Load("libLArCafJobsDict.so");
360 gSystem->Load("libLArSamplesMonDict.so");
361
362
363 if (gSystem->AccessPathName(inputFile)) {
364 printf("Cannot access file %s.\n",inputFile);
365 return -1;
366 }
367
368
369 LCE_CellList lceList;
370
371 if (defectsLBFileName) {
372 lceList.readDefectLBList(defectsLBFileName);
373 }
374
375 const float Ethr=1000.0; //1GeV
376 const float nSigma=10.0;
377 const float Qthr=4000;
378 unsigned nLBsSeen=0;
379
380 printf("Thresholds:\n");
381 printf("\tAbsolute Energy: %.2f MeV\n",Ethr);
382 printf("\tSigma Noise: %.2f\n",nSigma);
383 printf("\tQuality Factor: %.2f\n\n",Qthr);
384
385 lceList.printThresholds();
386
387 std::vector<LCE_CellList::thrCounter_t> celllist=lceList.buildList(inputFile, nSigma, Ethr, Qthr, nLBsSeen);
388 printf("Total number of cells read from LCE ntuple: %zu\n",celllist.size());
389 lceList.addFlags(celllist, nLBsSeen);
390 lceList.writeList(outputFile,celllist);
391
392 return 0;
393}
int main(int, char **)
Main class for all the CppUnit test classes.
char data[hepevt_bytes_allocation_ATLAS]
Definition HepEvt.cxx:11
int main ATLAS_NOT_THREAD_SAFE(int argc, char **argv)
Define macros for attributes used to control the static checker.
#define ATLAS_NOT_THREAD_SAFE
getNoisyStrip() Find noisy strips from hitmaps and write out into xml/db formats
virtual unsigned int nChannels() const
Definition AbsLArCells.h:34
short slot() const
Definition CellInfo.h:68
short layer() const
Definition CellInfo.h:53
short feedThrough() const
Definition CellInfo.h:65
ULong64_t onlid() const
Definition CellInfo.h:90
CaloId calo() const
Definition CellInfo.h:50
short channel() const
Definition CellInfo.h:76
static Interface * open(const TString &fileName)
Definition Interface.cxx:35
const History * cellHistory(unsigned int i) const
double m_UpperCellEnergyThreshold
std::vector< LCE_CellList::thrCounter_t > buildList(const char *inputfile, const float nSigma, const float Ethr, const float QThr, unsigned &nLBsSeen) const
std::string partitionName(const short caloId, const short slot) const
unsigned m_minAboveSigNoise
void addFlags(std::vector< LCE_CellList::thrCounter_t > &celllist, const unsigned nLBsSeen) const
void setListingThresholds(const unsigned minNSeen, const unsigned minAboveSigNoise)
unsigned m_minNSeen
void writeList(const char *filename, const std::vector< LCE_CellList::thrCounter_t > &celllist) const
double m_LowerCellEnergyThreshold
std::set< unsigned > m_badLBs
void setFlaggingThresholds(const float hitsPerLB, const unsigned upperCountThr, const double lowerEThr, const unsigned lowerCountThr, const double upperEThr)
bool checkBadLBList(const unsigned lumiBlock) const
void printThresholds() const
unsigned m_LowerCountThreshold
unsigned m_UpperCountThreshold
void readDefectLBList(const char *LBfile)
bool applySelection(const LCE_CellList::thrCounter_t &counter) const
float m_hitCountPerLBThreshold
const int nEvents
@ EMEC_INNER_C
Definition CaloId.h:22
@ EMEC_INNER_A
Definition CaloId.h:24
@ EMEC_OUTER_C
Definition CaloId.h:22
@ EMEC_OUTER_A
Definition CaloId.h:24
STL namespace.
thrCounter_t(const unsigned id)
std::set< unsigned > LBs