ATLAS Offline Software
Loading...
Searching...
No Matches
Egamma1eRatioAlgTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
6#include "../Utilities/dump.h"
10
12#include <sstream>
13
14namespace GlobalSim {
15
17 const std::string& name,
18 const IInterface* parent) :
19 base_class(type, name, parent){
20 }
21
23
24 CHECK(m_nbhdTOBContainerReadKey.initialize());
25 CHECK(m_eRatioResultKey.initialize());
26 CHECK(m_eRatioKey.initialize());
27 CHECK(m_eRatioSimpleKey.initialize());
28
29 return StatusCode::SUCCESS;
30 }
31
32 StatusCode
33 Egamma1eRatioAlgTool::run(const EventContext& ctx) const {
34 ATH_MSG_DEBUG("run()");
35
36
37 // read in LArStrip neighborhoods from the event store
38 auto in =
40 ctx);
41 CHECK(in.isValid());
42
43 ATH_MSG_DEBUG("read in " << (*in).size() << " neighborhoods");
44
45 ap_int<16> peak = 0;
46 ap_int<16> secondMax = 0;
47
49 CHECK(h_eRatioResult.record(std::make_unique<IOBitwise::eEmEg1eRatioTOBContainer>()));
51 CHECK(h_eRatio.record(std::make_unique<std::vector<int> >()));
53 CHECK(h_eRatioSimple.record(std::make_unique<std::vector<float> >()));
54
55 for (const auto nbhdTOB : *in) {
56 auto c_phi = combine_phi(nbhdTOB);
57 if (msgLevel() <= MSG::DEBUG) {
58 std::stringstream ss;
59 ss << "eRatio input: ";
60 for (const auto& i : c_phi) {ss << i << ' ';}
61 ATH_MSG_DEBUG(ss.str());
62 }
63 //if (c_phi.empty()) {continue;} // corner case: not all phi have len 17
64 auto input = digitizer::digitize16(c_phi);
65 //Do want to ap_int them?
66 //ap_int<10>* c_input = &input[0]; // vector->array
67
68 if (msgLevel() <= MSG::DEBUG) {
69 std::stringstream ss;
70 ss << "eRatio input: ";
71 for (const auto& i : input) {ss << i << ' ';}
72 ATH_MSG_DEBUG(ss.str());
73 }
74
75 //Central peak is easy, it's column 9 in the middle row, i.e. entry 25.
76 peak = input.at(25);
77 ATH_MSG_DEBUG("Peak " << peak);
78
79 //Find the 6 secondary peaks, lets do it like the VHDL
80 //holder of the current second peak (we will have 6)
81 std::vector<ap_int<16>> secondPeak;
82 secondPeak.resize(6);
83
84 //Noise Margin: This should be 2sigma... set to 1 for now...
85 int noiseMargin = 1;
86 //Route one: <<< middle row, 24 down to 17
87 secondPeak[0] = secondPeakSearch(input, peak, 24, 17, noiseMargin);
88 //Route two: middle row >>>, 26 up to 33
89 secondPeak[1] = secondPeakSearch(input, peak, 26, 33, noiseMargin);
90 //Route three: <<< top row, 8 down to 0
91 secondPeak[2] = secondPeakSearch(input, peak, 8, 0, noiseMargin);
92 //Route four: top row >>>, 8 up to 16
93 secondPeak[3] = secondPeakSearch(input, peak, 8, 16, noiseMargin);
94 //Route five: <<< bottom row, 42 down to 34
95 secondPeak[4] = secondPeakSearch(input, peak, 42, 34, noiseMargin);
96 //Route five: bottom row >>>, 42 up to 50
97 secondPeak[5] = secondPeakSearch(input, peak, 42, 50, noiseMargin);
98
99 auto result = std::max_element(secondPeak.begin(), secondPeak.end());
100 ATH_MSG_DEBUG("Max element found at index "
101 << std::distance(secondPeak.begin(), result)
102 << " has value " << *result);
103
104 secondMax = *result;
105 ATH_MSG_DEBUG("Peak " << peak << " second " << secondMax);
106
107 //I am not confident on waht div_gen_0 does. So I am casting to floats for now.
108 if(peak > 0 || secondMax > 0){
109 auto eRatio = static_cast< float >(peak - secondMax)/static_cast< float >(peak + secondMax);
110 //h_eRatio->push_back(eRatio);
111 ATH_MSG_DEBUG("eRatio (p-sp/p+sp) is " << eRatio);
112
113 auto eRatioSimple = static_cast< float >(secondMax)/static_cast< float >(peak);
114 h_eRatioSimple->push_back(eRatioSimple);
115 ATH_MSG_DEBUG("eRatio (sp/p) is " << eRatio);
116
117 //Make a bitset to hold the result
118 std::bitset<IOBitwise::IeEmEg1eRatioTOB::s_eGamma1eRatio_width> result = 0;
119 //Sanity check to make sure we are in range 0-1
120 if(eRatio >= 0. && eRatio <= 1.0){
121 //Convert to 0-2047. If s_eGamma1eRatio_width changes this will change.
122 int eRatioPower = (1 << IOBitwise::IeEmEg1eRatioTOB::s_eGamma1eRatio_width) -1;
123 h_eRatio->push_back((int)(eRatio*eRatioPower));
124 result = (int)(eRatio*eRatioPower);
125 } else {
126 ATH_MSG_DEBUG("eRatio is not in the range 0-1");
127 }
128 h_eRatioResult->push_back(std::make_unique<IOBitwise::eEmEg1eRatioTOB>(*nbhdTOB, result));
129 }
130 }
131
132 return StatusCode::SUCCESS;
133 }
134
136 const ap_int<16> peak,
137 const int startCell,
138 const int endCell,
139 const ap_int<16> noiseMargin) const {
140 //First set a series of counters to "descending" = 0
141 int ascending = 0;
142
143 //Setup holder of the last energy we checked... which starts at the peak...
144 ap_int<16> lastEnergy = peak;
145 ap_int<16> secondPeak = 0;
146
147 //There must be a better way to do this?
148 int direction = 0;
149 if(startCell > endCell) {
150 direction =-1;
151 } else {
152 direction =1;
153 }
154
155 //Route 1: go down in the middlle row
156 for (auto itr = input.begin() + startCell; itr != input.begin() + endCell + direction; itr+=direction){
157 //Going down hill... until we are not...
158 ATH_MSG_DEBUG("Input is " << *itr << " last energy is " << lastEnergy);
159 if(ascending==0 && *itr>lastEnergy && *itr-lastEnergy > noiseMargin){
160 ATH_MSG_DEBUG("We are going up now " << *itr << " is more then " << lastEnergy);
161 ascending=1;
162 lastEnergy=*itr;
163 //Now check if we are at the peak as we are going uphill
164 } else if(ascending==1 && lastEnergy>*itr && lastEnergy-*itr > noiseMargin){
165 ATH_MSG_DEBUG("We are past the top " << *itr << " is less than " << lastEnergy);
166 if(secondPeak<*itr) secondPeak = lastEnergy;
167 ATH_MSG_DEBUG("The peak was " << secondPeak);
168 //I think we can break here... don't need to find a third peak
169 break;
170 } else {
171 lastEnergy=*itr;
172 }
173 }
174
175 return secondPeak;
176 }
177
178 std::vector<double>
180 auto result = std::vector<double>();
181
182 const auto& phi_low = nbhdTOB->Neighbourhood().phi_low();
183 //if (phi_low.size() != s_required_phi_len) {return result;}
184
185 const auto& phi_center = nbhdTOB->Neighbourhood().phi_center();
186 //if (phi_center.size() != s_required_phi_len) {return result;}
187
188 const auto& phi_high = nbhdTOB->Neighbourhood().phi_high();
189 //if (phi_high.size() != s_required_phi_len) {return result;}
190
191 result.reserve(s_combination_len);
192
193 //Make a big vector as the VHDL does. Not strictly necessary, could just use the nbhd.
194 std::transform(std::begin(phi_high), std::end(phi_high), std::back_inserter(result), [](const auto& high) {
195 return high.m_e;
196 });
197 std::transform(std::begin(phi_center), std::end(phi_center), std::back_inserter(result), [](const auto& center) {
198 return center.m_e;
199 });
200 std::transform(std::begin(phi_low), std::end(phi_low), std::back_inserter(result), [](const auto& low) {
201 return low.m_e;
202 });
203
204 return result;
205 }
206
207 std::string Egamma1eRatioAlgTool::toString() const {
208
209 std::stringstream ss;
210 ss << "Egamma1eRatioAlgTool. name: " << name() << '\n'
212 << '\n';
213 return ss.str();
214 }
215}
216
#define ATH_MSG_DEBUG(x)
#define CHECK(...)
Evaluate an expression and check for errors.
static Double_t ss
Header file to be included by clients of the Monitored infrastructure.
SG::ReadHandleKey< IOBitwise::eEmNbhoodTOBContainer > m_nbhdTOBContainerReadKey
virtual StatusCode run(const EventContext &ctx) const override
SG::WriteHandleKey< std::vector< int > > m_eRatioKey
virtual std::string toString() const override
ap_int< 16 > secondPeakSearch(const std::vector< ap_int< 16 > > &input, const ap_int< 16 > peak, const int startCell, const int endCell, const ap_int< 16 > noiseMargin) const
SG::WriteHandleKey< IOBitwise::eEmEg1eRatioTOBContainer > m_eRatioResultKey
SG::WriteHandleKey< std::vector< float > > m_eRatioSimpleKey
std::vector< double > combine_phi(const IOBitwise::eEmNbhoodTOB *) const
Egamma1eRatioAlgTool(const std::string &type, const std::string &name, const IInterface *parent)
static const std::size_t s_eGamma1eRatio_width
Count: Size of output bits of the eGamma1 eRatio algorithm.
Class to hold an eFexRoI and LAr strip neighbourhood.
virtual const LArStripNeighborhood & Neighbourhood() const
Returns the LarStripNeighbourhood: 3*17 cells centred on the eFexROI.
const StripDataVector & phi_low() const
Returns a vector of strip cell e/eta/phi data for the low phi row of the neighborhood.
const StripDataVector & phi_center() const
Returns a vector of strip cell e/eta/phi data for the central phi row of the neighborhood.
const StripDataVector & phi_high() const
Returns a vector of strip cell e/eta/phi data for the central high row of the neighborhood.
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
AlgTool that to test whether expected the TIP values generated by data supplied by eEmMultTestBench c...
static std::vector< ap_int< 16 > > digitize16(const std::vector< double > &v)
Definition Digitizer.h:42