ATLAS Offline Software
Loading...
Searching...
No Matches
LArRawDataContByteStreamTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3*/
4
8
10
16
24#include "GaudiKernel/MsgStream.h"
25#include "GaudiKernel/ThreadLocalContext.h"
26
28
29// STL stuff
30#include <map>
31
32
33static const InterfaceID IID_ILArRawDataContByteStreamTool
34 ("LArRawDataContByteStreamTool", 1, 0);
35
36
39
40// default contructor
41
43( const std::string& type, const std::string& name,const IInterface* parent )
44 : AthAlgTool(type,name,parent)
45{ declareInterface< LArRawDataContByteStreamTool >( this );
46 declareProperty("DSPRunMode",m_DSPRunMode=4);
47 declareProperty("RodBlockVersion",m_RodBlockVersion=0);
48 declareProperty("FebNoiseCut",m_nfebsigma=2);
49 declareProperty("InitializeForWriting",m_initializeForWriting=false);
50 declareProperty("SubDetectorId",m_subDetId=0);
51 declareProperty("IncludeDigits",m_includeDigits=false);
52 declareProperty("DigitsContainer",m_DigitContName="LArDigitContainer_MC_Thinned");
53}
54
57
58
59StatusCode
61{
62 ATH_CHECK( AthAlgTool::initialize() );
63 ATH_MSG_DEBUG ( "Initializing LArRawDataContByteStream" );
64
65 ATH_CHECK( m_decoder.retrieve() );
66
68 if (m_DSPRunMode == 0) {
69 // Obsolete mode
70 ATH_MSG_ERROR ( "LArRodBlockStructure type 0 is obsolete and can't be used any more." );
71 return StatusCode::FAILURE;
72 }
73
74 ATH_CHECK( detStore()->retrieve (m_onlineHelper, "LArOnlineID") );
75
76 ATH_MSG_INFO ( "Initialization done for reading and writing" );
77 }
78 else {
79 ATH_MSG_INFO ( "Initialization done for reading only" );
80 }
81
86
87 return StatusCode::SUCCESS;
88}
89
90
91StatusCode
93{
94 ATH_CHECK( AthAlgTool::finalize() );
95 return StatusCode::SUCCESS;
96}
97
98
99StatusCode
101 FEA_t& fea) const
102{
104 ATH_MSG_ERROR ( "Tool not setup for writing! Use property "
105 << name()<<".InitializeForWriting" );
106 return StatusCode::FAILURE;
107 }
108 ATH_MSG_DEBUG ( "Writing LArDigitContainer to ByteStream" );
109 if (!digitCont) {
110 ATH_MSG_ERROR ( "Null pointer passed to WriteLArDigit routine!" );
111 return StatusCode::FAILURE;
112 }
113
114 std::unique_ptr<LArRodBlockStructure> blstruct = makeRodBlockStructure();
115
116 if (!blstruct->canSetRawData() && !blstruct->canSetRawDataFixed()) {
117 ATH_MSG_DEBUG ( "This instance of LArRodBlockStructure can't hold LArDigits!" );
118 return StatusCode::FAILURE;
119 }
120 //prepareWriting();
123 ATH_MSG_DEBUG ( "Setting Detector Event Type to " << m_DSPRunMode
124 << " and Minor Version Number to " << m_RodBlockVersion );
125 FEA_t::RODDATA* theROD;
127 LArDigitContainer::const_iterator it_e=digitCont->end();
128 if (it_b==it_e) {
129 ATH_MSG_WARNING ( "Attempt to persistify an empty LArDigitContainer to ByteStream" );
130 return StatusCode::SUCCESS;
131 }
132
133 const EventContext& ctx = Gaudi::Hive::currentContext();
137 const Hid2RESrcID& hid2re = getHid2RESrcID (**febRodMapping);
138
139 std::map<uint32_t, LArRodEncoder> mapEncoder;
140
141 auto getEncoder = [&] (uint32_t reid) -> LArRodEncoder&
142 { return mapEncoder.try_emplace (reid,
144 **caloMgr,
145 **onOffMapping,
146 blstruct.get()).first->second; };
147
148 unsigned n=0;
149 if (blstruct->canSetRawDataFixed() && checkGainConsistency(digitCont))
150 {//Set fixed gain raw data
151 int fixgain=(*it_b)->gain();
152 ATH_MSG_DEBUG(" number of Digits in LArDigitContainer for gain " << fixgain << ": "
153 << digitCont->size() );
154 // Sorting Channels by ROD
155 for(; it_b!=it_e; ++it_b){
156 const LArDigit* digit = *it_b;
157 HWIdentifier chid = digit->hardwareID() ;
158 uint32_t reid = hid2re.getRodID( **febRodMapping, chid );
159 getEncoder(reid).add (digit, fixgain);
160 n++;
161 }
162 ATH_MSG_VERBOSE(" number of channels in the LArDigitContainer for gain "
163 << fixgain << ": "<<n );
164 } // end if
165 else
166 if (blstruct->canSetRawData()) { //Set free gain raw data
167 ATH_MSG_DEBUG(" number of channels in LArDigit container: "<< digitCont->size() );
168
169 // Sorting Channels by ROD
170 for(; it_b!=it_e; ++it_b){
171 const LArDigit* digit = *it_b;
172 HWIdentifier chid = digit->hardwareID() ;
173 uint32_t reid = hid2re.getRodID( **febRodMapping, chid );
174 getEncoder(reid).add (digit);
175 n++;
176 }
177 ATH_MSG_VERBOSE(" number of channels added to framgent: "<<n );
178 }// end else-if(can set Raw data)
179
181
182 // Now loop over map and fill all ROD Data Blocks
183 std::map<uint32_t,LArRodEncoder>::iterator it =mapEncoder.begin();
184 std::map<uint32_t,LArRodEncoder>::iterator it_end=mapEncoder.end();
185 // LArRodEncoder has collected all the channels, now can fill the
186 // ROD block data.
187 for(; it!=it_end;++it) {
188 theROD = fea.getRodData( (*it).first );
189 ((*it).second).fillROD(*theROD,msg(), **noise, m_nfebsigma ) ;
190 }
191 ATH_MSG_DEBUG ( "Filled " << mapEncoder.size() << " Rod Blocks" );
192 // Finally, fill full event
193 //fea.fill(re,log);
194 return StatusCode::SUCCESS;
195}
196
197
198
199StatusCode
201 FEA_t& fea) const
202{
204 ATH_MSG_ERROR ( "Tool not setup for writing! Use property "
205 << name()<<".InitializeForWriting" );
206 return StatusCode::FAILURE;
207 }
208 ATH_MSG_DEBUG ( "Writing LArCalibDigitContainer to ByteStream" );
209 if (!digitCont) {
210 ATH_MSG_DEBUG ( "Null pointer passed to WriteLArCalibDigit routine!" );
211 return StatusCode::FAILURE;
212 }
213
214 std::unique_ptr<LArRodBlockStructure> blstruct = makeRodBlockStructure();
215
216 if (!blstruct->canSetCalibration()|| !blstruct->canSetRawDataFixed()) {
217 ATH_MSG_DEBUG ( "This instance of LArRodBlockStructure can't hold LArCalibDigits!" );
218 return StatusCode::FAILURE;
219 }
220 //prepareWriting();
223 ATH_MSG_DEBUG ( "Setting Detector Event Type to " << m_DSPRunMode
224 << "and Minor Version Number to " << m_RodBlockVersion );
225 FEA_t::RODDATA* theROD;
228 if (it_b==it_e) {
229 ATH_MSG_WARNING ( "Attempt to persistify a empty LArDigitContainer to ByteStream" );
230 return StatusCode::SUCCESS;
231 }
232 if (!checkGainConsistency(digitCont)) {
233 ATH_MSG_ERROR ( "Inconsistent gain in LArCalibDigitContainer" );
234 return StatusCode::FAILURE;
235 }
236
237 const EventContext& ctx = Gaudi::Hive::currentContext();
241 const Hid2RESrcID& hid2re = getHid2RESrcID (**febRodMapping);
242
243 std::map<uint32_t, LArRodEncoder> mapEncoder;
244
245 auto getEncoder = [&] (uint32_t reid) -> LArRodEncoder&
246 { return mapEncoder.try_emplace (reid,
248 **caloMgr,
249 **onOffMapping,
250 blstruct.get()).first->second; };
251
252 unsigned n=0;
253 int fixgain=(*it_b)->gain();
254
255 // Sorting Channels by ROD
256 for(; it_b!=it_e; ++it_b){
257 const LArCalibDigit* digit = *it_b;
258 HWIdentifier chid = digit->hardwareID() ;
259 uint32_t reid = hid2re.getRodID( **febRodMapping, chid );
260 getEncoder(reid).add (digit, fixgain);
261 n++;
262 }
263
264 ATH_MSG_VERBOSE(" number of channels in the LArCalibDigitContainer for gain "
265 << fixgain << ": "<<n );
266
268
269 // Now loop over map and fill all ROD Data Blocks
270 std::map<uint32_t,LArRodEncoder>::iterator it =mapEncoder.begin();
271 std::map<uint32_t,LArRodEncoder>::iterator it_end=mapEncoder.end();
272 // LArRodEncoder has collected all the channels, now can fill the
273 // ROD block data.
274 for(; it!=it_end;++it) {
275 theROD = fea.getRodData( (*it).first );
276 ((*it).second).fillROD( *theROD,msg(), **noise, m_nfebsigma ) ;
277 }
278 ATH_MSG_DEBUG ( "Filled " << mapEncoder.size() << " Rod Blocks" );
279 return StatusCode::SUCCESS;
280}
281
282
283StatusCode
285 FEA_t& fea) const
286{
288 ATH_MSG_ERROR ( "Tool not setup for writing! Use property "
289 << name()<<".InitializeForWriting" );
290 return StatusCode::FAILURE;
291 }
292 if (!channelCont) {
293 ATH_MSG_DEBUG ( "Null pointer passed to WriteLArCalibDigit routine!" );
294 return StatusCode::FAILURE;
295 }
296
297 std::unique_ptr<LArRodBlockStructure> blstruct = makeRodBlockStructure();
298
299 if (!blstruct->canSetEnergy()) {
300 ATH_MSG_DEBUG ( "This instance of LArRodBlockStructure can't hold LArRawChannels!" );
301 return StatusCode::FAILURE;
302 }
303 FEA_t::RODDATA* theROD;
306 ATH_MSG_DEBUG ( "Setting Detector Event Type to " << m_DSPRunMode
307 << "and Minor Version Number to " << m_RodBlockVersion );
308 LArRawChannelContainer::const_iterator it = channelCont->begin();
309 LArRawChannelContainer::const_iterator it_e= channelCont->end();
310 if ( it==it_e) {
311 ATH_MSG_WARNING ( "Attempt to persistify a empty LArDigitContainer to ByteStream" );
312 return StatusCode::SUCCESS;
313 }
314
315 const EventContext& ctx = Gaudi::Hive::currentContext();
319 const Hid2RESrcID& hid2re = getHid2RESrcID (**febRodMapping);
320
321 std::map<uint32_t, LArRodEncoder> mapEncoder;
322
323 auto getEncoder = [&] (uint32_t reid) -> LArRodEncoder&
324 { return mapEncoder.try_emplace (reid,
326 **caloMgr,
327 **onOffMapping,
328 blstruct.get()).first->second; };
329
330 //LArRodEncoder* Encoder = NULL;
331 //uint32_t last_reid(0x0);
332 //unsigned n=0; //For debug only
333 ATH_MSG_DEBUG(" number of LArRawChannel container "<< channelCont->size() );
334 for(; it!=it_e; ++it){
335 const LArRawChannel& rawChan = *it;
336 HWIdentifier chid = rawChan.channelID() ;
337 uint32_t reid = hid2re.getRodID( **febRodMapping, chid );
338
339/*
340 if ( reid != last_reid ) {
341 last_reid = reid;
342 // The guy does not exist
343 // This will create it
344 getEncoder(reid).add(&rawChan);
345 // This will get its address
346 Encoder = &(mapEncoder[reid]);
347 } else Encoder->add(&rawChan) ; // Encoder already there
348*/
349 getEncoder(reid).add (&rawChan);
350 }
351 // I may want to also include the digits
352 if ( m_includeDigits ) {
353 const LArDigitContainer* digitCont = nullptr;
354 if ( evtStore()->retrieve(digitCont,m_DigitContName).isFailure() ){
355 ATH_MSG_ERROR ( "Digits required but not really found" );
356 } else {
357 if ( blstruct->canIncludeRawData() ){
359 LArDigitContainer::const_iterator it_e=digitCont->end();
360 if (it_b==it_e) {
361 ATH_MSG_WARNING ( "Attempt to persistify a empty LArDigitContainer to ByteStream" );
362 }
363 for(; it_b!=it_e; ++it_b){
364 const LArDigit* digit = *it_b;
365 HWIdentifier chid = digit->hardwareID() ;
366 uint32_t reid = hid2re.getRodID( **febRodMapping, chid );
367 // Lets use anygain for the moment
368 getEncoder(reid).add (digit);
369 }
370 } // End of check whether format allows to include RawData
371 } // Finish checking for Digit container in SG
372 } // End of check for digits inclusion
373
375
376 // Now loop over map and fill all ROD Data Blocks
377 std::map<uint32_t,LArRodEncoder>::iterator it_m =mapEncoder.begin();
378 std::map<uint32_t,LArRodEncoder>::iterator it_m_e=mapEncoder.end();
379 // LArRodEncoder has collected all the channels, now can fill the
380 // ROD block data.
381 for(; it_m!=it_m_e;++it_m) {
382 theROD = fea.getRodData( (*it_m).first );
383 ((*it_m).second).fillROD( *theROD,msg(), **noise, m_nfebsigma ) ;
384 // delete ((*it_m).second);
385 // ((*it_m).second)=NULL;
386 }
387 ATH_MSG_DEBUG ( "Filled " << mapEncoder.size() << " Rod Blocks" );
388 return StatusCode::SUCCESS;
389}
390
391
392StatusCode
394 RobIndex_t& robIndex) const
395{
396 // This `optimization' leads to disaster if there ever happen to be
397 // two adjacent events with identical L1 IDs --- in that case,
398 // we'd be using the dangling ROB pointers from the last event.
399 // We should generally come through here only once per event anyway.
400 ATH_MSG_DEBUG( "Converting event (from ByteStream)" );
401
402 if (!re) {
403 ATH_MSG_ERROR ( "RawEvent passed to 'convert'-function is a null pointer!" );
404 return StatusCode::FAILURE;
405 }
406
407 if (!m_decoder->check_valid (re, msg())) return StatusCode::FAILURE;
408
409 ATH_MSG_VERBOSE ( "Full Event:" );
410 ATH_MSG_VERBOSE ( MSG::hex << "Full source ID: " << re->source_id() << MSG::dec );
411 ATH_MSG_VERBOSE ("Fragment size in words: " << re->fragment_size_word() );
412
413 // Need to clear up or it will accumulate
414 robIndex.clear();
415 eformat::helper::build_toc(*re, robIndex );
416 if ( robIndex.empty() ) { // This is a problem
417 return StatusCode::FAILURE;
418 }
419 return StatusCode::SUCCESS;
420}
421
422
424std::unique_ptr<LArRodBlockStructure>
426{
427 switch(m_DSPRunMode) {
428 case 0: // Obsolete; shouldn't get here.
429 std::abort();
430
431 case 2: //Transparent mode, DSP just copies FEB-data
432 ATH_MSG_DEBUG ( "Set Rod Block Type to LArRodBlockTransparent (#2)" );
433 return std::make_unique<LArRodBlockTransparentV0<LArRodBlockHeaderTransparentV0> >();
434 break;
435
436 case 7: //Calibration mode
437 ATH_MSG_DEBUG ( "Set Rod Block Type to LArRodBlockCalibration (#7)" );
438 return std::make_unique<LArRodBlockCalibrationV0<LArRodBlockHeaderCalibrationV0> >();
439 break;
440
441 case 4: //Physics assembly mode
442 if ( m_RodBlockVersion == 10 ){
443 ATH_MSG_DEBUG ( "Set Rod Block Type to LArRodBlockPhysics (#5)" );
444 return std::make_unique<LArRodBlockPhysicsV5>();
445 }
446 else if ( m_RodBlockVersion == 12 ){
447 ATH_MSG_DEBUG ( "Set Rod Block Type to LArRodBlockPhysics (#6)" );
448 return std::make_unique<LArRodBlockPhysicsV6>();
449 }
450 else {
451 ATH_MSG_DEBUG ( "Set Rod Block Type to LArRodBlockPhysics (#4)" );
452 return std::make_unique<LArRodBlockPhysicsV0>();
453 }
454 break;
455
456 case 5: //Physics assembly mode
457 ATH_MSG_DEBUG ( "Set Rod Block Type to LArRodBlockPhysics (#5)" );
458 return std::make_unique<LArRodBlockPhysicsV3>();
459
460 default:
461 ATH_MSG_WARNING ( "DSP runmode " << m_DSPRunMode << " is unknown. Using physics assembly mode (#4) by default" );
462 return std::make_unique<LArRodBlockPhysicsV0>();
463 }
464}
465
466
467const Hid2RESrcID&
469{
470 if (!m_hid2re) {
471 auto hid2re = std::make_unique<Hid2RESrcID>();
472 if (hid2re->initialize (rodMapping).isFailure()) {
473 std::abort();
474 }
475 m_hid2re.set (std::move (hid2re));
476 }
477 return *m_hid2re.get();
478}
479
const boost::regex re(r_e)
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
static const InterfaceID IID_ILArRawDataContByteStreamTool("LArRawDataContByteStreamTool", 1, 0)
Helper tool for conversion of raw data classes to/from LArByteStream.
OFFLINE_FRAGMENTS_NAMESPACE::FullEventFragment RawEvent
data type for reading raw event
Definition RawEvent.h:37
AthAlgTool(const std::string &type, const std::string &name, const IInterface *parent)
Constructor with parameters:
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
ServiceHandle< StoreGateSvc > & evtStore()
const ServiceHandle< StoreGateSvc > & detStore() const
MsgStream & msg() const
DataModel_detail::const_iterator< DataVector > const_iterator
Definition DataVector.h:838
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
size_type size() const noexcept
Returns the number of elements in the collection.
void setRodMinorVersion(uint16_t m)
change the ROD minor version
void setDetEvtType(uint32_t m)
change Detector Event Type
RODDATA * getRodData(uint32_t id)
get a block of ROD data
uint32_t getRodID(const LArFebRodMapping &rodMapping, const HWIdentifier &hid) const
make a ROD SrcID for a HWIdentifier
Container class for LArCalibDigit.
Base class for LArDigits taken during calibration runs.
Container class for LArDigit.
Liquid Argon digit base class.
Definition LArDigit.h:25
Container for LArRawChannel (IDC using LArRawChannelCollection)
Liquid Argon ROD output object base class.
HWIdentifier channelID() const
StatusCode prepareRobIndex(const RawEvent *event, RobIndex_t &robIndex) const
Prepare ROB index before conversion.
CxxUtils::CachedUniquePtr< Hid2RESrcID > m_hid2re
Contains the mapping from channel to ROD (writing only)
std::map< eformat::SubDetectorGroup, std::vector< const uint32_t * > > RobIndex_t
unsigned m_DSPRunMode
Indicates which version of DSP code should be used for writing.
PublicToolHandle< LArRodDecoder > m_decoder
bool checkGainConsistency(const COLLECTION *coll) const
Check that all elements in a container have the same gain.
SG::ReadCondHandleKey< CaloNoise > m_caloNoiseKey
StatusCode WriteLArRawChannels(const LArRawChannelContainer *CannelCont, FEA_t &fea) const
Fill channels from LArRawChannelContainer to a FullEvent.
SG::ReadCondHandleKey< LArOnOffIdMapping > m_onOffIdMappingKey
SG::ReadCondHandleKey< CaloDetDescrManager > m_caloMgrKey
virtual StatusCode initialize() override
static const InterfaceID & interfaceID()
AlgTool InterfaceID.
SG::ReadCondHandleKey< LArFebRodMapping > m_febRodMappingKey
StatusCode WriteLArDigits(const LArDigitContainer *digit_cont, FEA_t &fea) const
Fill channels from LArDigitContainer to a FullEvent.
LArRawDataContByteStreamTool(const std::string &type, const std::string &name, const IInterface *parent)
Constructor Standard AlgTool constructor.
StatusCode WriteLArCalibDigits(const LArCalibDigitContainer *digit_cont, FEA_t &fea) const
Fill channels from LArCalibDigitContainer to a FullEvent.
std::unique_ptr< LArRodBlockStructure > makeRodBlockStructure() const
Construct a RodBlockStructure instance of the proper concrete type.
unsigned short m_RodBlockVersion
Minor ROD block version This is equivalent to the lower 16 bits of the version word in the ROD block ...
FullEventAssembler< RodRobIdMap > FEA_t
const Hid2RESrcID & getHid2RESrcID(const LArFebRodMapping &rodMapping) const
virtual StatusCode finalize() override
bool m_initializeForWriting
JobOption to intitialize services needed only for writing.
This class provides conversion from LArRawChannel and LArDigit to ROD format.