Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
eEmSortSelectCountContainerAlgTool.cxx
Go to the documentation of this file.
1 
2 /*
3  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
4 */
5 
7 #include "AlgoDataTypes.h" // bitSetToInt()
8 #include "GenericTob.h" // bitSetToInt()
9 #include "DataCollector.h"
10 
11 #include "../../../dump.h"
12 #include "../../../dump.icc"
13 
16 
17 #include <sstream>
18 #include <algorithm>
19 
20 namespace GlobalSim {
21 
22 
23  std::vector<eEmTobPtr>
25 
26 
28  const std::string& name,
29  const IInterface* parent) :
30  base_class(type, name, parent){
31  }
32 
34 
37 
38  // Cut values for Select part of Algorithm. Not yet provided in VHDL
39 
40 
41  m_EtMin = std::vector(s_NumSelect, 0U);
42  m_REtaMin = std::vector(s_NumSelect, 0U);
43  m_RHadMin = std::vector(s_NumSelect, 0U);
44  m_WsTotMin = std::vector(s_NumSelect, 0U);
45 
46  // Cut values for the Count part of Algorithm.
47  // All values set to EM5 for now
48 
50  std::vector<std::vector<unsigned int>> (s_NumCnt,
51  std::vector<unsigned int>(s_NumEtaRanges,
52  0x019));
53 
54 
55  // Eta min is tricky:
56  // It has been set to 0x100000000 in a 9 bit word. So it is negative.
57  // But our words have > 9 bits.
59  std::vector<std::vector<int>>(s_NumCnt,
60  std::vector<int>(s_NumEtaRanges, -0b011111111));
61 
62  m_count_EtaMax = std::vector<std::vector<int>>(s_NumCnt,
63  std::vector<int>(s_NumEtaRanges, 0b011111111));
64 
65 
66  return StatusCode::SUCCESS;
67  }
68 
70  eEmSortSelectCountContainerAlgTool::run(const EventContext& ctx) const {
71  ATH_MSG_DEBUG("run()");
72 
73 
74  // read in input data (a FIFO) for GepAlgoHypothesis from the event store
75 
76  auto fifo =
78  ctx);
79  CHECK(fifo.isValid());
80 
81  ATH_MSG_DEBUG("read in GepAlgoHypothesis fifo ");
82 
83  {
84  std::stringstream ss;
85  ss <<'\n';
86  for (const auto& p : *fifo) {
87  ss << eEmInputTOBToString(*(p.m_I_eEmTobs)) << '\n';
88  }
89  ATH_MSG_DEBUG(ss.str());
90  }
91 
92 
93  auto eEmTobs = make_eEmTobs(*fifo);
94 
95  auto collector = std::make_unique<DataCollector>();
96  collector->collect("eEmTobs in", eEmTobs);
97 
98  // Select. In the VHDL, ech TOB is treated indiviually. Here
99  // we split the TOBs into vectors of selected TOBs, where
100  // each selection has a set of associated cuts.
101  auto selected_eEmTobs = std::vector<std::vector<eEmTobPtr>>();
102  CHECK(make_selectedTobs(eEmTobs, selected_eEmTobs));
103 
104  collector->collect("selected eEmTobs containers", selected_eEmTobs);
105 
106  // why isn't the selection done on Generic TOBs? - bacause the original
107  // VHDL does not.
108 
109  auto selected_genericTobs =
110  std::vector<std::vector<GenTobPtr>>(selected_eEmTobs.size(),
111  std::vector<GenTobPtr>());
112 
113  auto make_genericTob = [](const auto& tob) {
114  return std::make_shared<GenericTob>(tob);
115  };
116 
117  for (std::size_t i = 0; i != selected_eEmTobs.size(); ++i){
118  std::transform(std::begin(selected_eEmTobs[i]),
119  std::end(selected_eEmTobs[i]),
120  std::back_inserter(selected_genericTobs[i]),
121  make_genericTob);
122  }
123 
124  collector->collect("Selected generic tob containers",
125  selected_genericTobs);
126  // Sort
127  auto EtGreater = [] (const GenTobPtr& l, const GenTobPtr& r) {
128  return l->Et() > r->Et();
129  };
130 
131 
132  auto ports_out = std::make_unique<eEmSortSelectCountContainerPortsOut>();
133 
134  // sort selected tobs, and copy to the appropriate location in the
135  // output port
136 
137  auto& outputTobs = ports_out->m_O_eEmGenTob;
138  for (std::size_t i = 0; i != s_NumSort; ++i) {
139 
140  auto& sel = selected_genericTobs[i];
141  auto divider = std::begin(sel) + std::min(sel.size(),
142  s_SortOutWidths[i]);
143 
144 
145  std::partial_sort(std::begin(sel),
146  divider,
147  std::end(sel),
148  EtGreater);
149 
150  auto start_iter =
151  std::begin(outputTobs) + s_SortOutStart[i];
152 
154  divider,
155  start_iter);
156  }
157 
158  collector->collect("Sorted generic tob containers",
159  selected_genericTobs);
160 
161 
162  // write unsorted generic TOBs to the output port
163  std::size_t numToCopy = std::min(outputTobs.size()-s_SortOutWidth,
164  eEmTobs.size());
165 
166  std::transform(std::cbegin(eEmTobs),
167  std::cbegin(eEmTobs)+numToCopy,
168  std::begin(outputTobs) + s_SortOutWidth,
169  make_genericTob);
170 
171 
172 
173  // count the tobs according to various criteria.
174  // limit the counts so that the number of output bits are not exeeded.
175  // write the counts to the output port
176 
177  // find the number count values
178  auto ntobs = count_tobs(selected_genericTobs);
179  collector->collect("eEmSortSelectCount counts", ntobs);
180 
181 
182  // limit the count values
183  std::vector<std::pair<std::size_t, unsigned>> bounded_counts;
184  bounded_counts.reserve(ntobs.size());
185 
186  for(std::size_t i = 0; i != ntobs.size(); ++i) {
187  bounded_counts.push_back({
188  std::min(ntobs[i], s_max_counts[i]),
189  s_CountOutWidth[i]});
190  };
191 
192  // convert each limited count value to 0, 1 (type: short)
193  std::vector<std::vector<short>> int_bit_vecs;
194  int_bit_vecs.reserve(bounded_counts.size());
195 
196 
197  std::transform(std::cbegin(bounded_counts),
198  std::cend(bounded_counts),
199  std::back_inserter(int_bit_vecs),
200  [](const auto& p) {
201  std::vector<short> bits(p.second, 0);
202  std::size_t i{0};
203  auto ntob = p.first;
204  while (ntob) {
205  if (ntob&1) {bits.at(i) = 1;}
206  ntob >>= 1;
207  ++i;
208  }
209  std::reverse(std::begin(bits),
210  std::end(bits));
211 
212  return bits;});
213 
214  // flatten the vectors of count bits
215  std::vector<short> int_bits;
216  for (const auto& v : int_bit_vecs) {
217  int_bits.insert(int_bits.end(), std::cbegin(v), std::cend(v));
218  }
219 
220  // sanity check
221 
222  if (int_bits.size() != s_NumTotalCountWidth or
223  int_bits.size() != (ports_out->m_O_Multiplicity)->size()) {
224  ATH_MSG_ERROR("incorrect number of count bits. Expected "
226  << " obtained " << int_bits.size()
227  << " number bits in output ports "
228  << (ports_out->m_O_Multiplicity)->size());
229  return StatusCode::FAILURE;
230  }
231 
232  // convert the 0, 1 (as shorts) to bits in the output port
233  auto& count_bits_out = ports_out->m_O_Multiplicity;
234  for (std::size_t i = 0; i != int_bits.size(); ++i){
235  if (int_bits[i] == 1) {
236  count_bits_out->set(i);
237  } else {
238  count_bits_out->reset(i);
239  }
240  }
241 
242 
243  {
244  std::stringstream ss;
245  ss << *collector;
246  ATH_MSG_DEBUG(ss.str());
247  }
248 
249  {
250  std::stringstream ss;
251  ss << *ports_out;
252  ATH_MSG_DEBUG(ss.str());
253  }
254 
255  // write the output port to the event store
256 
257  auto h_write =
259  ctx);
260 
261  CHECK(h_write.record(std::move(ports_out)));
262  return StatusCode::SUCCESS;
263  }
264 
265  std::vector<eEmTobPtr>
267 
268  auto eEmTobs = std::vector<eEmTobPtr>();
269  eEmTobs.reserve(fifo.size());
270 
271  std::transform(fifo.cbegin(),
272  fifo.cend(),
273  std::back_inserter(eEmTobs),
274  [](const auto& f) {
275  return std::make_shared<eEmTob>(f);
276  });
277 
278  return eEmTobs;
279  }
280 
281 
282  StatusCode
283  eEmSortSelectCountContainerAlgTool::make_selectedTobs(const std::vector<eEmTobPtr>& eEmTobs,
284  std::vector<std::vector<eEmTobPtr>>& selectedTobs) const {
285 
286  selectedTobs.reserve(s_NumSelect);
287 
288  for(std::size_t i = 0; i != s_NumSelect; ++i) {
289  auto selector = [etMin = m_EtMin[i],
290  rEtaMin= m_REtaMin[i],
291  rHadMin = m_RHadMin[i],
292  wsTotMin = m_WsTotMin[i]](const auto& tob){
293 
294  return
295  (tob->Et_bits()).to_ulong() >= etMin and
296  (tob->REta_bits()).to_ulong() >= rEtaMin and
297  (tob->RHad_bits()).to_ulong() >= rHadMin and
298  (tob->WsTot_bits()).to_ulong() >= wsTotMin;
299  };
300 
301  std::vector<eEmTobPtr> s_tobs;
302  s_tobs.reserve(eEmTobs.size());
303  std::copy_if(eEmTobs.cbegin(),
304  eEmTobs.cend(),
305  std::back_inserter(s_tobs),
306  selector);
307 
308  selectedTobs.push_back(s_tobs);
309 
310  }
311 
312  return StatusCode::SUCCESS;
313  }
314 
315  std::vector<std::size_t>
316  eEmSortSelectCountContainerAlgTool::count_tobs(const std::vector<std::vector<GenTobPtr>>& tobContainerVector) const
317 
318  {
319  auto counts = std::vector<std::size_t>(s_NumCnt, 0);
320 
321  for (std::size_t i = 0; i != s_NumCnt; ++i) {
322  const auto& gTobs = tobContainerVector[s_CntSelN[i]];
323 
324  counts[i] = std::count_if(std::begin(gTobs),
325  std::end(gTobs),
326  [etMin_etaRegs = m_count_EtMin[i],
327  etaMin_etaRegs = m_count_EtaMin[i],
328  etaMax_etaRegs = m_count_EtaMax[i]](const auto& tob) {
329 
330  // loop over eta regions
331  for(std::size_t j{0}; j != etMin_etaRegs.size(); ++j) {
332  if (tob->Et() > etMin_etaRegs[j] and
333  tob->Eta() > etaMin_etaRegs[j] and
334  tob->Eta() <= etaMax_etaRegs[j]) {return true;}
335  }
336  return false;
337  });
338 
339  }
340  return counts;
341  }
342 
344 
345  std::stringstream ss;
346  ss << "eEmSortSelectCountContainerAlgTool.name: " << name() << '\n'
347  << m_HypoFIFOReadKey << '\n'
348  << '\n';
349  return ss.str();
350  }
351 }
352 
GlobalSim::eEmSortSelectCountContainerAlgTool::s_NumSelect
constexpr static auto & s_NumSelect
Definition: eEmSortSelectCountContainerAlgTool.h:99
beamspotman.r
def r
Definition: beamspotman.py:676
GlobalSim::eEmSortSelectCountContainerAlgTool::m_WsTotMin
std::vector< unsigned > m_WsTotMin
Definition: eEmSortSelectCountContainerAlgTool.h:72
GlobalSim::eEmSortSelectCountContainerAlgTool::s_NumTotalCountWidth
constexpr static auto & s_NumTotalCountWidth
Definition: eEmSortSelectCountContainerAlgTool.h:126
PowhegControl_ttHplus_NLO.ss
ss
Definition: PowhegControl_ttHplus_NLO.py:83
eEmSortSelectCountContainerAlgTool.h
GlobalSim::eEmInputTOBToString
std::string eEmInputTOBToString(const std::bitset< 72 > &bs)
Definition: GepAlgoHypothesisPortsIn.cxx:9
python.ext.silence.fifo
def fifo()
Definition: silence.py:37
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
min
constexpr double min()
Definition: ap_fixedTest.cxx:26
AlgoDataTypes.h
GlobalSim::eEmSortSelectCountContainerAlgTool::GenTobPtr
typename eEmSortSelectCountContainerPortsOut::GenTobPtr GenTobPtr
Definition: eEmSortSelectCountContainerAlgTool.h:24
GlobalSim::eEmSortSelectCountContainerAlgTool::m_HypoFIFOReadKey
SG::ReadHandleKey< GlobalSim::GepAlgoHypothesisFIFO > m_HypoFIFOReadKey
Definition: eEmSortSelectCountContainerAlgTool.h:49
PlotCalibFromCool.begin
begin
Definition: PlotCalibFromCool.py:94
GlobalSim::eEmSortSelectCountContainerAlgTool::s_CountOutWidth
constexpr static auto & s_CountOutWidth
Definition: eEmSortSelectCountContainerAlgTool.h:122
UploadAMITag.l
list l
Definition: UploadAMITag.larcaf.py:158
GlobalSim::eEmSortSelectCountContainerAlgTool::m_count_EtMin
std::vector< std::vector< unsigned int > > m_count_EtMin
Definition: eEmSortSelectCountContainerAlgTool.h:79
GenericTob.h
GlobalSim::eEmSortSelectCountContainerAlgTool::s_NumSort
constexpr static auto & s_NumSort
Definition: eEmSortSelectCountContainerAlgTool.h:102
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
DeMoUpdate.reverse
reverse
Definition: DeMoUpdate.py:563
GlobalSim::eEmSortSelectCountContainerAlgTool::eEmSortSelectCountContainerAlgTool
eEmSortSelectCountContainerAlgTool(const std::string &type, const std::string &name, const IInterface *parent)
Definition: eEmSortSelectCountContainerAlgTool.cxx:27
GlobalSim::eEmSortSelectCountContainerAlgTool::m_RHadMin
std::vector< unsigned > m_RHadMin
Definition: eEmSortSelectCountContainerAlgTool.h:71
GlobalSim::eEmSortSelectCountContainerAlgTool::m_count_EtaMin
std::vector< std::vector< int > > m_count_EtaMin
Definition: eEmSortSelectCountContainerAlgTool.h:80
GlobalSim::eEmSortSelectCountContainerAlgTool::s_SortOutWidth
constexpr static auto & s_SortOutWidth
Definition: eEmSortSelectCountContainerAlgTool.h:113
GlobalSim::make_eEmTobs
std::vector< eEmTobPtr > make_eEmTobs(const GlobalSim::GepAlgoHypothesisFIFO &fifo)
Definition: eEmSortSelectCountContainerAlgTool.cxx:266
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
GlobalSim
AlgTool to obtain a selection of eFex RoIs read in from the event store.
Definition: dump.h:8
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
MonitoredCollection.h
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
GlobalSim::eEmSortSelectCountContainerAlgTool::make_selectedTobs
StatusCode make_selectedTobs(const std::vector< eEmTobPtr > &, std::vector< std::vector< eEmTobPtr >> &) const
Definition: eEmSortSelectCountContainerAlgTool.cxx:283
lumiFormat.i
int i
Definition: lumiFormat.py: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
GlobalSim::eEmSortSelectCountContainerAlgTool::m_EtMin
std::vector< unsigned > m_EtMin
Definition: eEmSortSelectCountContainerAlgTool.h:69
GlobalSim::GepAlgoHypothesisFIFO
std::vector< GepAlgoHypothesisPortsIn > GepAlgoHypothesisFIFO
Definition: GepAlgoHypothesisPortsIn.h:70
sel
sel
Definition: SUSYToolsTester.cxx:97
Amg::transform
Amg::Vector3D transform(Amg::Vector3D &v, Amg::Transform3D &tr)
Transform a point from a Trasformation3D.
Definition: GeoPrimitivesHelpers.h:156
GlobalSim::eEmSortSelectCountContainerAlgTool::s_max_counts
constexpr static auto & s_max_counts
Definition: eEmSortSelectCountContainerAlgTool.h:119
test_pyathena.parent
parent
Definition: test_pyathena.py:15
AnalysisUtils::copy_if
Out copy_if(In first, const In &last, Out res, const Pred &p)
Definition: IFilterUtils.h:30
GlobalSim::eEmSortSelectCountContainerAlgTool::s_NumEtaRanges
constexpr static std::size_t s_NumEtaRanges
Definition: eEmSortSelectCountContainerAlgTool.h:91
CHECK
#define CHECK(...)
Evaluate an expression and check for errors.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:422
hist_file_dump.f
f
Definition: hist_file_dump.py:141
GlobalSim::eEmSortSelectCountContainerAlgTool::s_NumCnt
constexpr static std::size_t s_NumCnt
Definition: eEmSortSelectCountContainerAlgTool.h:90
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
GlobalSim::eEmSortSelectCountContainerAlgTool::m_portsOutWriteKey
SG::WriteHandleKey< GlobalSim::eEmSortSelectCountContainerPortsOut > m_portsOutWriteKey
Definition: eEmSortSelectCountContainerAlgTool.h:58
GlobalSim::eEmSortSelectCountContainerAlgTool::toString
virtual std::string toString() const override
Definition: eEmSortSelectCountContainerAlgTool.cxx:343
Monitored.h
Header file to be included by clients of the Monitored infrastructure.
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
python.PyAthena.v
v
Definition: PyAthena.py:154
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
python.selector.AtlRunQuerySelectorLhcOlc.selector
selector
Definition: AtlRunQuerySelectorLhcOlc.py:611
GlobalSim::eEmSortSelectCountContainerAlgTool::s_SortOutWidths
constexpr static auto & s_SortOutWidths
Definition: eEmSortSelectCountContainerAlgTool.h:110
GlobalSim::eEmSortSelectCountContainerAlgTool::run
virtual StatusCode run(const EventContext &ctx) const override
Definition: eEmSortSelectCountContainerAlgTool.cxx:70
GlobalSim::eEmSortSelectCountContainerAlgTool::s_CntSelN
constexpr static std::array< std::size_t, s_NumCnt > s_CntSelN
Definition: eEmSortSelectCountContainerAlgTool.h:96
GlobalSim::eEmSortSelectCountContainerAlgTool::m_REtaMin
std::vector< unsigned > m_REtaMin
Definition: eEmSortSelectCountContainerAlgTool.h:70
calibdata.copy
bool copy
Definition: calibdata.py:27
GlobalSim::eEmSortSelectCountContainerAlgTool::s_SortOutStart
constexpr static auto & s_SortOutStart
Definition: eEmSortSelectCountContainerAlgTool.h:116
DataCollector.h
GlobalSim::eEmSortSelectCountContainerAlgTool::count_tobs
std::vector< std::size_t > count_tobs(const std::vector< std::vector< GenTobPtr >> &) const
Definition: eEmSortSelectCountContainerAlgTool.cxx:316
GlobalSim::eEmSortSelectCountContainerAlgTool::initialize
virtual StatusCode initialize() override
Definition: eEmSortSelectCountContainerAlgTool.cxx:33
GlobalSim::eEmSortSelectCountContainerAlgTool::m_count_EtaMax
std::vector< std::vector< int > > m_count_EtaMax
Definition: eEmSortSelectCountContainerAlgTool.h:81