2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
5 // $Id: RingerProcedureWrapper.icc 791627 2017-01-10 04:45:53Z wsfreund $
6 #ifndef RINGERSELECTORTOOLS_PROCEDURES_RINGERPROCEDUREWRAPPER_ICC
7 #define RINGERSELECTORTOOLS_PROCEDURES_RINGERPROCEDUREWRAPPER_ICC
17 #include "RingerProcedureWrapper.h"
18 #include "RingerSelectorTools/RingerSelectorToolsDefs.h"
19 #include "RingerSelectorTools/tools/VariableDependency.h"
20 #include "RingerSelectorTools/tools/TrackPatternsHolder.h"
21 #include "RingerSelectorTools/tools/IOHelperFcns.h"
24 * @brief Namespace dedicated for Ringer utilities
28 namespace WrapperHelperFcns {
30 // =============================================================================
32 * @brief Returns eta bin number which is within region.
34 * Vector Dimension: [etBin][etaBin]:
36 template<class vecType>
37 size_t findEtaBin( const float eta,
38 const typename std::vector< std::vector <
43 std::vector< vecType* > firstRow = varDepCol[0];
44 for (size_t idx = 0; idx < firstRow.size(); ++idx){
45 if (firstRow[idx]->isWithinEtaRange(eta)){
49 throw std::runtime_error(std::string( "eta ") +
50 std::to_string(eta) + " is not inside Ringer Procedure range.");
53 // =============================================================================
55 * @brief Returns Et bin number which is within region.
57 * Vector Dimension: [etBin][etaBin]:
59 template<class vecType>
60 size_t findEtBin( const float et,
67 for (size_t idx = 0; idx < varDepCol.size(); ++idx){
68 if ( varDepCol[idx][0]->isWithinEtRange(et) ){
72 throw std::runtime_error(std::string( "Et ") +
73 std::to_string(et) + " is not inside Ringer Procedure range.");
76 // =============================================================================
77 template < typename vecType >
78 void checkCollection( const std::vector< std::vector<vecType*> > &vec,
79 EtaDependency etaDependency, EtDependency etDependency )
82 throw std::runtime_error(std::string(
83 "Dependency collection cannot be empty."));
85 if ( !static_cast<bool>(etDependency) && (vec.size() != 1) ) {
86 throw std::runtime_error(std::string(
87 "Cannot have EtIndependent vector with size different from unit."));
89 for ( size_t etIdx = 0; etIdx < vec.size() ; ++etIdx){
90 size_t etaSize = vec[etIdx].size();
92 throw std::runtime_error(std::string(
93 "Dependency collection cannot have an empty vector."));
95 if ( !static_cast<bool>(etaDependency) && (etaSize != 1) ) {
96 throw std::runtime_error(std::string(
97 "Cannot have EtaIndependent vector with size "
98 "different from unit."));
100 for ( size_t etaIdx = 0; etaIdx < etaSize; ++etaIdx){
101 if( !vec[etIdx][etaIdx] ){
102 throw std::runtime_error(std::string(
103 "Dependency collection holden object is invalid."));
105 if ( etaDependency != vec[etIdx][etaIdx]->etaDep() ){
106 if ( etDependency != vec[etIdx][etaIdx]->etDep() ){
107 throw std::runtime_error(std::string(
108 "Wrong eta and Et dependency. EtaDependency should be ") +
109 toStr(etaDependency) + " and is " +
110 toStr(vec[etIdx][etaIdx]->etaDep()) + ", as well as EtDependency "
111 "should be " + toStr(etDependency) + " and is " +
112 toStr(vec[etIdx][etaIdx]->etDep()) + "." );
114 throw std::runtime_error(std::string(
115 "Wrong eta dependency. EtaDependency should be ") +
116 toStr(etaDependency) + " and is " +
117 toStr(vec[etIdx][etaIdx]->etaDep()) + "." );
120 if ( etDependency != vec[etIdx][etaIdx]->etDep() ){
121 throw std::runtime_error(std::string(
122 "Wrong Et dependency. EtDependency should be ") +
123 toStr(etDependency) + " and is " +
124 toStr(vec[etIdx][etaIdx]->etDep()) + "." );
130 // =============================================================================
131 template < typename vecType >
132 void checkCollection(
133 const std::vector< std::vector< std::vector<vecType*> > > &vec,
134 EtaDependency etaDependency, EtDependency etDependency)
137 throw std::runtime_error(std::string(
138 "Dependency collection cannot be empty."));
140 for ( size_t segIdx = 0; segIdx < vec.size(); ++segIdx){
141 size_t etSize = vec[segIdx].size();
143 throw std::runtime_error(std::string(
144 "Dependency collection cannot have an"
145 " empty vector<vector>."));
147 checkCollection(vec[segIdx], etaDependency, etDependency);
151 // =============================================================================
152 template < typename vecType >
153 void setCollectionMsgStream(
155 const std::vector< std::vector<vecType*> > &vec )
157 for ( size_t etIdx = 0; etIdx < vec.size() ; ++etIdx){
158 size_t etaSize = vec[etIdx].size();
159 for ( size_t etaIdx = 0; etaIdx < etaSize; ++etaIdx){
160 vec[etIdx][etaIdx]->setMsgStream(stream);
165 // =============================================================================
166 template < typename vecType >
167 void setCollectionMsgStream(
169 const std::vector< std::vector< std::vector<vecType*> > > &vec )
171 for ( size_t segIdx = 0; segIdx < vec.size(); ++segIdx) {
172 setCollectionMsgStream(stream,vec[segIdx]);
176 // =============================================================================
178 * @brief Ensure that segType is NoSegmentation, otherwise throws runtime_error
181 void ensureNoSegmentationOnlyFcn(const SegmentationType segType)
183 // XXX This shouldn't be a runtime_error, but it seems I would need to
184 // implement a RingerProcedureWrapper for each segType (check if this
185 // hypothesis is true), which would demand more time that I afford.
186 if (segType != SegmentationType::NoSegmentation ){
187 throw std::runtime_error(std::string(
188 "Cannot use this method for other"
189 " SegmentationType then NoSegmentation."));
193 // =============================================================================
195 * @brief Insert toBeCopied vector values to newHolder end
198 void insertVecToVecEnd(
199 const std::vector<float> &toBeCopied,
200 std::vector<float> &newHolder)
202 newHolder.insert(newHolder.end(), toBeCopied.begin(), toBeCopied.end());
205 // =============================================================================
207 * @brief Execute segmented pre-processor unified routine
210 void executeSegmentedPP(
211 std::vector<float> &input,
212 const PreProcessing::IPreProcessor *proc,
213 std::vector<float> &transformVec)
216 proc->execute(input);
217 // Concatenate it to global transformation vector:
218 insertVecToVecEnd(input, transformVec);
219 // Clear input vector:
223 // =============================================================================
225 * @brief Execute segmented discriminator unified routine
228 void executeSegmentedDiscr(
229 std::vector<float> &input,
230 const Discrimination::IDiscriminator *discr,
231 std::vector<float> &output)
233 // Get this procedure output into another vector:
234 std::vector<float> localOutput;
235 localOutput.reserve(output.capacity());
237 discr->execute(input,localOutput);
238 // Concatenate it to global output vec:
239 insertVecToVecEnd(localOutput, output);
240 // Clear input vector:
244 // =============================================================================
246 * @brief Get Calorimeter segment from transformVec
249 void getCaloSegmentFromTransformVec(
250 const unsigned nRings,
251 const std::vector<float> &transformVec,
252 std::vector<float> &transformVecSegment)
254 transformVecSegment.clear();
255 for ( size_t idx = 0; idx < nRings; ++idx )
257 transformVecSegment.push_back(transformVec[idx]);
261 // =============================================================================
263 * @brief Get Calorimeter segment from transformVec
265 template<typename segment_t>
267 void getCaloSegmentFromTransformVec(
268 const xAOD::RingSetConf::RawConfCollection &rawConfCol,
269 const segment_t segment,
270 const std::vector<float> &transformVec,
271 std::vector<float> &transformVecSegment)
274 transformVecSegment.clear();
276 // Get the start and end indexes from the required segment:
277 unsigned startIdx(0), endIdx(0);
278 xAOD::RingSetConf::getEdges(rawConfCol,segment,startIdx,endIdx);
280 for (unsigned idx = startIdx; idx < endIdx; ++idx ) {
281 transformVecSegment.push_back(transformVec[idx]);
285 // =============================================================================
287 * @brief Get Track segment from transformVec
290 void getTrackSegmentFromTransformVec(
291 const unsigned nRings,
292 const std::vector<float> &transformVec,
293 std::vector<float> &transformVecSegment)
295 transformVecSegment.clear();
296 for ( size_t idx = nRings; idx < transformVec.size(); ++idx ) {
297 transformVecSegment.push_back(transformVec[idx]);
302 // =============================================================================
304 * These macros can be used if we want to declare a specific type wrapper, so
305 * that it can have it dedicated compiled code. Although its wrapper calling
306 * method will be determined during runtime (it is holded though its
307 * interface), it will have its code specifically compiled for this type. This
308 * means that it execute its collection methods through statically determined
309 * methods are can also benefit of inline methods.
311 * However, it shouldn't be used for every type created, as it will increase
312 * compile time and library size.
314 * Besides, there is no garantee that there will be speed gain or that it won't
317 * If you use the READ_ALL_DEP_WRAPPER macro, it will declare all possible
318 * dependent types for that class: you should pass this wrapper the variables
319 * that are read on the file.
321 * Instead using the READ_ALL_DEP_WRAPPER, when you need to declare only one
322 * specific template type for reading, you can otherwise use READ_WRAPPER. In
323 * this case, the passing parameters are not variables, but rather the template
326 * If you want to declare all eta/et dependent cases for a procedure type,
327 * you can use the READ_ETA_ET_DEP_WRAPPER, where it defines for the etaDep and
328 * etDep variables all possible variations. The segType however must be a
329 * value determined at compile time.
331 * Otherwise you want to declare segment dependent cases, which are not eta/et
332 * dependent, use READ_SEG_DEP_WRAPPER inserting segType as the variable and
333 * eta/et as compile time values, such as EtaIndependent and EtIndependent.
335 #define READ_WRAPPER(vec, wrapType, segType, etaDep, etDep, configDir, version) \
336 vec.push_back( Ringer::RingerProcedureWrapper<wrapType, \
337 etaDep, etDep, segType >::read(configDir,version) );
339 // =============================================================================
340 #define READ_ETA_DEP_WRAPPER(vec, wrapType, segType, etaDep, etDep, \
341 configDir, version) \
344 case Ringer::EtaDependent: \
345 READ_WRAPPER(vec, wrapType, segType, Ringer::EtaDependent, etDep, \
346 configDir, version) \
348 case Ringer::EtaIndependent: \
349 READ_WRAPPER(vec, wrapType, segType, Ringer::EtaIndependent, etDep, \
350 configDir, version) \
353 throw std::runtime_error(std::string("Unknown Eta dependency.")); \
356 // =============================================================================
357 #define READ_SEG_DEP_WRAPPER(vec, wrapType, segType, etaDep, etDep, configDir, \
361 case Ringer::NoSegmentation: \
362 READ_WRAPPER(vec, wrapType, Ringer::NoSegmentation, etaDep, etDep, \
363 configDir, version) \
365 case Ringer::TrackCalPatTypeSegmentation: \
366 READ_WRAPPER(vec, wrapType, Ringer::TrackCalPatTypeSegmentation, etaDep, \
367 etDep, configDir, version) \
369 case Ringer::TrackCalSegmentation: \
370 READ_WRAPPER(vec, wrapType, Ringer::TrackCalSegmentation, etaDep, etDep, \
371 configDir, version) \
373 case Ringer::TrackCalJointLayers: \
374 READ_ETA_ET_DEP_WRAPPER(vec, wrapType, Ringer::TrackCalJointLayers, \
375 etaDep, etDep, configDir, version) \
377 case Ringer::TrackCalJointSections: \
378 READ_ETA_ET_DEP_WRAPPER(vec, wrapType, Ringer::TrackCalJointSections, \
379 etaDep, etDep, configDir, version) \
382 throw std::runtime_error(std::string("Unknown segmentation type")); \
385 // =============================================================================
386 #define READ_ETA_ET_DEP_WRAPPER(vec, wrapType, segType, etaDep, etDep, \
387 configDir, version) \
390 case Ringer::EtDependent: \
391 READ_ETA_DEP_WRAPPER(vec, wrapType, segType, etaDep, Ringer::EtDependent,\
392 configDir, version) \
394 case Ringer::EtIndependent: \
395 READ_ETA_DEP_WRAPPER(vec, wrapType, segType, etaDep, \
396 Ringer::EtIndependent, configDir, version) \
399 throw std::runtime_error(std::string("Unknown Et dependency.")); \
402 // =============================================================================
403 #define READ_ALL_DEP_WRAPPER(vec, wrapType, segType, etaDep, etDep, \
404 configDir, version ) \
407 case Ringer::NoSegmentation: \
408 READ_ETA_ET_DEP_WRAPPER(vec, wrapType, Ringer::NoSegmentation, etaDep, \
409 etDep, configDir, version ) \
411 case Ringer::TrackCalPatTypeSegmentation: \
412 READ_ETA_ET_DEP_WRAPPER(vec, wrapType, \
413 Ringer::TrackCalPatTypeSegmentation, \
414 etaDep, etDep, configDir, version ) \
416 case Ringer::TrackCalSegmentation: \
417 READ_ETA_ET_DEP_WRAPPER(vec, wrapType, \
418 Ringer::TrackCalSegmentation, etaDep, \
419 etDep, configDir, version ) \
421 case Ringer::TrackCalJointLayers: \
422 READ_ETA_ET_DEP_WRAPPER(vec, wrapType, \
423 Ringer::TrackCalJointLayers, etaDep, \
424 etDep, configDir, version) \
426 case Ringer::TrackCalJointSections: \
427 READ_ETA_ET_DEP_WRAPPER(vec, wrapType, \
428 Ringer::TrackCalJointSections, etaDep, \
429 etDep, configDir, version) \
432 throw std::runtime_error(std::string("Unknown segmentation type")); \
436 } // WrapperUtils namespace
438 } // Ringer namespace
440 #include "RingerSelectorTools/procedures/RingerPreProcessorWrapper.icc"
441 #include "RingerSelectorTools/procedures/RingerDiscriminatorWrapper.icc"
442 #include "RingerSelectorTools/procedures/RingerThresholdWrapper.icc"
444 #endif // RINGERSELECTORTOOLS_PROCEDURES_RINGERPROCEDUREWRAPPER_ICC