ATLAS Offline Software
RoIBResultByteStreamCnv.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef TRIGT1RESULTBYTESTREAM_ROIBRESULTBYTESTREAMCNV_ICC
6 #define TRIGT1RESULTBYTESTREAM_ROIBRESULTBYTESTREAMCNV_ICC
7 
8 // Trigger includes
9 #include "TrigT1Result/RoIBResult.h"
10 
11 // Athena includes
12 #include "ByteStreamCnvSvcBase/ByteStreamAddress.h"
13 #include "ByteStreamData/RawEvent.h"
14 #include "AthenaKernel/StorableConversions.h"
15 
16 // Gaudi includes
17 #include "GaudiKernel/ConcurrencyFlags.h"
18 
19 /**
20  * The constructor sets up all the ToolHandle and ServiceHandle objects and initialises the
21  * base class in the correct way.
22  */
23 template< class ROBF >
24 RoIBResultByteStreamCnv< ROBF >::RoIBResultByteStreamCnv( ISvcLocator* svcloc )
25  : Converter( storageType(), classID(), svcloc ),
26  AthMessaging( msgSvc(), "RoIBResultByteStreamCnv" ),
27  m_tool( "RoIBResultByteStreamTool/RoIBResultBSEncoderTool" ),
28  m_ByteStreamCnvSvc( "ByteStreamCnvSvc", "RoIBResultByteStreamCnv" ),
29  m_robDataProviderSvc( "ROBDataProviderSvc", "RoIBResultByteStreamCnv" ) {
30 
31 }
32 
33 /**
34  * Function telling the framework the Class ID of the object that this converter
35  * is for (RoIBResult).
36  */
37 template< class ROBF >
38 const CLID& RoIBResultByteStreamCnv< ROBF >::classID(){
39  return ClassID_traits< ROIB::RoIBResult >::ID();
40 }
41 
42 template< class ROBF >
43 long RoIBResultByteStreamCnv< ROBF >::storageType() {
44  return ByteStreamAddress::storageType();
45 }
46 
47 /**
48  * Init method gets all necessary services etc.
49  */
50 template< class ROBF >
51 StatusCode RoIBResultByteStreamCnv< ROBF >::initialize() {
52  //
53  // Initialise the base class:
54  //
55  ATH_CHECK( Converter::initialize() );
56  ATH_MSG_DEBUG("In initialize()");
57 
58  //
59  // Get ByteStreamCnvSvc:
60  //
61  ATH_CHECK( m_ByteStreamCnvSvc.retrieve() );
62  ATH_MSG_DEBUG("Connected to ByteStreamCnvSvc");
63 
64  //
65  // Get ROBDataProviderSvc:
66  //
67  ATH_CHECK( m_robDataProviderSvc.retrieve() );
68  ATH_MSG_DEBUG("Connected to ROBDataProviderSvc");
69 
70  //
71  // Get RoIBResultByteStreamTool:
72  //
73  ATH_CHECK( m_tool.retrieve() );
74  ATH_MSG_DEBUG("Connected to RoIBResultByteStreamTool");
75 
76  //
77  // Flag if running in AthenaMT
78  //
79  m_isMT = (Gaudi::Concurrency::ConcurrencyFlags::numThreads() + Gaudi::Concurrency::ConcurrencyFlags::numConcurrentEvents()) > 0;
80  if (m_isMT) ATH_MSG_DEBUG("Detected running in AthenaMT");
81  else ATH_MSG_DEBUG("Detected running in legacy Athena");
82 
83  return StatusCode::SUCCESS;
84 }
85 
86 /**
87  * This function creates the RoIBResult object from the BS data. It collects all the
88  * ROB fragments coming from the RoI Builder and gives them to RoIBResultByteStreamTool
89  * for conversion.
90  */
91 template< class ROBF >
92 StatusCode RoIBResultByteStreamCnv< ROBF >::createObj( IOpaqueAddress* /*pAddr*/, DataObject*& /*pObj*/ ) {
93  ATH_MSG_ERROR("Misconfiguration! Decoding ROIB::RoIBResult from ByteStream using a Converter is deprecated. "
94  << "Use the L1TriggerByteStreamDecoderAlg configured with RoIBResultByteStreamTool instead.");
95  return StatusCode::FAILURE;
96 }
97 
98 /**
99  * This function receives an RoIBResult object as input, and adds all the ROB fragments
100  * of the RoI Builder to the current raw event using the IByteStreamEventAccess
101  * interface.
102  */
103 template< class ROBF >
104 StatusCode RoIBResultByteStreamCnv< ROBF >::createRep( DataObject* pObj, IOpaqueAddress*& pAddr ) {
105  ATH_MSG_DEBUG("createRep() called");
106 
107  // Create a ByteStreamAddress for RoIBResult
108  ROIB::RoIBResult* result;
109  if( ! SG::fromStorable( pObj, result ) ) {
110  ATH_MSG_ERROR("Failed to convert DataObject to RoIBResult");
111  return StatusCode::FAILURE;
112  }
113 
114  if ( pAddr != nullptr ) pAddr->release();
115  ByteStreamAddress* bsAddr = new ByteStreamAddress( classID(), pObj->registry()->name(), "" );
116  pAddr = static_cast<IOpaqueAddress*>(bsAddr);
117  pAddr->addRef();
118 
119  // Retrieve the raw event pointer
120  RawEventWrite* re = m_ByteStreamCnvSvc->getRawEvent();
121  if (!re) {
122  ATH_MSG_ERROR("Failed to obtain a pointer to RawEventWrite");
123  return StatusCode::FAILURE;
124  }
125  ATH_MSG_VERBOSE("Obtained RawEventWrite pointer = " << re);
126 
127  // Use the IL1TriggerByteStreamTool interface of the tool to convert RoIBResult to ByteStream.
128  // NOTE: the pObj passed as input to this function is actually not passed to the tool. Instead,
129  // the tool retrieves the RoIBResult from event store using a ReadHandle which ensures correct
130  // data dependencies and integration with other uses of the same interface.
131  std::vector<OFFLINE_FRAGMENTS_NAMESPACE_WRITE::ROBFragment*> vrobf;
132  ATH_CHECK(m_tool->convertToBS(vrobf, Gaudi::Hive::currentContext())); // TODO: find a way to avoid ThreadLocalContext
133  ATH_MSG_DEBUG("Created " << vrobf.size() << " ROB Fragments");
134  for (OFFLINE_FRAGMENTS_NAMESPACE_WRITE::ROBFragment* rob : vrobf) {
135  // Set LVL1 Trigger Type from the full event
136  rob->rod_lvl1_type(re->lvl1_trigger_type());
137  // Set LVL1 ID from the full event
138  rob->rod_lvl1_id(re->lvl1_id());
139  // Add the ROBFragment to the full event
140  re->append(rob);
141  ATH_MSG_DEBUG("Added ROB fragment " << MSG::hex << rob->source_id() << MSG::dec << " to the output raw event");
142  }
143 
144  return StatusCode::SUCCESS;
145 
146 }
147 
148 #endif // TRIGT1RESULTBYTESTREAM_ROIBRESULTBYTESTREAMCNV_ICC