ATLAS Offline Software
Loading...
Searching...
No Matches
CalibrationHitMerger.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3*/
4
6
8LArG4::CalibrationHitMerger::CalibrationHitMerger( const std::string& name, ISvcLocator* pSvcLocator ) :
9 ::AthAlgorithm( name, pSvcLocator )
10{}
11
14{
15 ATH_CHECK(m_inputHits.initialize());
16 ATH_CHECK(m_outputHits.initialize());
17 return StatusCode::SUCCESS;
18}
19
20
23{
24 // skip if not input collection
25 if ( m_inputHits.empty() ) {
26 return StatusCode::SUCCESS;
27 }
28 // TODO: is there a way to conveniently get the total number of hits in all m_inputHits
29 // and reserve the corresponding size in the outputHandle
31 ATH_CHECK( outputHandle.record(std::make_unique<CaloCalibrationHitContainer>()) );
32 calibrationHits_t calibrationHits;
33 unsigned int nHitsMerged(0);
34 for ( const auto& collKey: m_inputHits ) {
36 ATH_MSG_DEBUG(inputHandle.name() << " has " << inputHandle->size() << " hits.");
37 for ( const CaloCalibrationHit* hit: *inputHandle ) {
38 // If we haven't had a hit in this cell before, create one and add
39 // it to the hit collection.
40
41 // If we've had a hit in this cell before, then add the energy to
42 // the existing hit.
43
44 // Look for the key in the hitCollection (this is a binary search).
45 auto bookmark = calibrationHits.lower_bound(hit);
46
47 // The lower_bound method of a map finds the first element
48 // whose key is not less than the identifier. If this element
49 // == our hit, we've found a match.
50
51 // Reminders:
52 // bookmark = iterator (pointer) into the hitCollection set.
53 // (*bookmark) = a member of the set, which is a LArG4Hit*.
54
55 // Equals() is a function defined in LArG4Hit.h; it has the value of
56 // "true" when a LArG4Hit* points to the same identifier.
57
58 if (bookmark == calibrationHits.end() ||
59 !(*bookmark)->Equals(hit)) {
60 auto&& hitCopy = std::make_unique<CaloCalibrationHit>(*hit);
61 // We haven't had a hit in this readout cell before. Add it
62 // to our set.
63 if (calibrationHits.empty() ||
64 bookmark == calibrationHits.begin()) {
65 // Insert the hit before the first entry in the map.
66 calibrationHits.insert(hitCopy.release());
67 } else {
68 // We'just done a binary search of hitCollection, so we should use
69 // the results of that search to speed up the insertion of a new
70 // hit into the map. The "insert" method is faster if the new
71 // entry is right _after_ the bookmark. If we left bookmark
72 // unchanged, the new entry would go right _before_ the
73 // bookmark. We therefore want to decrement the bookmark from
74 // the lower_bound search.
75 calibrationHits.insert(--bookmark, hitCopy.release());
76 }
77 } else {
78 ++nHitsMerged;
79 // Update the existing hit.
80 (*bookmark)->Add(hit);
81 }
82 }
83 }
84
85 // Loop through the hits...
86 for(auto *hit : calibrationHits)
87 {
88 // Can we actually do this with move?
89 outputHandle->push_back(hit);
90 } // End of loop over hits
91 // Clean up
92 calibrationHits.clear();
93 ATH_MSG_DEBUG(outputHandle.name() << " has " << outputHandle->size() << " hits. Merged " << nHitsMerged << " hits.");
94 return StatusCode::SUCCESS;
95}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_DEBUG(x)
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
Class to store calorimeter calibration hit.
virtual StatusCode execute() override final
Athena Algorithm execute.
std::set< CaloCalibrationHit *, LessHit > calibrationHits_t
SG::WriteHandleKey< CaloCalibrationHitContainer > m_outputHits
SG::ReadHandleKeyArray< CaloCalibrationHitContainer > m_inputHits
CalibrationHitMerger(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
virtual StatusCode initialize() override final
Athena algorithm's interface methods.
const std::string & name() const
Return the StoreGate ID for the referenced object.
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.