ATLAS Offline Software
PackedContainerStreamer.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
15 #include "TClassStreamer.h"
16 #include "TMemberStreamer.h"
17 #include "TError.h"
18 #include "TROOT.h"
19 #include <cassert>
20 
21 
22 namespace SG {
23 
24 
30 inline
31 void writePackedParameters( TBuffer& b, const SG::PackedParameters& parms ) {
32  b << parms.nbits();
33  b << parms.flags();
34  if (parms.isFloat()) {
35  b << parms.nmantissa();
36  if (parms.hasScale())
37  b << parms.scale();
38  }
39 }
40 
41 
46 inline
48  uint8_t nbits;
49  b >> nbits;
50 
51  uint8_t flags;
52  b >> flags;
53 
54  SG::PackedParameters parms (nbits, flags);
55  if (parms.isFloat()) {
56  uint8_t nmantissa;
57  b >> nmantissa;
58  parms.setNmantissa (nmantissa);
59  if (parms.hasScale()) {
60  float scale;
61  b >> scale;
62  parms.setScale (scale);
63  }
64  }
65  return parms;
66 }
67 
68 
72 template <class T, class ALLOC>
74  : public TClassStreamer
75 {
76 public:
81 
82 
88  virtual void operator()(TBuffer &b, void *objp) override;
89 
90 
94  virtual TClassStreamer* Generate() const override;
95 
96 
97 private:
99  std::string m_className;
100 };
101 
102 
106 template <class T, class ALLOC>
108  : m_className( ClassName< SG::PackedContainer< T, ALLOC > >::name() )
109 {
110 }
111 
112 
118 template <class T, class ALLOC>
119 void PackedContainerStreamer<T, ALLOC>::operator() ( TBuffer& b, void* objp ) {
121  reinterpret_cast<SG::PackedContainer<T, ALLOC>*> (objp);
122 
123  if (b.IsReading()) {
124  UInt_t R__s, R__c;
125  Version_t R__v = b.ReadVersion(&R__s, &R__c);
126  if (R__v != 1) {
127  Error ("PackedContainerStreamer",
128  "Bad version %d for object of type %s (expected 1)",
129  R__v, m_className.c_str());
130  }
131 
132  cont->setParms (readPackedParameters (b));
133 
134  uint32_t nelt;
135  b >> nelt;
136 
137  SG::PackedConverter cnv (cont->parms());
138  cnv.read (nelt, *cont, b);
139 
140  b.CheckByteCount(R__s, R__c, m_className.c_str());
141  }
142  else {
143  UInt_t R__c = b.Length();
144  b.SetBufferOffset (R__c + sizeof(UInt_t));
145  b << Version_t(1);
146  writePackedParameters (b, cont->parms());
147 
148  uint32_t nelt = cont->size();
149  b << nelt;
150 
151  SG::PackedConverter cnv (cont->parms());
152  cnv.write (cont->size(), *cont, b);
153 
154  b.SetByteCount(R__c, kTRUE);
155  }
156 }
157 
158 
162 template <class T, class ALLOC>
164 {
165  return new PackedContainerStreamer<T, ALLOC> (*this);
166 }
167 
168 
169 template <class T, class ALLOC>
171 
172 
173 } // namespace SG
174 
175 
176 #define STREAMER1(TYPE, ALLOC) \
177  namespace ROOT { TGenericClassInfo* GenerateInitInstance(const SG::PackedContainer<TYPE, ALLOC>*);} \
178  namespace SG { \
179  template <> struct InstallPackedContainerStreamer<TYPE, ALLOC> { \
180  InstallPackedContainerStreamer() { \
181  ROOT::GenerateInitInstance((SG::PackedContainer<TYPE, ALLOC>*)nullptr)->AdoptStreamer (new PackedContainerStreamer<TYPE, ALLOC>) ; \
182  } }; \
183  InstallPackedContainerStreamer<TYPE, ALLOC> _R__UNIQUE_(streamerInstance); \
184  } class swallowSemicolon
185 
186 #define STREAMER(TYPE) STREAMER1(TYPE, std::allocator<TYPE>)
187 
188 
189 STREAMER(char);
190 STREAMER(unsigned char);
191 STREAMER(short);
192 STREAMER(unsigned short);
193 STREAMER(int);
194 STREAMER(unsigned int);
195 STREAMER(float);
196 STREAMER(double);
197 
198 STREAMER(std::vector<char>);
199 STREAMER(std::vector<unsigned char>);
200 STREAMER(std::vector<short>);
201 STREAMER(std::vector<unsigned short>);
202 STREAMER(std::vector<int>);
203 STREAMER(std::vector<unsigned int>);
204 STREAMER(std::vector<float>);
205 STREAMER(std::vector<double>);
206 
207 STREAMER(std::vector<std::vector<char> >);
208 STREAMER(std::vector<std::vector<unsigned char> >);
209 STREAMER(std::vector<std::vector<short> >);
210 STREAMER(std::vector<std::vector<unsigned short> >);
211 STREAMER(std::vector<std::vector<int> >);
212 STREAMER(std::vector<std::vector<unsigned int> >);
213 STREAMER(std::vector<std::vector<float> >);
214 STREAMER(std::vector<std::vector<double> >);
215 
216 #undef STREAMER
PackedContainer.h
Container to hold aux data to be stored in a packed form.
SG
Forward declaration.
Definition: CaloCellPacker_400_500.h:32
SG::writePackedParameters
void writePackedParameters(TBuffer &b, const SG::PackedParameters &parms)
Write a set of packed parameters to a buffer.
Definition: PackedContainerStreamer.cxx:31
xAOD::uint8_t
uint8_t
Definition: Muon_v1.cxx:575
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
SG::PackedParameters::flags
uint8_t flags() const
Additional flags describing the packing.
AthenaPoolTestRead.flags
flags
Definition: AthenaPoolTestRead.py:8
SG::InstallPackedContainerStreamer
Definition: PackedContainerStreamer.cxx:170
yodamerge_tmp.scale
scale
Definition: yodamerge_tmp.py:138
SG::PackedContainerStreamer::PackedContainerStreamer
PackedContainerStreamer()
Constructor.
Definition: PackedContainerStreamer.cxx:107
SG::PackedParameters::nmantissa
uint8_t nmantissa() const
The number of bits used for the mantissa portion of a float-point representation, excluding a sign bi...
SG::PackedContainerStreamer
Streamer for reading/writing SG::PackedContainer instances.
Definition: PackedContainerStreamer.cxx:75
SG::PackedContainerStreamer::Generate
virtual TClassStreamer * Generate() const override
Clone operation, required for MT.
Definition: PackedContainerStreamer.cxx:163
SG::PackedParameters::hasScale
bool hasScale() const
Should floats be rescaled before writing?
SG::PackedParameters::isFloat
bool isFloat() const
Are elements being written as floating-point numbers?
SG::PackedConverter
Helper for packing/unpacking a PackedContainer to/from a stream.
Definition: PackedConverter.h:44
ClassName
An interface for getting the name of a class as a string.
Definition: AthenaKernel/AthenaKernel/ClassName.h:33
SG::PackedContainer::setParms
void setParms(const PackedParameters &parms)
Set the packing parameters.
SG::PackedParameters::nbits
uint8_t nbits() const
The number of bits used to store each element.
SG::PackedParameters::setNmantissa
bool setNmantissa(uint8_t nmantissa)
Set the number of mantissa bits used in the packed representation.
Definition: PackedParameters.cxx:95
SG::PackedContainerStreamer::operator()
virtual void operator()(TBuffer &b, void *objp) override
Run the streamer.
Definition: PackedContainerStreamer.cxx:119
ClassName.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
SG::PackedContainer::parms
const PackedParameters & parms() const
Return the packing parameters for this container.
SG::PackedConverter::write
void write(size_t nelt, const std::vector< U, ALLOC > &vec, STREAM &stream)
Pack a vector to the stream.
SG::PackedParameters::setScale
bool setScale(float scale)
Set the scale to use when packing floating-point data.
Definition: PackedParameters.cxx:116
SG::PackedContainer
Container to hold aux data to be stored in a packed form.
Definition: PackedContainer.h:50
SG::readPackedParameters
SG::PackedParameters readPackedParameters(TBuffer &b)
Read a set of packed parameters from a buffer.
Definition: PackedContainerStreamer.cxx:47
STREAMER
#define STREAMER(TYPE)
Definition: PackedContainerStreamer.cxx:186
SG::PackedContainerStreamer::m_className
std::string m_className
Name of the class we read/write (for error messages).
Definition: PackedContainerStreamer.cxx:99
SG::PackedConverter::read
void read(size_t nelt, std::vector< U, ALLOC > &vec, STREAM &stream)
Unpack a vector from the stream.
SG::PackedParameters::scale
float scale() const
Return the scale for floating-point numbers.
SG::PackedParameters
Describe how the contents of a PackedContainer are to be saved.
Definition: PackedParameters.h:66
PackedConverter.h
Helper for packing/unpacking a PackedContainer to/from a stream.