LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
RecompressRawDigits_module.cc
Go to the documentation of this file.
1 
13 // LArSoft libraries
15 #include "lardataobj/RawData/raw.h" // raw::Compress(), raw::Uncompress()
16 #include "larcoreobj/SimpleTypesAndConstants/RawTypes.h" // raw::Compress_t
17 
18 // framework libraries
22 #include "art/Framework/Principal/Handle.h" // art::ValidHandle
26 #include "fhiclcpp/types/Atom.h"
27 #include "fhiclcpp/types/Table.h"
28 
29 // C/C++ standard libraries
30 #include <vector>
31 #include <string>
32 #include <algorithm> // std::transform()
33 #include <memory> // std::make_unique()
34 #include <cctype> // std::toupper()
35 
36 
37 namespace raw {
38 
70  public:
71 
72  struct Config {
73 
74  using Name = fhicl::Name;
76 
78  Name("rawDigitLabel"),
79  Comment("input tag for the original raw::RawDigit collection")
80  };
81 
83  Name("compressionType"),
84  Comment("compression mode code (e.g. \"kHuffman\")")
85  };
86 
88  Name("instanceName"),
89  Comment("instance name for the output data product (none by default)"),
90  std::string() // default
91  };
92 
93  }; // Config
94 
95 
97 
98 
100  explicit RecompressRawDigits(Parameters const& config)
101  : fRawDigitLabel(config().rawDigitLabel())
103  , fInstanceName(config().instanceName())
104  {
105  produces<std::vector<raw::RawDigit>>(fInstanceName);
106  }
107 
108 
109  virtual void produce(art::Event& event) override;
110 
111 
127  (raw::RawDigit const& digit, Compress_t newFormat, bool force = false);
128 
129 
142  static raw::Compress_t parseCompressionType(std::string spec);
143 
144 
145  private:
148  std::string fInstanceName;
149 
150 
151  }; // class RecompressRawDigits
152 
153 
154 } // namespace raw
155 
156 
157 
158 
159 //------------------------------------------------------------------------------
160 //--- implementation
161 //------------------------------------------------------------------------------
162 namespace {
163  std::string toupper(std::string s) {
164  // act directly on s (which is a copy of the original);
165  // explicit cast is needed to pick which std::toupper() to use
166  std::transform(s.begin(), s.end(), s.begin(), (int(*)(int)) std::toupper);
167  return s;
168  } // raw::RecompressRawDigits::toupper()
169 } // local namespace
170 
171 
172 //------------------------------------------------------------------------------
174 
175  //
176  // prepare input and output container
177  //
178  auto oldRawDigitHandle
179  = event.getValidHandle<std::vector<raw::RawDigit>>(fRawDigitLabel);
180 
181  auto newRawDigits = std::make_unique<std::vector<raw::RawDigit>>();
182 
183  //
184  // create a new compressed raw digit for each existing one
185  //
186  std::set<raw::Compress_t> formats;
187  for (raw::RawDigit const& oldDigits: *oldRawDigitHandle) {
188 
189  // keep track of all the formats
190  formats.insert(oldDigits.Compression());
191 
192  // recompress the digit, and put it into the future data product
193  newRawDigits->emplace_back(recompress(oldDigits, fCompressionType));
194 
195  } // for oldDigits
196 
197  //
198  // store the new digits into the event
199  //
200  event.put(std::move(newRawDigits), fInstanceName);
201 
202 } // raw::RecompressRawDigits::produce()
203 
204 
205 
206 //------------------------------------------------------------------------------
208  (raw::RawDigit const& digit, Compress_t newFormat, bool force /* = false */)
209 {
210 
211  if ((newFormat == digit.Compression()) && !force)
212  return digit; // return a copy
213 
214  // uncompress the data
215  raw::RawDigit::ADCvector_t ADCs(digit.Samples());
216  raw::Uncompress(digit.ADCs(), ADCs, digit.Compression());
217 
218  // compress the data (it happens in place)
219  raw::Compress(ADCs, newFormat);
220 
221  // we force a copy in a buffer sized just big enough to host the data;
222  // in case the buffer is already of the right size (as it happens when
223  // newFormat specifies no compression), then no copy happens
224  ADCs.shrink_to_fit();
225 
226  // create a new digit with the new buffer
227  return raw::RawDigit{
228  digit.Channel(),
229  digit.Samples(),
230  std::move(ADCs),
231  newFormat
232  };
233 
234 } // raw::RecompressRawDigits::recompress()
235 
236 
237 //------------------------------------------------------------------------------
239 {
240  std::string SPEC(toupper(spec));
241 
242  if ((SPEC == "NONE" ) || (SPEC == "KNONE" )) return raw::kNone ;
243  if ((SPEC == "HUFFMAN" ) || (SPEC == "KHUFFMAN" )) return raw::kHuffman ;
244  if ((SPEC == "ZEROSUPPRESSION") || (SPEC == "KZEROSUPPRESSION")) return raw::kZeroSuppression;
245  if ((SPEC == "ZEROHUFFMAN" ) || (SPEC == "KZEROHUFFMAN" )) return raw::kZeroHuffman ;
246  if ((SPEC == "DYNAMICDEC" ) || (SPEC == "KDYNAMICDEC" )) return raw::kDynamicDec ;
247 
248  // if a valid one triggers this error, it needs to be added above
250  << "Unrecognized compression type: '" << spec << "'\n";
251 
252 } // raw::RecompressRawDigits::parseCompressionType()
253 
254 
255 //------------------------------------------------------------------------------
257 
258 
259 
260 //------------------------------------------------------------------------------
Huffman Encoding.
Definition: RawTypes.h:10
Float_t s
Definition: plot.C:23
const ADCvector_t & ADCs() const
Reference to the compressed ADC count vector.
Definition: RawDigit.h:209
enum raw::_compress Compress_t
Collection of charge vs time digitized from a single readout channel.
Definition: RawDigit.h:68
ChannelID_t Channel() const
DAQ channel this raw data was read from.
Definition: RawDigit.h:211
std::vector< short > ADCvector_t
Type representing a (compressed) vector of ADC counts.
Definition: RawDigit.h:72
Definition of basic raw digits.
Zero Suppression followed by Huffman Encoding.
Definition: RawTypes.h:12
Raw data description.
Definition: RawTypes.h:6
no compression
Definition: RawTypes.h:9
unsigned short Samples() const
Number of samples in the uncompressed ADC data.
Definition: RawDigit.h:212
Zero Suppression algorithm.
Definition: RawTypes.h:11
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:42
art::InputTag fRawDigitLabel
tag of input raw digit collection
raw::Compress_t fCompressionType
type of compression to be applied
Collect all the RawData header files together.
static raw::Compress_t parseCompressionType(std::string spec)
Returns the compression mode corresponding to the specified string.
raw::Compress_t Compression() const
Compression algorithm used to store the ADC counts.
Definition: RawDigit.h:215
static raw::RawDigit recompress(raw::RawDigit const &digit, Compress_t newFormat, bool force=false)
Returns a RawDigit with its waveform compressed in newFormat
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
std::string fInstanceName
instance name of output data product
RecompressRawDigits(Parameters const &config)
Constructor; see module documentation for configuration directions.
void Compress(std::vector< short > &adc, raw::Compress_t compress)
Compresses a raw data buffer.
Definition: raw.cxx:20
fhicl::Atom< art::InputTag > rawDigitLabel
fhicl::Atom< std::string > compressionType
void Uncompress(const std::vector< short > &adc, std::vector< short > &uncompressed, raw::Compress_t compress)
Uncompresses a raw data buffer.
Definition: raw.cxx:756
virtual void produce(art::Event &event) override
Dynamic decimation.
Definition: RawTypes.h:13
Writes the input raw::RawDigit with a different compression.
Event finding and building.