CalWireDUNE35t_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // CalWireDUNE35t class
4 //
5 // brebel@fnal.gov
6 //
7 // 11-3-09 Pulled all FFT code out and put into Utilitiess/LArFFT
8 //
9 ////////////////////////////////////////////////////////////////////////
10 
11 // C/C++ standard libraries
12 #include <string>
13 #include <vector>
14 #include <utility> // std::move()
15 #include <memory> // std::unique_ptr<>
16 #include <iostream>
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
38 #include "lardataobj/RawData/raw.h"
45 
46 using std::cout;
47 using std::endl;
48 
49 ///creation of calibrated signals on wires
50 namespace caldata {
51 
53 
54  public:
55 
56  // create calibrated signals on wires. this class runs
57  // an fft to remove the electronics shaping.
58  explicit CalWireDUNE35t(fhicl::ParameterSet const& pset);
59  virtual ~CalWireDUNE35t();
60 
61  void produce(art::Event& evt);
62  void beginJob();
63  void endJob();
64  void reconfigure(fhicl::ParameterSet const& p);
65 
66  private:
67 
68  //int fDataSize; ///< size of raw data on one wire // unused
69  int fPostsample; ///< number of postsample bins
70  int fDoBaselineSub; ///< number of postsample bins
71  std::string fDigitModuleLabel; ///< module that made digits
72  ///< constants
73  std::string fSpillName; ///< nominal spill is an empty string
74  ///< it is set by the DigitModuleLabel
75  ///< ex.: "daq:preSpill" for prespill data
76  unsigned short fPreROIPad; ///< ROI padding
77  unsigned short fPostROIPad; ///< ROI padding
78  double fSigThrFact; ///< Signal shreshold factor
79 
80  void SubtractBaseline(std::vector<float>& holder);
81 
82 
83  protected:
84 
85  }; // class CalWireDUNE35t
86 
88 
89  //-------------------------------------------------
90  CalWireDUNE35t::CalWireDUNE35t(fhicl::ParameterSet const& pset) : EDProducer{pset}
91  {
92  this->reconfigure(pset);
93 
94  produces< std::vector<recob::Wire> >(fSpillName);
95  produces<art::Assns<raw::RawDigit, recob::Wire>>(fSpillName);
96  }
97 
98  //-------------------------------------------------
100  {
101  }
102 
103  //////////////////////////////////////////////////////
105  {
106  std::vector<unsigned short> uin; std::vector<unsigned short> vin;
107  std::vector<unsigned short> zin;
108 
109 
110  fDigitModuleLabel = p.get< std::string >("DigitModuleLabel", "daq");
111  fPostsample = p.get< int > ("PostsampleBins");
112  fDoBaselineSub = p.get< bool > ("DoBaselineSub");
113  uin = p.get< std::vector<unsigned short> > ("PlaneROIPad");
114  fSigThrFact = p.get< double> ("SigThrFact", 3.0);
115 
116  // put the ROI pad sizes into more convenient vectors
117  fPreROIPad = uin[0];
118  fPostROIPad = uin[1];
119 
120 
121  fSpillName.clear();
122 
123  size_t pos = fDigitModuleLabel.find(":");
124  if( pos!=std::string::npos ) {
125  fSpillName = fDigitModuleLabel.substr( pos+1 );
126  fDigitModuleLabel = fDigitModuleLabel.substr( 0, pos );
127  }
128 
129  }
130 
131  //-------------------------------------------------
133  {
134  }
135 
136  //////////////////////////////////////////////////////
138  {
139  }
140 
141  //////////////////////////////////////////////////////
143  {
144  // get the geometry
146 
147  // get the FFT service to have access to the FFT size
149  int transformSize = fFFT->FFTSize();
150 
151  // Get signal shaping service.
153  double DeconNorm = sss->GetDeconNorm();
154 
155  // make a collection of Wires
156  std::unique_ptr<std::vector<recob::Wire> > wirecol(new std::vector<recob::Wire>);
157  // ... and an association set
158  std::unique_ptr<art::Assns<raw::RawDigit,recob::Wire> > WireDigitAssn
160 
161  // Read in the digit List object(s).
163  auto digitVecHandle = evt.getHandle< std::vector<raw::RawDigit> >(itag1);
164 
165  if (!digitVecHandle->size()) return;
166  mf::LogInfo("CalWireDUNE35t") << "CalWireDUNE35t:: digitVecHandle size is " << digitVecHandle->size();
167 
168  // Use the handle to get a particular (0th) element of collection.
169  art::Ptr<raw::RawDigit> digitVec0(digitVecHandle, 0);
170 
171  unsigned int dataSize = digitVec0->Samples(); //size of raw data vectors
172  //std::cout << "Xin " << dataSize << std::endl;
173  auto const clockData = art::ServiceHandle<detinfo::DetectorClocksService const>()->DataFor(evt);
174  auto const detProp = art::ServiceHandle<detinfo::DetectorPropertiesService const>()->DataFor(evt, clockData);
175  int readoutwindowsize = detProp.ReadOutWindowSize();
176  if (int(dataSize) != readoutwindowsize){
178  << "ReadOutWindowSize "<<readoutwindowsize<<" does not match data size "<<dataSize<<". Please set services.DetectorPropertiesService.NumberTimeSamples and services.DetectorPropertiesService.ReadOutWindowSize in fcl file to "<<dataSize;
179  }
180 
181  raw::ChannelID_t channel = raw::InvalidChannelID; // channel number
182  unsigned int bin(0); // time bin loop variable
183 
184  filter::ChannelFilter *chanFilt = new filter::ChannelFilter();
185 
186  std::vector<float> holder; // holds signal data
187  std::vector<short> rawadc(transformSize); // vector holding uncompressed adc values
188  std::vector<TComplex> freqHolder(transformSize+1); // temporary frequency data
189 
190  // loop over all wires
191  wirecol->reserve(digitVecHandle->size());
192  for(size_t rdIter = 0; rdIter < digitVecHandle->size(); ++rdIter){ // ++ move
193  holder.clear();
194 
195  // get the reference to the current raw::RawDigit
196  art::Ptr<raw::RawDigit> digitVec(digitVecHandle, rdIter);
197  channel = digitVec->Channel();
198 
199  // skip bad channels
200  if(!chanFilt->BadChannel(channel)) {
201  holder.resize(transformSize);
202 
203  // uncompress the data
204 
205  int pedestal_value = (int) digitVec->GetPedestal();
206  raw::Uncompress(digitVec->ADCs(), rawadc, pedestal_value, digitVec->Compression());
207 
208  // loop over all adc values and subtract the pedestal
209  for(bin = 0; bin < dataSize; ++bin)
210  holder[bin]=(rawadc[bin]-digitVec->GetPedestal());
211 
212  //Xin fill the remaining bin with data
213  for (bin = dataSize;bin<holder.size();bin++){
214  holder[bin] = (rawadc[bin-dataSize]-digitVec->GetPedestal());
215  }
216 
217  // Do deconvolution.
218  sss->Deconvolute(clockData, channel, holder);
219  for(bin = 0; bin < holder.size(); ++bin) holder[bin]=holder[bin]/DeconNorm;
220  } // end if not a bad channel
221 
222  holder.resize(dataSize,1e-5);
223 
224  //This restores the DC component to signal removed by the deconvolution.
225  if(fPostsample) {
226  double average=0.0;
227  for(bin=0; bin < (unsigned int)fPostsample; ++bin)
228  average+=holder[holder.size()-1-bin]/(double)fPostsample;
229  for(bin = 0; bin < holder.size(); ++bin) holder[bin]-=average;
230  }
231  // adaptive baseline subtraction
232  if(fDoBaselineSub) SubtractBaseline(holder);
233 
234 
235  // work out the ROI
237  std::vector<std::pair<unsigned int, unsigned int>> holderInfo;
238  std::vector<std::pair<unsigned int, unsigned int>> rois;
239 
240  double max = 0;
241  double deconNoise = sss->GetDeconNoise(channel);
242  // find out all ROI
243  unsigned int roiStart = 0;
244  for(bin = 0; bin < dataSize; ++bin) {
245  double SigVal = holder[bin];
246  if (SigVal > max) max = SigVal;
247  if(roiStart == 0) {
248  if (SigVal > fSigThrFact*deconNoise) roiStart = bin; // n sigma above noise
249  }else{
250  if (SigVal < deconNoise){
251  rois.push_back(std::make_pair(roiStart, bin));
252  roiStart = 0;
253  }
254  }
255  }
256  if (roiStart!=0){
257  rois.push_back(std::make_pair(roiStart, dataSize-1));
258  roiStart = 0;
259  }
260 
261  // pad them
262  // if (channel==512){
263  // for (bin = 0; bin< holder.size();++bin){
264  // if (fabs(holder[bin]) > 2)
265  // std::cout << "Xin1: " << holder[bin] << std::endl;
266  // }
267  // }
268  //std::cout << "Xin: " << max << " "<< channel << " " << deconNoise << " " << rois.size() << std::endl;
269 
270  if(rois.size() == 0) continue;
271  holderInfo.clear();
272  for(unsigned int ii = 0; ii < rois.size(); ++ii) {
273  // low ROI end
274  int low = rois[ii].first - fPreROIPad;
275  if(low < 0) low = 0;
276  rois[ii].first = low;
277  // high ROI end
278  unsigned int high = rois[ii].second + fPostROIPad;
279  if(high >= dataSize) high = dataSize-1;
280  rois[ii].second = high;
281 
282  }
283  // merge them
284  if(rois.size() >= 1) {
285  // temporary vector for merged ROIs
286 
287  for (unsigned int ii = 0; ii<rois.size();ii++){
288  unsigned int roiStart = rois[ii].first;
289  unsigned int roiEnd = rois[ii].second;
290 
291  int flag1 = 1;
292  unsigned int jj=ii+1;
293  while(flag1){
294  if (jj<rois.size()){
295  if(rois[jj].first <= roiEnd ) {
296  roiEnd = rois[jj].second;
297  ii = jj;
298  jj = ii+1;
299  }else{
300  flag1 = 0;
301  }
302  }else{
303  flag1 = 0;
304  }
305  }
306  std::vector<float> sigTemp;
307  for(unsigned int kk = roiStart; kk < roiEnd; ++kk) {
308  sigTemp.push_back(holder[kk]);
309  } // jj
310  // std::cout << "Xin: " << roiStart << std::endl;
311  ROIVec.add_range(roiStart, std::move(sigTemp));
312  //trois.push_back(std::make_pair(roiStart,roiEnd));
313  }
314  }
315 
316  // save them
317  wirecol->push_back(recob::WireCreator(std::move(ROIVec),*digitVec).move());
318 
319  // Make a single ROI that spans the entire data size
320  //wirecol->push_back(recob::WireCreator(holder,*digitVec).move());
321 
322 
323 
324 
325  // add an association between the last object in wirecol
326  // (that we just inserted) and digitVec
327  if (!util::CreateAssn(*this, evt, *wirecol, digitVec, *WireDigitAssn, fSpillName)) {
329  << "Can't associate wire #" << (wirecol->size() - 1)
330  << " with raw digit #" << digitVec.key();
331  } // if failed to add association
332  }
333 
334  if(wirecol->size() == 0)
335  mf::LogWarning("CalWireDUNE35t") << "No wires made for this event.";
336 
337  evt.put(std::move(wirecol), fSpillName);
338  evt.put(std::move(WireDigitAssn), fSpillName);
339 
340 
341  delete chanFilt;
342  return;
343  }
344 
345  void CalWireDUNE35t::SubtractBaseline(std::vector<float>& holder)
346  {
347 
348  float min = 0,max=0;
349  for (unsigned int bin = 0; bin < holder.size(); bin++){
350  if (holder[bin] > max) max = holder[bin];
351  if (holder[bin] < min) min = holder[bin];
352  }
353  int nbin = max - min;
354  if (nbin!=0){
355  TH1F *h1 = new TH1F("h1","h1",nbin,min,max);
356  for (unsigned int bin = 0; bin < holder.size(); bin++){
357  h1->Fill(holder[bin]);
358  }
359  float ped = h1->GetMaximum();
360  float ave=0,ncount = 0;
361  for (unsigned int bin = 0; bin < holder.size(); bin++){
362  if (fabs(holder[bin]-ped)<2){
363  ave +=holder[bin];
364  ncount ++;
365  }
366  }
367  if (ncount==0) ncount=1;
368  ave = ave/ncount;
369  //cout << "CalWireDUNE35t::SubtractBaseline: size, ped, ncount, ave = "
370  // << holder.size()
371  // << ", " << ped
372  // << ", " << ncount
373  // << ", " << ave
374  // << endl;
375  for (unsigned int bin = 0; bin < holder.size(); bin++){
376  holder[bin] -= ave;
377  }
378  h1->Delete();
379  }
380  }
381 
382 
383 } // end namespace caldata
float GetPedestal() const
Definition: RawDigit.h:214
void Deconvolute(detinfo::DetectorClocksData const &clockData, Channel channel, std::vector< T > &func) const
const ADCvector_t & ADCs() const
Reference to the compressed ADC count vector.
Definition: RawDigit.h:210
void produce(art::Event &evt)
ULong64_t Samples() const
Number of samples in the uncompressed ADC data.
Definition: RawDigit.h:213
unsigned short fPreROIPad
ROI padding.
Handle< PROD > getHandle(SelectorBase const &) const
Definition: DataViewImpl.h:382
std::string string
Definition: nybbler.cc:12
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
bool BadChannel(uint32_t channel) const
Helper functions to create a wire.
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
const datarange_t & add_range(size_type offset, ITER first, ITER last)
Adds a sequence of elements as a range with specified offset.
unsigned short fPostROIPad
ROI padding.
uint8_t channel
Definition: CRTFragment.hh:201
void reconfigure(fhicl::ParameterSet const &p)
Class managing the creation of a new recob::Wire object.
Definition: WireCreator.h:53
double GetDeconNoise(Channel channel) const override
Service to provide microboone-specific signal shaping for simulation (convolution) and reconstruction...
creation of calibrated signals on wires
art framework interface to geometry description
int fPostsample
number of postsample bins
int FFTSize() const
Definition: LArFFT.h:69
constexpr ChannelID_t InvalidChannelID
ID of an invalid channel.
Definition: RawTypes.h:32
const double e
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
def move(depos, offset)
Definition: depos.py:107
key_type key() const noexcept
Definition: Ptr.h:216
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
bool CreateAssn(PRODUCER const &prod, art::Event &evt, std::vector< T > const &a, art::Ptr< U > const &b, art::Assns< U, T > &assn, std::string a_instance, size_t indx=UINT_MAX)
Creates a single one-to-one association.
static int max(int a, int b)
CalWireDUNE35t(fhicl::ParameterSet const &pset)
raw::Compress_t Compression() const
Compression algorithm used to store the ADC counts.
Definition: RawDigit.h:216
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:55
double fSigThrFact
Signal shreshold factor.
void SubtractBaseline(std::vector< float > &holder)
QTextStream & bin(QTextStream &s)
Declaration of basic channel signal object.
int fDoBaselineSub
number of postsample bins
TCEvent evt
Definition: DataStructs.cxx:7
unsigned int ChannelID_t
Type representing the ID of a readout channel.
Definition: RawTypes.h:28
void Uncompress(const std::vector< short > &adc, std::vector< short > &uncompressed, raw::Compress_t compress)
Uncompresses a raw data buffer.
Definition: raw.cxx:776
std::string fDigitModuleLabel
constants
QTextStream & endl(QTextStream &s)