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