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, 0);
42  m_REtaMin = std::vector(s_NumSelect, 0);
43  m_RHadMin = std::vector(s_NumSelect, 0);
44  m_WsTotMin = std::vector(s_NumSelect, 0);
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.
58  m_count_EtaMin = std::vector<std::vector<int>>(s_NumCnt,
59  std::vector<int>(s_NumEtaRanges, -0b011111111));
60 
61  m_count_EtaMax = std::vector<std::vector<int>>(s_NumCnt,
62  std::vector<int>(s_NumEtaRanges, 0b011111111));
63 
64 
65  return StatusCode::SUCCESS;
66  }
67 
69  eEmSortSelectCountContainerAlgTool::run(const EventContext& ctx) const {
70  ATH_MSG_DEBUG("run()");
71 
72 
73  // read in input data (a FIFO) for GepAlgoHypothesis from the event store
74 
75  auto fifo =
77  ctx);
78  CHECK(fifo.isValid());
79 
80  ATH_MSG_DEBUG("read in GepAlgoHypothesis fifo ");
81 
82  auto eEmTobs = make_eEmTobs(*fifo);
83 
84  auto collector = std::make_unique<DataCollector>();
85  collector->collect("eEmTobs in", eEmTobs);
86 
87  // Select. In the VHDL, ech TOB is treated indiviually. Here
88  // we split the TOBs into vectors of selected TOBs, where
89  // each selection has a set of associated cuts.
90  auto selected_eEmTobs = std::vector<std::vector<eEmTobPtr>>();
91  CHECK(make_selectedTobs(eEmTobs, selected_eEmTobs));
92 
93  collector->collect("selected eEmTobs containers", selected_eEmTobs);
94 
95  // why isn't the selection done on Generic TOBs? - bacause the original
96  // VHDL does not.
97 
98  auto selected_genericTobs =
99  std::vector<std::vector<GenTobPtr>>(selected_eEmTobs.size(),
100  std::vector<GenTobPtr>());
101 
102  auto make_genericTob = [](const auto& tob) {
103  return std::make_shared<GenericTob>(tob);
104  };
105 
106  for (std::size_t i = 0; i != selected_eEmTobs.size(); ++i){
107  std::transform(std::begin(selected_eEmTobs[i]),
108  std::end(selected_eEmTobs[i]),
109  std::back_inserter(selected_genericTobs[i]),
110  make_genericTob);
111  }
112 
113  collector->collect("Selected generic tob containers",
114  selected_genericTobs);
115  // Sort
116  auto EtGreater = [] (const GenTobPtr& l, const GenTobPtr& r) {
117  return l->Et() > r->Et();
118  };
119 
120 
121  auto ports_out = std::make_unique<eEmSortSelectCountContainerPortsOut>();
122 
123  // sort selected tobs, and copy to the appropriate location in the
124  // output port
125 
126  auto& outputTobs = ports_out->m_O_eEmGenTob;
127  for (std::size_t i = 0; i != s_NumSort; ++i) {
128 
129  auto& sel = selected_genericTobs[i];
130  auto divider = std::begin(sel) + std::min(sel.size(),
131  s_SortOutWidth[i]);
132 
133 
134  std::partial_sort(std::begin(sel),
135  divider,
136  std::end(sel),
137  EtGreater);
138 
139  auto start_iter =
140  std::begin(outputTobs) + s_SortOutStart[i];
141 
143  divider,
144  start_iter);
145  }
146 
147  collector->collect("Sorted generic tob containers",
148  selected_genericTobs);
149 
150 
151  // write unsorted generic TOBs to the output port
152  std::size_t numToCopy = std::min(s_NumNoSort, eEmTobs.size());
153 
154  std::transform(std::cbegin(eEmTobs),
155  std::cbegin(eEmTobs)+s_NumNoSort-1,
156  std::begin(outputTobs) + numToCopy,
157  make_genericTob);
158 
159 
160 
161  // count the tobs according to various criteria.
162  // limit the counts so that the number of output bits are not exeeded.
163  // write the counts to the output port
164 
165  // find the number count values
166  auto ntobs = count_tobs(selected_genericTobs);
167  collector->collect("eEmSortSelectCount counts", ntobs);
168 
169 
170  // limit the count values
171  std::vector<std::pair<std::size_t, unsigned>> bounded_counts;
172  bounded_counts.reserve(ntobs.size());
173 
174  for(std::size_t i = 0; i != ntobs.size(); ++i) {
175  bounded_counts.push_back({
176  std::min(ntobs[i], s_max_counts[i]),
177  s_CountOutWidth[i]});
178  };
179 
180  // convert each limited count value to 0, 1 (type: short)
181  std::vector<std::vector<short>> int_bit_vecs;
182  int_bit_vecs.reserve(bounded_counts.size());
183 
184 
185  std::transform(std::cbegin(bounded_counts),
186  std::cend(bounded_counts),
187  std::back_inserter(int_bit_vecs),
188  [](const auto& p) {
189  std::vector<short> bits(p.second, 0);
190  std::size_t i{0};
191  auto ntob = p.first;
192  while (ntob) {
193  if (ntob&1) {bits.at(i) = 1;}
194  ntob >>= 1;
195  ++i;
196  }
197  std::reverse(std::begin(bits),
198  std::end(bits));
199 
200  return bits;});
201 
202  // flatten the vectors of count bits
203  std::vector<short> int_bits;
204  for (const auto& v : int_bit_vecs) {
205  int_bits.insert(int_bits.end(), std::cbegin(v), std::cend(v));
206  }
207 
208  // sanity check
209 
210  if (int_bits.size() != s_NumTotalCountWidth or
211  int_bits.size() != (ports_out->m_O_Multiplicity)->size()) {
212  ATH_MSG_ERROR("incorrect number of count bits. Expected "
214  << " obtained " << int_bits.size()
215  << " number bits in output ports "
216  << (ports_out->m_O_Multiplicity)->size());
217  return StatusCode::FAILURE;
218  }
219 
220  // convert the 0, 1 (as shorts) to bits in the output port
221  auto& count_bits_out = ports_out->m_O_Multiplicity;
222  for (std::size_t i = 0; i != int_bits.size(); ++i){
223  if (int_bits[i] == 1) {
224  count_bits_out->set(i);
225  } else {
226  count_bits_out->reset(i);
227  }
228  }
229 
230 
231  {
232  std::stringstream ss;
233  ss << *collector;
234  ATH_MSG_DEBUG(ss.str());
235  }
236 
237  {
238  std::stringstream ss;
239  ss << *ports_out;
240  ATH_MSG_DEBUG(ss.str());
241  }
242 
243  // write the output port to the event store
244 
245  auto h_write =
247  ctx);
248 
249  CHECK(h_write.record(std::move(ports_out)));
250  return StatusCode::SUCCESS;
251  }
252 
253  std::vector<eEmTobPtr>
255 
256  auto eEmTobs = std::vector<eEmTobPtr>();
257  eEmTobs.reserve(fifo.size());
258 
259  std::transform(fifo.cbegin(),
260  fifo.cend(),
261  std::back_inserter(eEmTobs),
262  [](const auto& f) {
263  return std::make_shared<eEmTob>(f);
264  });
265 
266  return eEmTobs;
267  }
268 
269 
270  StatusCode
271  eEmSortSelectCountContainerAlgTool::make_selectedTobs(const std::vector<eEmTobPtr>& eEmTobs,
272  std::vector<std::vector<eEmTobPtr>>& selectedTobs) const {
273 
274  selectedTobs.reserve(s_NumSelect);
275 
276  for(std::size_t i = 0; i != s_NumSelect; ++i) {
277  auto selector = [etMin = m_EtMin[i],
278  rEtaMin= m_REtaMin[i],
279  rHadMin = m_RHadMin[i],
280  wsTotMin = m_WsTotMin[i]](const auto& tob){
281  return
282  bitSetToInt(tob->Et_bits()) >= etMin and
283  bitSetToInt(tob->REta_bits()) >= rEtaMin and
284  bitSetToInt(tob->RHad_bits()) >= rHadMin and
285  bitSetToInt(tob->WsTot_bits()) >= wsTotMin;
286  };
287 
288  std::vector<eEmTobPtr> s_tobs;
289  s_tobs.reserve(eEmTobs.size());
290  std::copy_if(eEmTobs.cbegin(),
291  eEmTobs.cend(),
292  std::back_inserter(s_tobs),
293  selector);
294 
295  selectedTobs.push_back(s_tobs);
296 
297  }
298 
299  return StatusCode::SUCCESS;
300  }
301 
302  std::vector<std::size_t>
303  eEmSortSelectCountContainerAlgTool::count_tobs(const std::vector<std::vector<GenTobPtr>>& tobContainerVector) const
304 
305  {
306  auto counts = std::vector<std::size_t>(s_NumCnt, 0);
307 
308  for (std::size_t i = 0; i != s_NumCnt; ++i) {
309  const auto& gTobs = tobContainerVector[s_CntSelN[i]];
310 
311  counts[i] = std::count_if(std::begin(gTobs),
312  std::end(gTobs),
313  [etMin_etaRegs = m_count_EtMin[i],
314  etaMin_etaRegs = m_count_EtaMin[i],
315  etaMax_etaRegs = m_count_EtaMax[i]](const auto& tob) {
316 
317  // loop over eta regions
318  for(std::size_t j{0}; j != etMin_etaRegs.size(); ++j) {
319  if (tob->Et() > etMin_etaRegs[j] and
320  tob->Eta() > etaMin_etaRegs[j] and
321  tob->Eta() <= etaMax_etaRegs[j]) {return true;}
322  }
323  return false;
324  });
325 
326  }
327  return counts;
328  }
329 
331 
332  std::stringstream ss;
333  ss << "eEmSortSelectCountContainerAlgTool.name: " << name() << '\n'
334  << m_HypoFIFOReadKey << '\n'
335  << '\n';
336  return ss.str();
337  }
338 }
339 
GlobalSim::eEmSortSelectCountContainerAlgTool::s_NumSelect
constexpr static auto & s_NumSelect
Definition: eEmSortSelectCountContainerAlgTool.h:99
beamspotman.r
def r
Definition: beamspotman.py:676
GlobalSim::eEmSortSelectCountContainerAlgTool::s_NumTotalCountWidth
constexpr static auto & s_NumTotalCountWidth
Definition: eEmSortSelectCountContainerAlgTool.h:124
PowhegControl_ttHplus_NLO.ss
ss
Definition: PowhegControl_ttHplus_NLO.py:83
eEmSortSelectCountContainerAlgTool.h
python.ext.silence.fifo
def fifo()
Definition: silence.py:37
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:67
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:120
UploadAMITag.l
list l
Definition: UploadAMITag.larcaf.py:158
GlobalSim::bitSetToInt
int bitSetToInt(BITSET bitset)
Definition: AlgoDataTypes.h:19
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
GlobalSim::eEmSortSelectCountContainerAlgTool::m_WsTotMin
std::vector< int > m_WsTotMin
Definition: eEmSortSelectCountContainerAlgTool.h:72
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_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:110
GlobalSim::make_eEmTobs
std::vector< eEmTobPtr > make_eEmTobs(const GlobalSim::GepAlgoHypothesisFIFO &fifo)
Definition: eEmSortSelectCountContainerAlgTool.cxx:254
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:271
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::GepAlgoHypothesisFIFO
std::vector< GepAlgoHypothesisPortsIn > GepAlgoHypothesisFIFO
Definition: GepAlgoHypothesisPortsIn.h:70
GlobalSim::eEmSortSelectCountContainerAlgTool::m_REtaMin
std::vector< int > m_REtaMin
Definition: eEmSortSelectCountContainerAlgTool.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:117
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:330
Monitored.h
Header file to be included by clients of the Monitored infrastructure.
GlobalSim::eEmSortSelectCountContainerAlgTool::m_RHadMin
std::vector< int > m_RHadMin
Definition: eEmSortSelectCountContainerAlgTool.h:71
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:73
GlobalSim::eEmSortSelectCountContainerAlgTool::m_EtMin
std::vector< int > m_EtMin
Definition: eEmSortSelectCountContainerAlgTool.h:69
python.selector.AtlRunQuerySelectorLhcOlc.selector
selector
Definition: AtlRunQuerySelectorLhcOlc.py:611
GlobalSim::eEmSortSelectCountContainerAlgTool::run
virtual StatusCode run(const EventContext &ctx) const override
Definition: eEmSortSelectCountContainerAlgTool.cxx:69
GlobalSim::eEmSortSelectCountContainerAlgTool::s_CntSelN
constexpr static std::array< std::size_t, s_NumCnt > s_CntSelN
Definition: eEmSortSelectCountContainerAlgTool.h:96
calibdata.copy
bool copy
Definition: calibdata.py:27
GlobalSim::eEmSortSelectCountContainerAlgTool::s_NumNoSort
constexpr static auto & s_NumNoSort
Definition: eEmSortSelectCountContainerAlgTool.h:106
GlobalSim::eEmSortSelectCountContainerAlgTool::s_SortOutStart
constexpr static auto & s_SortOutStart
Definition: eEmSortSelectCountContainerAlgTool.h:114
DataCollector.h
GlobalSim::eEmSortSelectCountContainerAlgTool::count_tobs
std::vector< std::size_t > count_tobs(const std::vector< std::vector< GenTobPtr >> &) const
Definition: eEmSortSelectCountContainerAlgTool.cxx:303
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