ATLAS Offline Software
Loading...
Searching...
No Matches
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
11EnhancedBiasWeightCompAlg::EnhancedBiasWeightCompAlg(const std::string& name, ISvcLocator* svcLoc)
12 : AthReentrantAlgorithm(name, svcLoc) {}
13
15
16 ATH_CHECK( m_HLTMenuKey.initialize() );
17 if (!m_finalDecisionKey.empty()) {
18 ATH_CHECK( m_finalDecisionKey.initialize() );
19 }
20
21 ATH_CHECK( m_HLTPrescaleSetInputKey.initialize() );
22 ATH_CHECK( m_L1PrescaleSetInputKey.initialize() );
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
80StatusCode 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
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
123StatusCode 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
174std::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
210bool 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
216std::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}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_INFO(x)
#define ATH_MSG_DEBUG(x)
An algorithm that can be simultaneously executed in multiple threads.
Store necessary information for weight computing for Enhanced Bias chain.
SG::ReadHandleKey< TrigCompositeUtils::DecisionContainer > m_finalDecisionKey
virtual StatusCode start() override
std::vector< HLT::Identifier > m_EBChainIds
Available EB chains' IDs from HLT Menu.
std::map< HLT::Identifier, std::vector< std::string > > m_EBChainIdToItem
L1 items for EB chains from HLT Menu.
Gaudi::Property< std::map< std::string, std::vector< std::string > > > m_chainToHLTSeed
virtual StatusCode initialize() override
virtual StatusCode execute(const EventContext &context) const override
StatusCode fillTotalPrescaleForChains(const EventContext &context, std::vector< EBChainInfo > &EBChains) const
Retrieve total prescales (L1 * HLT) for chains into map.
EBResult calculateEBWeight(const std::vector< EBChainInfo > &EBChains) const
Calculate EB result based on total prescales of chains.
SG::ReadHandleKey< TrigConf::HLTMenu > m_HLTMenuKey
SG::ReadCondHandleKey< TrigConf::L1PrescalesSet > m_L1PrescaleSetInputKey
PublicToolHandle< Trig::TrigDecisionTool > m_tdt
std::vector< std::string > parseItems(const std::string &itemStr)
Parse list of l1 items.
virtual StatusCode stop() override
EnhancedBiasWeightCompAlg(const std::string &name, ISvcLocator *svcLoc)
std::vector< EBChainInfo > getPassedEBChains() const
Get list of Enhanced Bias chains that cound have passed.
bool checkIfTriggeredByRandomChain(const std::vector< EBChainInfo > &EBChain) const
Check if any of random chains passed.
SG::ReadCondHandleKey< TrigConf::HLTPrescalesSet > m_HLTPrescaleSetInputKey
std::string name() const
reports human redable name
virtual bool isValid() override final
Can the handle be successfully dereferenced?
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
Store result of EB weight calculation.