ATLAS Offline Software
EnhancedBiasWeightCompAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 
7 
8 #include <sstream>
9 
10 
11 EnhancedBiasWeightCompAlg::EnhancedBiasWeightCompAlg(const std::string& name, ISvcLocator* svcLoc)
12  : AthReentrantAlgorithm(name, svcLoc) {}
13 
15 
17  if (!m_finalDecisionKey.empty()) {
18  ATH_CHECK( m_finalDecisionKey.initialize() );
19  }
20 
23 
24  ATH_CHECK( m_tdt.retrieve() );
25 
26  return StatusCode::SUCCESS;
27 }
28 
29 
31 
33  ATH_CHECK( hltMenuHandle.isValid() );
34 
35  // Save ids of EB chains - that contain "_eb_"
36  m_EBChainIds = std::vector<HLT::Identifier>();
37  m_EBChainIds = std::vector<HLT::Identifier>();
38  for (const TrigConf::Chain& chain : *hltMenuHandle){
39  if (chain.name() == "HLT_noalg_eb_L1All") continue; // this is a special chain
40  std::vector<std::string> streams = chain.streams();
41  if (std::find(streams.begin(), streams.end(), "EnhancedBias") != streams.end()){
42  auto chainId = HLT::Identifier(chain.name());
43  m_EBChainIds.push_back(chainId);
44  m_EBChainIdToItem[chainId] = parseItems(chain.l1item());
45  }
46  }
47 
48  return StatusCode::SUCCESS;
49 }
50 
52 
53  // Save weights and events mapping to xml
54  if (!m_outputFilename.empty()){
55  std::ofstream outputStream;
56  outputStream.open(m_outputFilename);
57  outputStream << "<?xml version=\"1.0\" encoding=\"us-ascii\"?>" << std::endl;
58  outputStream << "<run>" << std::endl;
59  outputStream << "<weights>" << std::endl;
60 
61  for (size_t i = 0; i < m_ebWeights.size(); ++i){
62  outputStream << "<weight id=\"" << i << "\" value=\"" << m_ebWeights[i].first << "\" unbiased=\"" << m_ebWeights[i].second << "\"/>" << std::endl;
63  }
64 
65  outputStream << "</weights>" << std::endl;
66  outputStream << "<events>" << std::endl;
67 
68  for (const auto& event : m_eventToWeight){
69  outputStream << "<e n=\"" << event.first << "\" w=\"" << event.second << "\"/>" << std::endl;
70  }
71 
72  outputStream << "</events>" << std::endl;
73  outputStream << "</run>" << std::endl;
74  outputStream.close();
75  }
76 
77  return StatusCode::SUCCESS;
78 }
79 
80 StatusCode EnhancedBiasWeightCompAlg::execute(const EventContext& context) const {
81 
82  // Retrieve information about EB chains that could have passed
83  std::vector<EBChainInfo> EBChains = getPassedEBChains();
84 
85  ATH_MSG_DEBUG("Number of eb chains that passed int this event: " << EBChains.size());
86 
87  // None of EB chains passed the algorithm
88  if (EBChains.empty()) {
89  ATH_MSG_DEBUG("Empty event!");
90  return StatusCode::SUCCESS;
91  }
92 
93  // Retrieve L1 and HLT prescales
94  ATH_CHECK( fillTotalPrescaleForChains(context, EBChains) );
95 
96  // Calculate EB weight
97  EBResult result = calculateEBWeight(EBChains);
98 
99  // Save output values: EBWeight and EBUnbiased to xml file
100  // Create filename (run number necessary)
101  if ( context.evt() == 0 ){
102  std::stringstream filename;
103  filename << "EnhancedBiasWeights_" << context.eventID().run_number() << ".xml";
104  m_outputFilename = filename.str();
105  ATH_MSG_INFO("The output file name is " << m_outputFilename);
106  }
107 
108  // Save to containers to be saved to xml
109  auto resultPair = std::pair<double, bool>(result.weight, result.isUnbiased);
110  auto newPair = std::find(m_ebWeights.begin(), m_ebWeights.end(), resultPair);
111  if (newPair == m_ebWeights.end()){
112  newPair = m_ebWeights.push_back(resultPair);
113  ATH_MSG_DEBUG("New weight value: " << result.weight << " with id " << (m_ebWeights.size()-1));
114  }
115  m_eventToWeight[context.eventID().event_number()] = std::distance(m_ebWeights.begin(), newPair);
116 
117  ATH_MSG_DEBUG("EnhacedBias EBWeight: " << result.weight << " EnhacedBias isUnbiased: " << (bool) result.isUnbiased );
118 
119  return StatusCode::SUCCESS;
120 }
121 
122 
123 StatusCode EnhancedBiasWeightCompAlg::fillTotalPrescaleForChains(const EventContext& context, std::vector<EBChainInfo>& EBChains) const {
124 
126  ATH_CHECK( HLTPrescalesSet.isValid() );
127 
129  ATH_CHECK( L1PrescalesSet.isValid() );
130 
131  for (EBChainInfo& chain : EBChains) {
132  if ( not HLTPrescalesSet->prescale(chain.getId()).enabled ){
133  chain.setTotalPrescale(-1);
134  continue;
135  }
136 
137  double HLTPrescale = HLTPrescalesSet->prescale(chain.getId()).prescale;
138  double L1Prescale = 1.0;
139  if (!chain.getIsNoPS()) {
140  for (const std::string& item : m_EBChainIdToItem.at(chain.getId())){
141  L1Prescale *= L1PrescalesSet->prescale(item).prescale;
142  }
143  }
144 
145  chain.setTotalPrescale(HLTPrescale * L1Prescale);
146  }
147 
148  return StatusCode::SUCCESS;
149 }
150 
151 
153 
154  double weight = 1.;
155 
156  for (const EBChainInfo& chain : EBChains){
157  if (chain.getIsDisabled()){
158  ATH_MSG_DEBUG("Chain " << chain.getName() << " disabled");
159  continue;
160  }
161  ATH_MSG_DEBUG("Chain " << chain.getName() << " total prescale " << chain.getTotalPrescale());
162  weight *= 1. - ( 1. / chain.getTotalPrescale() );
163  }
164 
165  weight = (std::fabs(1.0 - weight) < 1e-10) ? 0. : (1. / (1. - weight));
166 
167  // Check if the event was triggered by EB noalg random chain
168  bool isUnbiased = checkIfTriggeredByRandomChain(EBChains);
169 
170  return {double(weight), isUnbiased};
171 }
172 
173 
174 std::vector<EnhancedBiasWeightCompAlg::EBChainInfo> EnhancedBiasWeightCompAlg::getPassedEBChains() const {
175 
176  std::vector<EBChainInfo> passedEBChains;
177 
178  for (const HLT::Identifier& chainId : m_EBChainIds) {
179  std::string chainName = HLT::Identifier(chainId).name();
180 
181  bool ebChainIsSeeded = false;
182  // For chains with HLT seed discrimintaion check if one of the seeds could pass
183  if ((chainName.find("_eb_") != std::string::npos) && (!m_chainToHLTSeed.empty())){
184  for (const std::string& l1Item : m_chainToHLTSeed.value().at(chainName)) {
185  if (m_tdt->isPassedBits(l1Item) & TrigDefs::L1_isPassedBeforePrescale) {
186  ebChainIsSeeded = true;
187  break;
188  }
189  }
190  } else if (chainName.find("L1RD3") != std::string::npos){
191  // For the random items, we need to look at HLT decision
192  ebChainIsSeeded = m_tdt->isPassed(chainName);
193  } else {
194  for (const std::string& l1Item : m_EBChainIdToItem.at(chainId)) {
195  if (m_tdt->isPassedBits(l1Item) & TrigDefs::L1_isPassedBeforePrescale) {
196  ebChainIsSeeded = true;
197  break;
198  }
199  }
200  }
201  if (ebChainIsSeeded) {
202  passedEBChains.push_back(EBChainInfo(HLT::Identifier(chainId)));
203  }
204  }
205 
206  return passedEBChains;
207 }
208 
209 
210 bool EnhancedBiasWeightCompAlg::checkIfTriggeredByRandomChain(const std::vector<EBChainInfo>& EBChains) const {
211 
212  return std::find_if (EBChains.begin(), EBChains.end(),
213  [](const EBChainInfo& chain) -> bool { return chain.getIsRandom(); }) != EBChains.end();
214 }
215 
216 std::vector<std::string> EnhancedBiasWeightCompAlg::parseItems(const std::string& itemStr) {
217 
218  std::vector<std::string> items;
219 
220  std::stringstream itemStream (itemStr);
221  std::string item;
222 
223  while (std::getline(itemStream, item, ',')) {
224  items.push_back(item);
225  }
226 
227  return items;
228 }
TrigConf::HLTPrescalesSet::HLTPrescale::prescale
double prescale
Definition: HLTPrescalesSet.h:24
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
get_generator_info.result
result
Definition: get_generator_info.py:21
runLayerRecalibration.chain
chain
Definition: runLayerRecalibration.py:175
EnhancedBiasWeightCompAlg::getPassedEBChains
std::vector< EBChainInfo > getPassedEBChains() const
Get list of Enhanced Bias chains that cound have passed.
Definition: EnhancedBiasWeightCompAlg.cxx:174
EnhancedBiasWeightCompAlg::parseItems
std::vector< std::string > parseItems(const std::string &itemStr)
Parse list of l1 items.
Definition: EnhancedBiasWeightCompAlg.cxx:216
SG::ReadCondHandle
Definition: ReadCondHandle.h:44
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
EnhancedBiasWeightCompAlg::m_EBChainIds
std::vector< HLT::Identifier > m_EBChainIds
Available EB chains' IDs from HLT Menu.
Definition: EnhancedBiasWeightCompAlg.h:99
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
EnhancedBiasWeightCompAlg::m_EBChainIdToItem
std::map< HLT::Identifier, std::vector< std::string > > m_EBChainIdToItem
L1 items for EB chains from HLT Menu.
Definition: EnhancedBiasWeightCompAlg.h:102
python.outputTest_v2.streams
streams
Definition: outputTest_v2.py:55
EnhancedBiasWeightCompAlg::m_HLTMenuKey
SG::ReadHandleKey< TrigConf::HLTMenu > m_HLTMenuKey
Definition: EnhancedBiasWeightCompAlg.h:74
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
SG::ReadCondHandle::isValid
bool isValid()
Definition: ReadCondHandle.h:206
RunGeantinoStepRecordingITk.outputStream
outputStream
Definition: RunGeantinoStepRecordingITk.py:126
EnhancedBiasWeightCompAlg::start
virtual StatusCode start() override
Definition: EnhancedBiasWeightCompAlg.cxx:30
EnhancedBiasWeightCompAlg::checkIfTriggeredByRandomChain
bool checkIfTriggeredByRandomChain(const std::vector< EBChainInfo > &EBChain) const
Check if any of random chains passed.
Definition: EnhancedBiasWeightCompAlg.cxx:210
EnhancedBiasWeightCompAlg::fillTotalPrescaleForChains
StatusCode fillTotalPrescaleForChains(const EventContext &context, std::vector< EBChainInfo > &EBChains) const
Retrieve total prescales (L1 * HLT) for chains into map.
Definition: EnhancedBiasWeightCompAlg.cxx:123
EnhancedBiasWeightCompAlg::calculateEBWeight
EBResult calculateEBWeight(const std::vector< EBChainInfo > &EBChains) const
Calculate EB result based on total prescales of chains.
Definition: EnhancedBiasWeightCompAlg.cxx:152
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
dqt_zlumi_pandas.weight
int weight
Definition: dqt_zlumi_pandas.py:189
SG::makeHandle
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
Definition: ReadCondHandle.h:270
PyPoolBrowser.item
item
Definition: PyPoolBrowser.py:129
event
POOL::TEvent event(POOL::TEvent::kClassAccess)
lumiFormat.i
int i
Definition: lumiFormat.py:85
TrigConf::HLTPrescalesSet::prescale
const HLTPrescale & prescale(const std::string &chainName) const
HLT prescales by chain names.
Definition: HLTPrescalesSet.cxx:85
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
EnhancedBiasWeightCompAlg::m_chainToHLTSeed
Gaudi::Property< std::map< std::string, std::vector< std::string > > > m_chainToHLTSeed
Definition: EnhancedBiasWeightCompAlg.h:81
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
EnhancedBiasWeightCompAlg::EBResult
Store result of EB weight calculation.
Definition: EnhancedBiasWeightCompAlg.h:69
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
xAOD::double
double
Definition: CompositeParticle_v1.cxx:159
EnhancedBiasWeightCompAlg::m_L1PrescaleSetInputKey
SG::ReadCondHandleKey< TrigConf::L1PrescalesSet > m_L1PrescaleSetInputKey
Definition: EnhancedBiasWeightCompAlg.h:79
HLT::Identifier
Definition: TrigCompositeUtils/TrigCompositeUtils/HLTIdentifier.h:20
EnhancedBiasWeightCompAlg.h
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
TrigConf::L1PrescalesSet::prescale
const L1Prescale & prescale(const std::string &itemName) const
Definition: L1PrescalesSet.cxx:54
EnhancedBiasWeightCompAlg::EBChainInfo
Store necessary information for weight computing for Enhanced Bias chain.
Definition: EnhancedBiasWeightCompAlg.h:44
SG::CondHandleKey::initialize
StatusCode initialize(bool used=true)
TrigJetMonitorAlgorithm.items
items
Definition: TrigJetMonitorAlgorithm.py:79
item
Definition: ItemListSvc.h:43
TrigConf::L1PrescalesSet::L1Prescale::prescale
double prescale
Definition: L1PrescalesSet.h:25
EnhancedBiasWeightCompAlg::m_tdt
PublicToolHandle< Trig::TrigDecisionTool > m_tdt
Definition: EnhancedBiasWeightCompAlg.h:75
EnhancedBiasWeightCompAlg::stop
virtual StatusCode stop() override
Definition: EnhancedBiasWeightCompAlg.cxx:51
EnhancedBiasWeightCompAlg::initialize
virtual StatusCode initialize() override
Definition: EnhancedBiasWeightCompAlg.cxx:14
python.TriggerAPI.TriggerAPISession.chainName
chainName
Definition: TriggerAPISession.py:426
HLT::Identifier::name
std::string name() const
reports human redable name if it is enabled or, empty string
Definition: HLTIdentifier.cxx:14
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:24
TrigConf::HLTPrescalesSet::HLTPrescale::enabled
bool enabled
Definition: HLTPrescalesSet.h:23
EnhancedBiasWeightCompAlg::m_HLTPrescaleSetInputKey
SG::ReadCondHandleKey< TrigConf::HLTPrescalesSet > m_HLTPrescaleSetInputKey
Definition: EnhancedBiasWeightCompAlg.h:78
TrigConf::Chain
HLT chain configuration.
Definition: TrigConfData/TrigConfData/HLTChain.h:18
Amg::distance
float distance(const Amg::Vector3D &p1, const Amg::Vector3D &p2)
calculates the distance between two point in 3D space
Definition: GeoPrimitivesHelpers.h:54
EnhancedBiasWeightCompAlg::m_finalDecisionKey
SG::ReadHandleKey< TrigCompositeUtils::DecisionContainer > m_finalDecisionKey
Definition: EnhancedBiasWeightCompAlg.h:76
EnhancedBiasWeightCompAlg::execute
virtual StatusCode execute(const EventContext &context) const override
Definition: EnhancedBiasWeightCompAlg.cxx:80
EnhancedBiasWeightCompAlg::EnhancedBiasWeightCompAlg
EnhancedBiasWeightCompAlg(const std::string &name, ISvcLocator *svcLoc)
Definition: EnhancedBiasWeightCompAlg.cxx:11