ATLAS Offline Software
JfexSimMonitorAlgorithm.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
5 
6 JfexSimMonitorAlgorithm::JfexSimMonitorAlgorithm( const std::string& name, ISvcLocator* pSvcLocator ) : AthMonitorAlgorithm(name,pSvcLocator) {}
7 
9 
10  ATH_MSG_DEBUG("Initializing JfexSimMonitorAlgorithm algorithm with name: "<< name());
11 
12 
13  ATH_MSG_DEBUG("m_data_key_jJ " << m_data_key_jJ );
14  ATH_MSG_DEBUG("m_data_key_jLJ " << m_data_key_jLJ );
15  ATH_MSG_DEBUG("m_data_key_jTau " << m_data_key_jTau );
16  ATH_MSG_DEBUG("m_data_key_jEM " << m_data_key_jEM );
17  ATH_MSG_DEBUG("m_data_key_jXE " << m_data_key_jXE );
18  ATH_MSG_DEBUG("m_data_key_jTE " << m_data_key_jTE );
19 
20  ATH_MSG_DEBUG("m_simu_key_jJ " << m_simu_key_jJ );
21  ATH_MSG_DEBUG("m_simu_key_jLJ " << m_simu_key_jLJ );
22  ATH_MSG_DEBUG("m_simu_key_jTau " << m_simu_key_jTau );
23  ATH_MSG_DEBUG("m_simu_key_jEM " << m_simu_key_jEM );
24  ATH_MSG_DEBUG("m_simu_key_jXE " << m_simu_key_jXE );
25  ATH_MSG_DEBUG("m_simu_key_jTE " << m_simu_key_jTE );
26 
27 
28  // we initialise all the containers
29  ATH_CHECK( m_data_key_jJ.initialize() );
30  ATH_CHECK( m_data_key_jLJ.initialize() );
31  ATH_CHECK( m_data_key_jTau.initialize() );
32  ATH_CHECK( m_data_key_jEM.initialize() );
33  ATH_CHECK( m_data_key_jXE.initialize() );
34  ATH_CHECK( m_data_key_jTE.initialize() );
35 
36  ATH_CHECK( m_simu_key_jJ.initialize() );
37  ATH_CHECK( m_simu_key_jLJ.initialize() );
38  ATH_CHECK( m_simu_key_jTau.initialize() );
39  ATH_CHECK( m_simu_key_jEM.initialize() );
40  ATH_CHECK( m_simu_key_jXE.initialize() );
41  ATH_CHECK( m_simu_key_jTE.initialize() );
42 
43  ATH_CHECK( m_jFexTowerKey.initialize() );
44 
45  ATH_CHECK( m_bcContKey.initialize() );
46 
47 
48  // TOBs may come from trigger bytestream - renounce from scheduler
55 
56 
58 }
59 
60 StatusCode JfexSimMonitorAlgorithm::fillHistograms( const EventContext& ctx ) const {
61 
62  ATH_MSG_DEBUG("JfexMonitorAlgorithm::fillHistograms");
63 
65  if(!jFexTowerContainer.isValid()) {
66  ATH_MSG_ERROR("No jFex Tower container found in storegate "<< m_jFexTowerKey);
67  return StatusCode::SUCCESS;
68  }
69 
70  std::string inputTower = jFexTowerContainer->empty() ? "EmulatedTowers" : "DataTowers";
71 
72  // mismatches can be caused by recent/imminent OTF maskings, so track timings
73  auto timeSince = Monitored::Scalar<int>("timeSince", -1);
74  auto timeUntil = Monitored::Scalar<int>("timeUntil", -1);
76  if(larBadChan.isValid()) {
77  timeSince = ctx.eventID().time_stamp() - larBadChan.getRange().start().time_stamp();
78  timeUntil = larBadChan.getRange().stop().time_stamp() - ctx.eventID().time_stamp();
79  }
80  auto EventType = Monitored::Scalar<std::string>("EventType","DataTowers");
81  if(jFexTowerContainer->empty()) {
82  EventType = "EmulatedTowers";
83  }
84 
85 
86  //maximum number of TOBs (not xTOBs) that fit on the realtime path in hardware
87  static constexpr int jJmaxTobs = 7;
88  static constexpr int jTAUmaxTobs = 6;
89  static constexpr int jEMmaxTobs = 5;
90  bool simReady = !jFexTowerContainer->empty();
91  compareRoI("jJ",EventType,m_data_key_jJ, m_simu_key_jJ,ctx,simReady,jJmaxTobs);
92  //compareRoI("jLJ",EventType,m_data_key_jLJ, m_simu_key_jLJ,ctx,false); - commented out b.c. jFEX doesn't produce Large jets now
93  compareRoI("jTAU",EventType,m_data_key_jTau, m_simu_key_jTau,ctx,simReady,jTAUmaxTobs);
94  compareRoI("jEM",EventType,m_data_key_jEM, m_simu_key_jEM,ctx,false,jEMmaxTobs);
97 
98 
99  return StatusCode::SUCCESS;
100 }
101 
102 template<typename T> uint16_t tobEt(const T* tob) { return tob->tobEt(); }
103 template<> uint16_t tobEt(const xAOD::jFexMETRoI*) { return 0; }
104 template<> uint16_t tobEt(const xAOD::jFexSumETRoI*) { return 0; }
105 
106 template <typename T> bool JfexSimMonitorAlgorithm::compareRoI(const std::string& label, const std::string& evenType,
107  const SG::ReadHandleKey<T>& tobsDataKey,
108  const SG::ReadHandleKey<T>& tobsSimKey,
109  const EventContext& ctx, bool simReadyFlag, size_t maxTobs) const {
110  SG::ReadHandle<T> tobsDataCont{tobsDataKey, ctx};
111  if(!tobsDataCont.isValid()) {
112  return false;
113  }
114  SG::ReadHandle<T> tobsSimCont{tobsSimKey, ctx};
115  if(!tobsSimCont.isValid()) {
116  return false;
117  }
118 
119  bool mismatches = false;
120 
121  auto eventType = Monitored::Scalar<std::string>("EventType",evenType);
122  auto Signature = Monitored::Scalar<std::string>("Signature",label);
123  auto tobMismatched = Monitored::Scalar<double>("tobMismatched",0);
124  auto simReady = Monitored::Scalar<bool>("SimulationReady",simReadyFlag);
125  auto IsDataTowers = Monitored::Scalar<bool>("IsDataTowers",evenType=="DataTowers");
126  auto IsEmulatedTowers = Monitored::Scalar<bool>("IsEmulatedTowers",!IsDataTowers);
127 
128  // saturation bits currently not properly simulated. But because they aren't used anywhere downstream
129  // in the trigger, we will allow mismatches in these bits.
130  // The saturation bit is the lowest bit on all TOBs except jTE where it is also the highest bit (2 bits):
131  auto mask = (label=="jTE") ? 0x7FFFFFFE : 0xFFFFFFFE;
132 
133  // if have a max tobs count, need to see if we reached max count in any fpga in any jfex module
134  // if we did, then we allow mismatches of any tob that has the min et
135  std::map<std::pair<uint8_t,uint8_t>,std::multiset<uint16_t>> tobEts_byFpga;
136 
137  unsigned zeroTobs1 = 0;
138  unsigned zeroTobs2 = 0;
139  for(const auto tob1 : *tobsDataCont) {
140  bool isMatched = false;
141  auto word1 = tob1->tobWord();
142  auto jfex1 = tob1->jFexNumber();
143  auto fpga1 = tob1->fpgaNumber();
144 
145  for (const auto tob2 : *tobsSimCont) {
146  if(word1==0 || ((word1&mask) == (tob2->tobWord()&mask) && jfex1 == tob2->jFexNumber() && fpga1 == tob2->fpgaNumber())) { // do not flag as mismatch if the TOB word is zero, it might simply be (zero) suppressed in the other container!
147  isMatched = true;
148  break;
149  }
150  }
151  if(!isMatched) {
152  // if this signature has a max number of TOBs (in an FPGA),
153  // possible the mismatch is an ambiguity in the lowest ET TOB
154  // so treat as a match if this data TOB has same ET as the lowest ET sim TOB in the same FPGA
155  if(maxTobs>0) {
156  // first populate the fpga->tobs map with simulation tobs if it hasn't already been filled
157  if(tobEts_byFpga.empty()) {
158  for (const auto tob: *tobsSimCont) {
159  // use of multiset ensures all the TOBs are automatically ordered by ET
160  // the first tob in the multiset will be the lowest ET tob.
161  tobEts_byFpga[std::pair(tob->jFexNumber(), tob->fpgaNumber())].insert(tobEt(tob));
162  }
163  }
164  // now check if the FPGA that produced the data TOB reached its max number of TOBs and
165  // the lowest ET TOB has the same ET
166  if(auto itr = tobEts_byFpga.find(std::pair(jfex1,fpga1)); itr != tobEts_byFpga.end()
167  && itr->second.size() == maxTobs // number of TOBs in the FPGA reached the maximum
168  && (*itr->second.begin())==tobEt(tob1) // tob has same ET as lowest ET TOB (first tob in the multiset)
169  ) {
170  // possible ambiguity ... treat as a match
171  isMatched = true;
172  }
173  }
174  if(!isMatched) {
175  mismatches = true;
176  }
177  }
178  if (word1 == 0) {
179  zeroTobs1++;
180  }
181  }
182 
183  for (const auto tob2: *tobsSimCont) {
184  if (tob2->tobWord() == 0) {
185  zeroTobs2++;
186  }
187  }
188 
189  // check for extra non-zero sim tobs (compared to number of non-zero data tobs)
190  if(tobsSimCont.isValid() && (tobsDataCont->size() - zeroTobs1) < (tobsSimCont->size() - zeroTobs2) ) {
191  mismatches=true;
192  }
193 
194  auto lbn = Monitored::Scalar<ULong64_t>("LBN",GetEventInfo(ctx)->lumiBlock());
195  if(mismatches) {
196  // fill the debugging tree with all the words for this signature
197  auto lbnString = Monitored::Scalar<std::string>("LBNString",std::to_string(GetEventInfo(ctx)->lumiBlock()));
198  auto evtNumber = Monitored::Scalar<ULong64_t>("EventNumber",GetEventInfo(ctx)->eventNumber());
199  {
200  std::scoped_lock lock(m_firstEventsMutex);
201  auto itr = m_firstEvents.find(lbn);
202  if(itr==m_firstEvents.end()) {
203  m_firstEvents[lbn] = std::to_string(lbn)+":"+std::to_string(evtNumber);
204  itr = m_firstEvents.find(lbn);
205  }
206  lbnString = itr->second;
207  }
208  std::vector<float> detas{};std::vector<float> setas{};
209  std::vector<float> dphis{};std::vector<float> sphis{};
210  std::vector<unsigned int> dword0s{};std::vector<unsigned int> sword0s{};
211  auto dtobEtas = Monitored::Collection("dataEtas", detas);
212  auto dtobPhis = Monitored::Collection("dataPhis", dphis);
213  auto dtobWord0s = Monitored::Collection("dataWord0s", dword0s);
214  auto stobEtas = Monitored::Collection("simEtas", setas);
215  auto stobPhis = Monitored::Collection("simPhis", sphis);
216  auto stobWord0s = Monitored::Collection("simWord0s", sword0s);
217  fillVectors(tobsDataKey,ctx,detas,dphis,dword0s);
218  fillVectors(tobsSimKey,ctx,setas,sphis,sword0s);
219  if(msgLvl(MSG::DEBUG)) {
220  std::cout << "LBN: " << std::string(lbnString) << " EventNumber: " << ULong64_t(evtNumber) << " signature: " << label << std::endl;
221  std::cout << " data : " << std::hex;
222  for (const auto w: dword0s) std::cout << w << " ";
223  std::cout << std::endl << " sim : ";
224  for (const auto w: sword0s) std::cout << w << " ";
225  std::cout << std::endl << std::dec;
226  }
227  tobMismatched=100;
228  fill("mismatches",tobMismatched,lbn,lbnString,evtNumber,dtobEtas,dtobPhis,dtobWord0s,stobEtas,stobPhis,stobWord0s,Signature,eventType,IsDataTowers,IsEmulatedTowers,simReady,eventType);
229  fill("mismatches_count",lbn,Signature,simReady,eventType);
230  } else {
231  tobMismatched=0;
232  fill("mismatches",lbn,Signature,tobMismatched,simReady,eventType);
233  }
234 
235  return !mismatches;
236 
237 }
238 
239 template <> void JfexSimMonitorAlgorithm::fillVectors(const SG::ReadHandleKey<xAOD::jFexMETRoIContainer>& key, const EventContext& ctx, std::vector<float>& etas, std::vector<float>& phis, std::vector<unsigned int>& word0s) const {
240  etas.clear();phis.clear();word0s.clear();
242  if(tobs.isValid()) {
243  etas.reserve(tobs->size());
244  phis.reserve(tobs->size());
245  word0s.reserve(tobs->size());
246  std::vector<SortableTob> sortedTobs;
247  sortedTobs.reserve(tobs->size());
248  for(const xAOD::jFexMETRoI* tob : *tobs) {
249  sortedTobs.emplace_back(SortableTob{tob->tobWord(),0.,0.});
250  }
251  std::sort(sortedTobs.begin(),sortedTobs.end(),[](const SortableTob& lhs, const SortableTob& rhs) { return lhs.word0<rhs.word0; });
252  for(const auto& tob : sortedTobs) {
253  etas.push_back(tob.eta);
254  phis.push_back(tob.phi);
255  word0s.push_back(tob.word0);
256  }
257  }
258 }
259 template <> void JfexSimMonitorAlgorithm::fillVectors(const SG::ReadHandleKey<xAOD::jFexSumETRoIContainer>& key, const EventContext& ctx, std::vector<float>& etas, std::vector<float>& phis, std::vector<unsigned int>& word0s) const {
260  etas.clear();phis.clear();word0s.clear();
262  if(tobs.isValid()) {
263  etas.reserve(tobs->size());
264  phis.reserve(tobs->size());
265  word0s.reserve(tobs->size());
266  std::vector<SortableTob> sortedTobs;
267  sortedTobs.reserve(tobs->size());
268  for(const auto tob : *tobs) {
269  sortedTobs.emplace_back(SortableTob{tob->tobWord(),0.,0.});
270  }
271  std::sort(sortedTobs.begin(),sortedTobs.end(),[](const SortableTob& lhs, const SortableTob& rhs) { return lhs.word0<rhs.word0; });
272  for(const auto& tob : sortedTobs) {
273  etas.push_back(tob.eta);
274  phis.push_back(tob.phi);
275  word0s.push_back(tob.word0);
276  }
277  }
278 }
JfexSimMonitorAlgorithm::m_simu_key_jLJ
SG::ReadHandleKey< xAOD::jFexLRJetRoIContainer > m_simu_key_jLJ
Definition: JfexSimMonitorAlgorithm.h:44
JfexSimMonitorAlgorithm::m_simu_key_jTE
SG::ReadHandleKey< xAOD::jFexSumETRoIContainer > m_simu_key_jTE
Definition: JfexSimMonitorAlgorithm.h:48
JfexSimMonitorAlgorithm::m_data_key_jJ
SG::ReadHandleKey< xAOD::jFexSRJetRoIContainer > m_data_key_jJ
Definition: JfexSimMonitorAlgorithm.h:35
xAOD::word1
word1
Definition: eFexEMRoI_v1.cxx:87
JfexSimMonitorAlgorithm::m_jFexTowerKey
SG::ReadHandleKey< xAOD::jFexTowerContainer > m_jFexTowerKey
Definition: JfexSimMonitorAlgorithm.h:32
SG::ReadCondHandle
Definition: ReadCondHandle.h:44
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
PlotCalibFromCool.label
label
Definition: PlotCalibFromCool.py:78
EventType
This class represents the "type of event" where the type is given by one or more "characteristics".
Definition: EventType.h:92
AthCommonDataStore< AthCommonMsg< Gaudi::Algorithm > >::renounce
std::enable_if_t< std::is_void_v< std::result_of_t< decltype(&T::renounce)(T)> > &&!std::is_base_of_v< SG::VarHandleKeyArray, T > &&std::is_base_of_v< Gaudi::DataHandle, T >, void > renounce(T &h)
Definition: AthCommonDataStore.h:380
JfexSimMonitorAlgorithm::fillHistograms
virtual StatusCode fillHistograms(const EventContext &ctx) const override
adds event to the monitoring histograms
Definition: JfexSimMonitorAlgorithm.cxx:60
AthCommonMsg< Gaudi::Algorithm >::msgLvl
bool msgLvl(const MSG::Level lvl) const
Definition: AthCommonMsg.h:30
InDetSecVtxTruthMatchUtils::isMatched
bool isMatched(int matchInfo)
Definition: InDetSecVtxTruthMatchTool.h:48
xAOD::jFexTowerContainer
jFexTowerContainer_v1 jFexTowerContainer
Define the latest version of the TriggerTower container.
Definition: jFexTowerContainer.h:14
JfexSimMonitorAlgorithm::m_data_key_jLJ
SG::ReadHandleKey< xAOD::jFexLRJetRoIContainer > m_data_key_jLJ
Definition: JfexSimMonitorAlgorithm.h:36
SG::ReadHandleKey
Property holding a SG store/key/clid from which a ReadHandle is made.
Definition: StoreGate/StoreGate/ReadHandleKey.h:39
tobEt
uint16_t tobEt(const T *tob)
Definition: JfexSimMonitorAlgorithm.cxx:102
JfexSimMonitorAlgorithm::m_simu_key_jXE
SG::ReadHandleKey< xAOD::jFexMETRoIContainer > m_simu_key_jXE
Definition: JfexSimMonitorAlgorithm.h:47
JfexSimMonitorAlgorithm::m_simu_key_jEM
SG::ReadHandleKey< xAOD::jFexFwdElRoIContainer > m_simu_key_jEM
Definition: JfexSimMonitorAlgorithm.h:46
python.utils.AtlRunQueryLookup.mask
string mask
Definition: AtlRunQueryLookup.py:460
Monitored::Collection
ValuesCollection< T > Collection(std::string name, const T &collection)
Declare a monitored (double-convertible) collection.
Definition: MonitoredCollection.h:38
xAOD::jFexMETRoI_v1
Class describing properties of a LVL1 jFEX global Trigger Object (TOB) in the xAOD format.
Definition: jFexMETRoI_v1.h:22
AthMonitorAlgorithm
Base class for Athena Monitoring Algorithms.
Definition: AthMonitorAlgorithm.h:36
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
JfexSimMonitorAlgorithm::JfexSimMonitorAlgorithm
JfexSimMonitorAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Definition: JfexSimMonitorAlgorithm.cxx:6
xAOD::uint16_t
setWord1 uint16_t
Definition: eFexEMRoI_v1.cxx:93
JfexSimMonitorAlgorithm::m_bcContKey
SG::ReadCondHandleKey< LArBadChannelCont > m_bcContKey
Definition: JfexSimMonitorAlgorithm.h:50
JfexSimMonitorAlgorithm.h
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
JfexSimMonitorAlgorithm::m_simu_key_jJ
SG::ReadHandleKey< xAOD::jFexSRJetRoIContainer > m_simu_key_jJ
Definition: JfexSimMonitorAlgorithm.h:43
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
xAOD::eventNumber
eventNumber
Definition: EventInfo_v1.cxx:124
AthMonitorAlgorithm::fill
void fill(const ToolHandle< GenericMonitoringTool > &groupHandle, std::vector< std::reference_wrapper< Monitored::IMonitoredVariable >> &&variables) const
Fills a vector of variables to a group by reference.
AthMonitorAlgorithm::GetEventInfo
SG::ReadHandle< xAOD::EventInfo > GetEventInfo(const EventContext &) const
Return a ReadHandle for an EventInfo object (get run/event numbers, etc.)
Definition: AthMonitorAlgorithm.cxx:107
JfexSimMonitorAlgorithm::m_firstEventsMutex
std::mutex m_firstEventsMutex
Definition: JfexSimMonitorAlgorithm.h:59
JfexSimMonitorAlgorithm::fillVectors
void fillVectors(const SG::ReadHandleKey< T > &key, const EventContext &ctx, std::vector< float > &etas, std::vector< float > &phis, std::vector< unsigned int > &word0s) const
Definition: JfexSimMonitorAlgorithm.h:67
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
xAOD::jFexSumETRoI_v1
Class describing properties of a LVL1 jFEX global Trigger Object (TOB) in the xAOD format.
Definition: jFexSumETRoI_v1.h:22
AthMonitorAlgorithm::initialize
virtual StatusCode initialize() override
initialize
Definition: AthMonitorAlgorithm.cxx:18
std::sort
void sort(typename std::reverse_iterator< DataModel_detail::iterator< DVL > > beg, typename std::reverse_iterator< DataModel_detail::iterator< DVL > > end, const Compare &comp)
Specialization of sort for DataVector/List.
Definition: DVL_algorithms.h:623
JfexSimMonitorAlgorithm::m_data_key_jXE
SG::ReadHandleKey< xAOD::jFexMETRoIContainer > m_data_key_jXE
Definition: JfexSimMonitorAlgorithm.h:39
DEBUG
#define DEBUG
Definition: page_access.h:11
JfexSimMonitorAlgorithm::initialize
virtual StatusCode initialize() override
initialize
Definition: JfexSimMonitorAlgorithm.cxx:8
JfexSimMonitorAlgorithm::m_data_key_jEM
SG::ReadHandleKey< xAOD::jFexFwdElRoIContainer > m_data_key_jEM
Definition: JfexSimMonitorAlgorithm.h:38
JfexSimMonitorAlgorithm::SortableTob
Definition: JfexSimMonitorAlgorithm.h:62
Monitored::Scalar
Declare a monitored scalar variable.
Definition: MonitoredScalar.h:34
JfexSimMonitorAlgorithm::m_simu_key_jTau
SG::ReadHandleKey< xAOD::jFexTauRoIContainer > m_simu_key_jTau
Definition: JfexSimMonitorAlgorithm.h:45
python.IoTestsLib.w
def w
Definition: IoTestsLib.py:200
xAOD::lumiBlock
setTeId lumiBlock
Definition: L2StandAloneMuon_v1.cxx:327
JfexSimMonitorAlgorithm::m_data_key_jTE
SG::ReadHandleKey< xAOD::jFexSumETRoIContainer > m_data_key_jTE
Definition: JfexSimMonitorAlgorithm.h:40
JfexSimMonitorAlgorithm::m_data_key_jTau
SG::ReadHandleKey< xAOD::jFexTauRoIContainer > m_data_key_jTau
Definition: JfexSimMonitorAlgorithm.h:37
JfexSimMonitorAlgorithm::compareRoI
bool compareRoI(const std::string &label, const std::string &evenType, const SG::ReadHandleKey< T > &tobs1Key, const SG::ReadHandleKey< T > &tobs2Key, const EventContext &ctx, bool simReadyFlag=false, size_t maxTobs=0) const
Definition: JfexSimMonitorAlgorithm.cxx:106
EgEfficiencyCorr_testFixedInput.etas
list etas
Definition: EgEfficiencyCorr_testFixedInput.py:9
DataVector::empty
bool empty() const noexcept
Returns true if the collection is empty.
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37