UnstickADCCodes_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // UnstickADCCodes class
4 //
5 // jti3@fnal.gov
6 //
7 // Module to interpolate over stuck ADC codes (0x00 and 0x3F)
8 // in 35t raw digits' 6 LSBs
9 //
10 ////////////////////////////////////////////////////////////////////////
11 
12 // C/C++ standard libraries
13 #include <string>
14 #include <vector>
15 #include <utility> // std::move()
16 #include <memory> // std::unique_ptr<>
17 
18 // ROOT libraries
19 #include "TComplex.h"
20 
21 // framework libraries
22 #include "cetlib_except/exception.h"
23 #include "cetlib/search_path.h"
24 #include "fhiclcpp/ParameterSet.h"
32 
33 // LArSoft libraries
34 #include "larcoreobj/SimpleTypesAndConstants/RawTypes.h" // raw::ChannelID_t
37 #include "lardataobj/RawData/raw.h"
39 
40 ///creation of calibrated signals on wires
41 namespace unstick {
42 
44 
45  public:
46 
47  // create calibrated signals on wires. this class runs
48  // an fft to remove the electronics shaping.
49  explicit UnstickADCCodes(fhicl::ParameterSet const& pset);
50  virtual ~UnstickADCCodes();
51 
52  void produce(art::Event& evt);
53  void beginJob();
54  void endJob();
55  void reconfigure(fhicl::ParameterSet const& p);
56 
57  private:
58 
59  std::string fDigitModuleLabel; ///< module that made digits
60  ///< constants
61  std::string fSpillName; ///< nominal spill is an empty string
62  ///< it is set by the DigitModuleLabel
63  ///< ex.: "daq:preSpill" for prespill data
64 
65  raw::Compress_t fCompression; ///< compression type to use
66 
67  const unsigned int onemask = 0x003f; ///< Unsigned int ending in 111111 used to select 6 LSBs with bitwise AND
68  unsigned int fStickyADCCodesLimit; ///< Number of ADC codes to check for stickiness at 0x00 or 0x3f before stopping
69 
70  protected:
71 
72  }; // class UnstickADCCodes
73 
75 
76  //-------------------------------------------------
77  UnstickADCCodes::UnstickADCCodes(fhicl::ParameterSet const& pset) : EDProducer{pset}
78  {
79  this->reconfigure(pset);
80  produces< std::vector<raw::RawDigit> >(fSpillName);
81 
82  }
83 
84  //-------------------------------------------------
86  {
87  }
88 
89  //////////////////////////////////////////////////////
91  {
92 
93  fDigitModuleLabel = p.get< std::string >("DigitModuleLabel", "daq");
94  fStickyADCCodesLimit = p.get< unsigned int >("StickyADCCodesLimit");
95  fSpillName.clear();
96 
97  size_t pos = fDigitModuleLabel.find(":");
98  if( pos!=std::string::npos ) {
99  fSpillName = fDigitModuleLabel.substr( pos+1 );
100  fDigitModuleLabel = fDigitModuleLabel.substr( 0, pos );
101  }
102 
103  }
104 
105  //-------------------------------------------------
107  {
108  }
109 
110  //////////////////////////////////////////////////////
112  {
113  }
114 
115  //////////////////////////////////////////////////////
117  {
118 
119  // make an unique_ptr of sim::SimDigits that allows ownership of the produced
120  // digits to be transferred to the art::Event after the put statement below
121  std::unique_ptr< std::vector<raw::RawDigit> > digcol(new std::vector<raw::RawDigit>);
122 
123  // Read in the digit List object(s).
125  auto digitVecHandle = evt.getHandle< std::vector<raw::RawDigit> >(itag1);
126 
127  if (!digitVecHandle->size()) return;
128  mf::LogInfo("UnstickADCCodes") << "UnstickADCCodes:: digitVecHandle size is " << digitVecHandle->size();
129 
130  // Use the handle to get a particular (0th) element of collection.
131  art::Ptr<raw::RawDigit> digitVec0(digitVecHandle, 0);
132 
133  unsigned int dataSize = digitVec0->Samples(); //size of raw data vectors
134 
135 
136  raw::ChannelID_t channel = raw::InvalidChannelID; // channel number
137 
138  std::vector<short> rawadc(dataSize); // vector holding uncompressed adc values
139 
140  short *rawadc_a=0;
141 
142  // loop over all raw digits
143  digcol->reserve(digitVecHandle->size());
144  for(size_t rdIter = 0; rdIter < digitVecHandle->size(); ++rdIter){ // ++ move
145 
146  // get the reference to the current raw::RawDigit
147  art::Ptr<raw::RawDigit> digitVec(digitVecHandle, rdIter);
148  channel = digitVec->Channel();
149  fCompression = digitVec->Compression();
150  float pedestal = digitVec->GetPedestal();
151  // uncompress the data
152 
153  int pedestal_value = (int) digitVec->GetPedestal();
154  raw::Uncompress(digitVec->ADCs(), rawadc, pedestal_value, fCompression);
155  dataSize = rawadc.size();
156  rawadc_a = rawadc.data();
157 
158  // loop over raw ADC vector and interpolate over stuck values
159  for(size_t i = 0; i < dataSize; ++i){
160 
161  unsigned int sixlsbs = rawadc_a[i] & onemask;
162 
163  if(sixlsbs==onemask || sixlsbs==0){ //ADC code is stuck at 0x3f or 0x00
164 
165 
166  if(i==0){ //if first ADC code is stuck, set it equal to pedestal value
167  rawadc_a[i] = (short) pedestal;
168  continue;
169  }
170 
171  //find nearest preceding unstuck ADC code
172  //which should be immediately preceding ADC code since all earlier ADC codes have been interpolated to non-stuck values
173  size_t last_unstuck = i > 0 ? i - 1 : 0;
174 
175 
176  //find nearest following unstuck ADC code
177  size_t next_unstuck = i;
178  unsigned short next_unstuck_sixlsbs;
179  unsigned short sixlsbs_stuck_in_a_row = 0;
180 
181  do{
182 
183  if(next_unstuck < dataSize - 1 && sixlsbs_stuck_in_a_row < fStickyADCCodesLimit)
184  ++next_unstuck;
185  else{
186  rawadc_a[next_unstuck] = (short) pedestal;
187  break;
188  }
189  next_unstuck_sixlsbs = rawadc_a[next_unstuck] & onemask;
190  if(next_unstuck_sixlsbs==0 || next_unstuck_sixlsbs==onemask)
191  ++sixlsbs_stuck_in_a_row;
192  else
193  sixlsbs_stuck_in_a_row = 0;
194 
195  }
196  while(next_unstuck_sixlsbs==onemask || next_unstuck_sixlsbs==0);
197 
198  // With last and next unstuck ADC codes found, interpolate linearly and replace stuck ADC code
199 
200 
201  float interpolated_unstuck_value = 1.0*(rawadc_a[next_unstuck] - rawadc_a[last_unstuck])/(next_unstuck-last_unstuck)*(i-last_unstuck)+rawadc_a[last_unstuck];
202  rawadc_a[i] = (short) interpolated_unstuck_value;
203 
204  }
205 
206 
207  }
208 
209  raw::RawDigit rd(channel, dataSize, rawadc, raw::kNone );
210  rd.SetPedestal( digitVec->GetPedestal(), digitVec->GetSigma() );
211  digcol->push_back(rd);
212 
213  }
214 
215  evt.put(std::move(digcol), fSpillName);
216 
217  return;
218  }
219 
220 
221 
222 } // end namespace unstick
223 
224 
float GetPedestal() const
Definition: RawDigit.h:214
const ADCvector_t & ADCs() const
Reference to the compressed ADC count vector.
Definition: RawDigit.h:210
enum raw::_compress Compress_t
ULong64_t Samples() const
Number of samples in the uncompressed ADC data.
Definition: RawDigit.h:213
Collection of charge vs time digitized from a single readout channel.
Definition: RawDigit.h:69
void reconfigure(fhicl::ParameterSet const &p)
creation of calibrated signals on wires
Handle< PROD > getHandle(SelectorBase const &) const
Definition: DataViewImpl.h:382
std::string string
Definition: nybbler.cc:12
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.h:20
ChannelID_t Channel() const
DAQ channel this raw data was read from.
Definition: RawDigit.h:212
std::string fDigitModuleLabel
constants
uint8_t channel
Definition: CRTFragment.hh:201
UnstickADCCodes(fhicl::ParameterSet const &pset)
const unsigned int onemask
Unsigned int ending in 111111 used to select 6 LSBs with bitwise AND.
no compression
Definition: RawTypes.h:9
art framework interface to geometry description
constexpr ChannelID_t InvalidChannelID
ID of an invalid channel.
Definition: RawTypes.h:32
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
def move(depos, offset)
Definition: depos.py:107
T get(std::string const &key) const
Definition: ParameterSet.h:271
p
Definition: test.py:223
ProductID put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: DataViewImpl.h:686
raw::Compress_t Compression() const
Compression algorithm used to store the ADC counts.
Definition: RawDigit.h:216
void SetPedestal(float ped, float sigma=1.)
Set pedestal and its RMS (the latter is 0 by default)
Definition: RawDigit.cxx:68
TCEvent evt
Definition: DataStructs.cxx:7
unsigned int ChannelID_t
Type representing the ID of a readout channel.
Definition: RawTypes.h:28
unsigned int fStickyADCCodesLimit
Number of ADC codes to check for stickiness at 0x00 or 0x3f before stopping.
raw::Compress_t fCompression
compression type to use
void Uncompress(const std::vector< short > &adc, std::vector< short > &uncompressed, raw::Compress_t compress)
Uncompresses a raw data buffer.
Definition: raw.cxx:776
float GetSigma() const
TODO RMS of the pedestal level?
Definition: RawDigit.h:215