ATLAS Offline Software
SCTOverlay.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
7 
9 
10 #include "StoreGate/ReadHandle.h"
11 #include "StoreGate/WriteHandle.h"
12 
13 #include "AthAllocators/DataPool.h"
14 
15 namespace Overlay
16 {
17  // Specialize copyCollection() for the SCT
18  template<>
19  std::unique_ptr<SCT_RDO_Collection> copyCollection(const IdentifierHash &hashId,
20  const SCT_RDO_Collection *collection,
21  DataPool<SCT3_RawData>& dataItems)
22  {
23  auto outputCollection = std::make_unique<SCT_RDO_Collection>(hashId);
24  outputCollection->setIdentifier(collection->identify());
25 
26  // Elements created here are owned by the DataPool
28  outputCollection->reserve(collection->size());
29 
30  for (const SCT_RDORawData *existingDatum : *collection) {
31  auto *oldDatum = dynamic_cast<const SCT3_RawData *>(existingDatum);
32  if (not oldDatum) {
33  throw std::runtime_error("Dynamic cast to SCT3_RawData failed in SCTOverlay.cxx, Overlay::copyCollection");
34  }
35  SCT3_RawData* datumCopy = dataItems.nextElementPtr();
36  (*datumCopy) = SCT3_RawData(oldDatum->identify(), oldDatum->getWord(),
37  &oldDatum->getErrorCondensedHit());
38  outputCollection->push_back(datumCopy);
39  }
40 
41  return outputCollection;
42  }
43 
44  // Specialize mergeCollections() for the SCT
45  // bkg,signal and output collections
46  // are all VIEW containers.
47  template<>
48  void mergeCollections(SCT_RDO_Collection *bkgCollection,
49  SCT_RDO_Collection *signalCollection,
52  DataPool<SCT3_RawData>& dataItems)
53  {
54  // We want to use the SCT_ID helper provided by SCTOverlay, thus the constraint
55  const SCTOverlay *parent = dynamic_cast<const SCTOverlay *>(algorithm);
56  if (!parent) {
57  throw std::runtime_error("mergeCollections<SCT_RDORawData>() called by a wrong parent algorithm? Must be SCTOverlay.");
58  }
59 
60  if (bkgCollection->identify() != signalCollection->identify()) {
61  throw std::runtime_error("mergeCollections<SCT_RDO_Collection>(): collection Id mismatch");
62  }
63 
64  outputCollection->reserve(
65  std::max(bkgCollection->size(), signalCollection->size()));
66  const Identifier idColl =
67  parent->get_sct_id()->wafer_id(signalCollection->identifyHash());
68 
69  // Strip hit timing information for Next, Current, Previous and Any BCs
70  // Prepare one more strip to create the last one. The additional strip has no hits.
71  std::bitset<SCTOverlay::NumberOfStrips+1> stripInfo[SCTOverlay::NumberOfBitSets];
72  // Process background and signal in the wafer
73  for (unsigned source = SCTOverlay::BkgSource; source < SCTOverlay::NumberOfSources; source++) {
76  if (source == SCTOverlay::BkgSource) { // background
77  rdo = bkgCollection->begin();
78  rdoEnd = bkgCollection->end();
79  } else { // signal
80  rdo = signalCollection->begin();
81  rdoEnd = signalCollection->end();
82  }
83  // Loop over all RDOs in the wafer
84  for (; rdo!=rdoEnd; ++rdo) {
85  // Here we'll just go with SCT3.
86  const SCT3_RawData* rdo3 = dynamic_cast<const SCT3_RawData*>(*rdo);
87  if (!rdo3) {
88  std::ostringstream os;
89  const auto& elt = **rdo;
90  os << "mergeCollection<SCT_RDO_Collection>(): wrong datum format. Only SCT3_RawData are produced by SCT_RodDecoder and supported by overlay."
91  << "For the supplied datum typeid(datum).name() = " << typeid(elt).name();
92  throw std::runtime_error(os.str());
93  }
94  int strip = parent->get_sct_id()->strip(rdo3->identify());
95  int stripEnd = strip + rdo3->getGroupSize();
96  int timeBin = rdo3->getTimeBin();
97  for (; strip < stripEnd && strip < SCTOverlay::NumberOfStrips; strip++) {
98  // Fill timing information for each strips, loop over 3 BCs
99  for (unsigned int bc = SCTOverlay::NextBC; bc < SCTOverlay::NumberOfBCs; bc++) {
100  if (timeBin & (1 << bc)) stripInfo[bc].set(strip);
101  }
102  }
103  }
104  }
105  // Get OR for AnyBC, loop over 3 BCs
106  for (unsigned int bc = SCTOverlay::NextBC; bc < SCTOverlay::NumberOfBCs; bc++) {
107  stripInfo[SCTOverlay::AnyBC] |= stripInfo[bc];
108  }
109  // Check if we need to use Expanded mode by checking if there is at least one hit in Next BC or Previous BC
110  bool anyNextBCHits = stripInfo[SCTOverlay::NextBC].any();
111  bool anyPreivousBCHits = stripInfo[SCTOverlay::PreviousBC].any();
112  bool isExpandedMode = (anyNextBCHits or anyPreivousBCHits);
113  // No error information is recorded because this information is not filled in data and no errors are assumed in MC.
114  const int ERRORS = 0;
115  const std::vector<int> errvec{};
116  if (isExpandedMode) {
117  // Expanded mode (record strip one by one)
118  const int groupSize = 1;
119  int tbin = 0;
120  for (unsigned int strip = 0; strip < SCTOverlay::NumberOfStrips; strip++) {
121  if (stripInfo[SCTOverlay::AnyBC][strip]) {
122  tbin = 0;
123  for (unsigned int bc = SCTOverlay::NextBC; bc < SCTOverlay::NumberOfBCs; bc++) {
124  if (stripInfo[bc][strip]) {
125  tbin |= (1 << bc);
126  }
127  }
128  unsigned int SCT_Word = (groupSize | (strip << 11) | (tbin <<22) | (ERRORS << 25));
129  Identifier rdoId = parent->get_sct_id()->strip_id(idColl, strip) ;
130  // use the pool
131  SCT3_RawData* datum = dataItems.nextElementPtr();
132  (*datum) = SCT3_RawData(rdoId, SCT_Word, &errvec);
133  outputCollection->push_back(datum);
134  }
135  }
136  } else {
137  // We can record consecutive hits into one RDO if all hits have timeBin of 010.
138  unsigned int groupSize = 0;
139  const int tbin = (1 << SCTOverlay::CurrentBC);
140  for (unsigned int strip=0; strip<SCTOverlay::NumberOfStrips+1; strip++) { // Loop over one more strip to create the last one if any
141  if (stripInfo[SCTOverlay::AnyBC][strip]) {
142  groupSize++;
143  } else {
144  if (groupSize>0) {
145  unsigned int firstStrip = strip - groupSize;
146  unsigned int SCT_Word = (groupSize | (firstStrip << 11) | (tbin <<22) | (ERRORS << 25));
147  Identifier rdoId = parent->get_sct_id()->strip_id(idColl, firstStrip) ;
148  SCT3_RawData* datum = dataItems.nextElementPtr();
149  (*datum) = SCT3_RawData(rdoId, SCT_Word, &errvec);
150  outputCollection->push_back(datum);
151  groupSize = 0;
152  }
153  }
154  }
155  }
156  } // mergeCollections()
157 } // namespace Overlay
158 
159 
160 SCTOverlay::SCTOverlay(const std::string &name, ISvcLocator *pSvcLocator)
161  : IDC_OverlayBase(name, pSvcLocator)
162 {
163 }
164 
166 {
167  ATH_MSG_DEBUG("Initializing...");
168 
169  // Check and initialize keys
170  ATH_CHECK( m_bkgInputKey.initialize(!m_bkgInputKey.key().empty()) );
171  ATH_MSG_VERBOSE("Initialized ReadHandleKey: " << m_bkgInputKey);
172  ATH_CHECK( m_signalInputKey.initialize() );
173  ATH_MSG_VERBOSE("Initialized ReadHandleKey: " << m_signalInputKey);
174  ATH_CHECK( m_outputKey.initialize() );
175  ATH_MSG_VERBOSE("Initialized WriteHandleKey: " << m_outputKey);
176 
177  // Retrieve SCT ID helper
178  if (!detStore()->retrieve(m_sctId, "SCT_ID").isSuccess() || !m_sctId) {
179  ATH_MSG_FATAL("Cannot retrieve SCT ID helper");
180  return StatusCode::FAILURE;
181  }
182 
183  return StatusCode::SUCCESS;
184 }
185 
186 StatusCode SCTOverlay::execute(const EventContext& ctx) const
187 {
188  ATH_MSG_DEBUG("execute() begin");
189 
190  // Reading the input RDOs
191  ATH_MSG_VERBOSE("Retrieving input RDO containers");
192 
193  const SCT_RDO_Container *bkgContainerPtr = nullptr;
194  if (!m_bkgInputKey.empty()) {
196  if (!bkgContainer.isValid()) {
197  ATH_MSG_ERROR("Could not get background SCT RDO container " << bkgContainer.name() << " from store " << bkgContainer.store());
198  return StatusCode::FAILURE;
199  }
200  bkgContainerPtr = bkgContainer.cptr();
201 
202  ATH_MSG_DEBUG("Found background SCT RDO container " << bkgContainer.name() << " in store " << bkgContainer.store());
203  ATH_MSG_DEBUG("SCT Background = " << Overlay::debugPrint(bkgContainer.cptr(), 50));
204  }
205 
207  if (!signalContainer.isValid()) {
208  ATH_MSG_ERROR("Could not get signal SCT RDO container " << signalContainer.name() << " from store " << signalContainer.store());
209  return StatusCode::FAILURE;
210  }
211  ATH_MSG_DEBUG("Found signal SCT RDO container " << signalContainer.name() << " in store " << signalContainer.store());
212  ATH_MSG_DEBUG("SCT Signal = " << Overlay::debugPrint(signalContainer.cptr(), 50));
213 
214  // Creating output RDO container
215  SG::WriteHandle<SCT_RDO_Container> outputContainer(m_outputKey, ctx);
216 
217 
218  ATH_CHECK(outputContainer.record(std::make_unique<SCT_RDO_Container>(signalContainer->size())));
219  if (!outputContainer.isValid()) {
220  ATH_MSG_ERROR("Could not record output SCT RDO container " << outputContainer.name() << " to store " << outputContainer.store());
221  return StatusCode::FAILURE;
222  }
223  ATH_MSG_DEBUG("Recorded output SCT RDO container " << outputContainer.name() << " in store " << outputContainer.store());
224 
225  // The DataPool, this is what will actually own the elements
226  // we create during this algorithm. The containers are views.
227  DataPool<SCT3_RawData> dataItemsPool(ctx);
228  // It resizes but lets reserve already quite a few
229  dataItemsPool.prepareToAdd(250000);
230 
231  ATH_CHECK(overlayContainer(bkgContainerPtr, signalContainer.cptr(), outputContainer.ptr(),dataItemsPool));
232  ATH_MSG_DEBUG("SCT Result = " << Overlay::debugPrint(outputContainer.ptr(), 50));
233 
234  ATH_MSG_DEBUG("execute() end");
235  return StatusCode::SUCCESS;
236 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
SCTOverlay::BkgSource
@ BkgSource
Definition: SCTOverlay.h:29
SCT3_RawData::getGroupSize
virtual int getGroupSize() const override final
Definition: SCT3_RawData.h:86
SCTOverlay::m_signalInputKey
SG::ReadHandleKey< SCT_RDO_Container > m_signalInputKey
Definition: SCTOverlay.h:35
IDC_OverlayHelpers.h
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
DataModel_detail::const_iterator
Const iterator class for DataVector/DataList.
Definition: DVLIterator.h:82
SCT_ID.h
This is an Identifier helper class for the SCT subdetector. This class is a factory for creating comp...
algorithm
std::string algorithm
Definition: hcg.cxx:82
max
#define max(a, b)
Definition: cfImp.cxx:41
SCTOverlay::execute
virtual StatusCode execute(const EventContext &ctx) const override final
Definition: SCTOverlay.cxx:186
SG::ReadHandle::cptr
const_pointer_type cptr()
Dereference the pointer.
SG::VIEW_ELEMENTS
@ VIEW_ELEMENTS
this data object is a view, it does not own its elmts
Definition: OwnershipPolicy.h:18
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
SG::VarHandleBase::name
const std::string & name() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleBase.cxx:75
SCTOverlay::NextBC
@ NextBC
Definition: SCTOverlay.h:27
InDetRawDataCollection::identify
virtual Identifier identify() const override final
IdentifiableContainerMT::size
size_t size() const
Duplicate of fullSize for backwards compatability.
Definition: IdentifiableContainerMT.h:209
SCT_RDORawData
Definition: SCT_RDORawData.h:24
InDetRawDataContainer
Definition: InDetRawDataContainer.h:27
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
SCTOverlay::NumberOfStrips
@ NumberOfStrips
Definition: SCTOverlay.h:28
SCTOverlay::m_sctId
const SCT_ID * m_sctId
Definition: SCTOverlay.h:32
SCTOverlay::NumberOfSources
@ NumberOfSources
Definition: SCTOverlay.h:29
AthCommonDataStore< AthCommonMsg< Gaudi::Algorithm > >::detStore
const ServiceHandle< StoreGateSvc > & detStore() const
The standard StoreGateSvc/DetectorStore Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:95
InDetRawDataCollection::identifyHash
virtual IdentifierHash identifyHash() const override final
SCTOverlay::NumberOfBitSets
@ NumberOfBitSets
Definition: SCTOverlay.h:28
WriteHandle.h
Handle class for recording to StoreGate.
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
SCTOverlay::CurrentBC
@ CurrentBC
Definition: SCTOverlay.h:27
SCT3_RawData
Definition: SCT3_RawData.h:24
Overlay::copyCollection
std::unique_ptr< HGTD_RDO_Collection > copyCollection(const IdentifierHash &hashId, const HGTD_RDO_Collection *collection)
Definition: HGTD_Overlay.cxx:24
Identifier
Definition: DetectorDescription/Identifier/Identifier/Identifier.h:32
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
SG::WriteHandle::ptr
pointer_type ptr()
Dereference the pointer.
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
SG::VarHandleBase::store
std::string store() const
Return the name of the store holding the object we are proxying.
Definition: StoreGate/src/VarHandleBase.cxx:379
SCTOverlay
Definition: SCTOverlay.h:14
InDetRawDataCollection
Definition: InDetRawDataCollection.h:31
IDC_OverlayBase::overlayContainer
StatusCode overlayContainer(const IDC_Container *bkgContainer, const IDC_Container *signalContainer, IDC_Container *outputContainer) const
test_pyathena.parent
parent
Definition: test_pyathena.py:15
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
SCTOverlay::m_outputKey
SG::WriteHandleKey< SCT_RDO_Container > m_outputKey
Definition: SCTOverlay.h:36
SCT3_RawData::getTimeBin
int getTimeBin() const
Definition: SCT3_RawData.h:92
SCTOverlay::m_bkgInputKey
SG::ReadHandleKey< SCT_RDO_Container > m_bkgInputKey
Definition: SCTOverlay.h:34
DataPool::nextElementPtr
pointer nextElementPtr()
obtain the next available element in pool by pointer pool is resized if its limit has been reached On...
ReadFromCoolCompare.os
os
Definition: ReadFromCoolCompare.py:231
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
DataPool.h
postInclude.outputCollection
outputCollection
Definition: postInclude.SortInput.py:27
SG::WriteHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
SCTOverlay::AnyBC
@ AnyBC
Definition: SCTOverlay.h:27
DataVector::end
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
Overlay::mergeCollections
void mergeCollections(SCT_RDO_Collection *bkgCollection, SCT_RDO_Collection *signalCollection, SCT_RDO_Collection *outputCollection, const IDC_OverlayBase *algorithm, DataPool< SCT3_RawData > &dataItems)
Definition: SCTOverlay.cxx:48
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
SCTOverlay::SCTOverlay
SCTOverlay(const std::string &name, ISvcLocator *pSvcLocator)
Definition: SCTOverlay.cxx:160
SG::WriteHandle::record
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
SCTOverlay.h
Overlay
Helpers for overlaying Identifiable Containers.
Definition: HGTD_Overlay.cxx:11
DataPool
a typed memory pool that saves time spent allocation small object. This is typically used by containe...
Definition: DataPool.h:47
IDC_OverlayBase
Definition: IDC_OverlayBase.h:25
SCTOverlay::NumberOfBCs
@ NumberOfBCs
Definition: SCTOverlay.h:27
SCT_CalibAlgs::firstStrip
@ firstStrip
Definition: SCT_CalibNumbers.h:10
InDetRawData::identify
virtual Identifier identify() const override final
Definition: InDetRawData.h:41
DataPool::prepareToAdd
void prepareToAdd(unsigned int size)
Prepare to add cached elements.
SCTOverlay::PreviousBC
@ PreviousBC
Definition: SCTOverlay.h:27
ReadHandle.h
Handle class for reading from StoreGate.
IdentifierHash
Definition: IdentifierHash.h:38
Overlay::debugPrint
std::string debugPrint(const IDC_Container *container, unsigned numprint=25)
Diagnostic output of Identifiable Containers.
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
DataVector::begin
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
SCTOverlay::initialize
virtual StatusCode initialize() override final
Definition: SCTOverlay.cxx:165