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