CalWire_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // CalWire class
4 //
5 // brebel@fnal.gov
6 //
7 ////////////////////////////////////////////////////////////////////////
8 
9 // ROOT includes
10 #include <TFile.h>
11 #include <TH2D.h>
12 #include <TH1D.h>
13 #include <TF1.h>
14 #include <TComplex.h>
15 
16 // Framework includes
17 #include "fhiclcpp/ParameterSet.h"
19 #include "cetlib_except/exception.h"
20 #include "cetlib/search_path.h"
26 #include "art/Framework/Core/EDProducer.h" // include the proper bit of the framework
28 
29 // LArSoft includes
30 #include "larcoreobj/SimpleTypesAndConstants/RawTypes.h" // raw::ChannelID_t
33 #include "lardataobj/RawData/raw.h"
38 
39 ///creation of calibrated signals on wires
40 namespace caldata {
41 
42  class CalWire : public art::EDProducer {
43 
44  public:
45 
46  // create calibrated signals on wires. this class runs
47  // an fft to remove the electronics shaping.
48  explicit CalWire(fhicl::ParameterSet const& pset);
49 
50  void produce(art::Event& evt);
51  void beginJob();
52 
53  private:
54 
55  std::string fResponseFile; ///< response file containing transformed
56  ///< shape histograms and decay constants
57  // for c2: fDataSize is not used
58  // int fDataSize; ///< size of raw data on one wire
59  int fExpEndBins; ///< number of end bins to consider for tail fit
60  int fPostsample; ///< number of postsample bins
61  std::string fDigitModuleLabel; ///< module that made digits
62 
63  std::vector<std::vector<TComplex> > fKernelR; ///< holds transformed induction
64  ///< response function
65  std::vector<std::vector<TComplex> > fKernelS; ///< holds transformed induction
66  ///< response function
67  std::vector<double> fDecayConstsR; ///< vector holding RC decay
68  ///< constants
69  std::vector<double> fDecayConstsS; ///< vector holding RC decay
70  ///< constants
71  std::vector<int> fKernMapR; ///< map telling which channels
72  ///< have which response functions
73  std::vector<int> fKernMapS; ///< map telling which channels
74  ///< have which response functions
75  protected:
76 
77  }; // class CalWire
78 }
79 
80 namespace caldata{
81 
82  //-------------------------------------------------
84  : EDProducer{pset}
85  {
86  fDigitModuleLabel = pset.get< std::string >("DigitModuleLabel", "daq");
87  cet::search_path sp("FW_SEARCH_PATH");
88  sp.find_file(pset.get<std::string>("ResponseFile"), fResponseFile);
89  fExpEndBins = pset.get< int > ("ExponentialEndBins");
90  fPostsample = pset.get< int > ("PostsampleBins");
91 
92  produces< std::vector<recob::Wire> >();
93  produces<art::Assns<raw::RawDigit, recob::Wire>>();
94 
95  }
96 
97  //-------------------------------------------------
99  {
100 
101  MF_LOG_DEBUG("CalWire") << "CalWire_plugin: Opening Electronics Response File: "
102  << fResponseFile.c_str();
103 
104  TFile f(fResponseFile.c_str());
105  if( f.IsZombie() )
106  mf::LogWarning("CalWire") << "Cannot open response file "
107  << fResponseFile.c_str();
108 
109  TH2D *respRe = dynamic_cast<TH2D*>(f.Get("real/RespRe") );
110  TH2D *respIm = dynamic_cast<TH2D*>(f.Get("real/RespIm") );
111  TH1D *decayHist = dynamic_cast<TH1D*>(f.Get("real/decayHist"));
112  unsigned int wires = decayHist->GetNbinsX();
113  unsigned int bins = respRe->GetYaxis()->GetNbins();
114  unsigned int bin = 0;
115  unsigned int wire = 0;
116  fDecayConstsR.resize(wires);
117  fKernMapR.resize(wires);
118  fKernelR.resize(respRe->GetXaxis()->GetNbins());
119  const TArrayD *edges = respRe->GetXaxis()->GetXbins();
120  for(int i = 0; i < respRe->GetXaxis()->GetNbins(); ++i) {
121  fKernelR[i].resize(bins);
122  for(bin = 0; bin < bins; ++bin) {
123 
124  const TComplex a(respRe->GetBinContent(i+1,bin+1),
125  respIm->GetBinContent(i+1,bin+1));
126  fKernelR[i][bin]=a;
127  }
128  for(; wire < (*edges)[i+1]; ++wire) {
129  fKernMapR[wire]=i;
130  fDecayConstsR[wire]=decayHist->GetBinContent(wire+1);
131  }
132  }
133  respRe = dynamic_cast<TH2D*>(f.Get("sim/RespRe") );
134  respIm = dynamic_cast<TH2D*>(f.Get("sim/RespIm") );
135  decayHist = dynamic_cast<TH1D*>(f.Get("sim/decayHist"));
136  wires = decayHist->GetNbinsX();
137  bins = respRe->GetYaxis()->GetNbins();
138  fDecayConstsS.resize(wires);
139  fKernMapS.resize(wires);
140  fKernelS.resize(respRe->GetXaxis()->GetNbins());
141  const TArrayD *edges1 = respRe->GetXaxis()->GetXbins();
142  wire =0;
143  for(int i = 0; i < respRe->GetXaxis()->GetNbins(); ++i) {
144  fKernelS[i].resize(bins);
145  for(bin = 0; bin < bins; ++bin) {
146  const TComplex b(respRe->GetBinContent(i+1,bin+1),
147  respIm->GetBinContent(i+1,bin+1));
148  fKernelS[i][bin]=b;
149  }
150  for(; wire < (*edges1)[i+1]; ++wire) {
151  fKernMapS[wire]=i;
152  fDecayConstsS[wire]=decayHist->GetBinContent(wire+1);
153  }
154  }
155 
156  f.Close();
157  }
158 
159  //////////////////////////////////////////////////////
161  {
162 
163 
164  // get the geometry
166 
167  std::vector<double> decayConsts;
168  std::vector<int> kernMap;
169  std::vector<std::vector<TComplex> > kernel;
170  //Put correct response functions and decay constants in place
171  if(evt.isRealData()) {
172  decayConsts=fDecayConstsR;
173  kernMap=fKernMapR;
174  kernel=fKernelR;
175  }
176  else {
177  decayConsts=fDecayConstsS;
178  kernMap=fKernMapS;
179  kernel=fKernelS;
180  }
181 
182  // get the FFT service to have access to the FFT size
184 
185  // make a collection of Wires
186  std::unique_ptr<std::vector<recob::Wire> > wirecol(new std::vector<recob::Wire>);
187  // ... and an association set
188  std::unique_ptr<art::Assns<raw::RawDigit,recob::Wire> > WireDigitAssn
190 
191  // Read in the digit List object(s).
193  evt.getByLabel(fDigitModuleLabel, digitVecHandle);
194 
195  if (!digitVecHandle->size()) return;
196  mf::LogInfo("CalWire") << "CalWire:: digitVecHandle size is " << digitVecHandle->size();
197 
198  // Use the handle to get a particular (0th) element of collection.
199  art::Ptr<raw::RawDigit> digitVec0(digitVecHandle, 0);
200 
201  unsigned int dataSize = digitVec0->Samples(); //size of raw data vectors
202 
203  int transformSize = fFFT->FFTSize();
205  unsigned int bin(0); // time bin loop variable
206 
207  double decayConst = 0.; // exponential decay constant of electronics shaping
208  double fitAmplitude = 0.; //This is the seed value for the amplitude in the exponential tail fit
209  std::vector<float> holder; // holds signal data
210  std::vector<short> rawadc(transformSize); // vector holding uncompressed adc values
211  std::vector<TComplex> freqHolder(transformSize+1); // temporary frequency data
212 
213  // loop over all wires
214  for(unsigned int rdIter = 0; rdIter < digitVecHandle->size(); ++rdIter){ // ++ move
215  holder.clear();
216 
217  art::Ptr<raw::RawDigit> digitVec(digitVecHandle, rdIter);
218  channel = digitVec->Channel();
219 
220  holder.resize(transformSize);
221 
222  // uncompress the data
223  raw::Uncompress(digitVec->ADCs(), rawadc, digitVec->Compression());
224 
225  for(bin = 0; bin < dataSize; ++bin)
226  holder[bin]=(rawadc[bin]-digitVec->GetPedestal());
227  // fExpEndBins only nonzero for detectors needing exponential tail fitting
228  if(fExpEndBins && std::abs(decayConsts[channel]) > 0.0){
229 
230  TH1D expTailData("expTailData","Tail data for fit",
231  fExpEndBins,dataSize-fExpEndBins,dataSize);
232  TF1 expFit("expFit","[0]*exp([1]*x)");
233 
234  for(bin = 0; bin < (unsigned int)fExpEndBins; ++bin)
235  expTailData.Fill(dataSize-fExpEndBins+bin,holder[dataSize-fExpEndBins+bin]);
236  decayConst = decayConsts[channel];
237  fitAmplitude = holder[dataSize-fExpEndBins]/exp(decayConst*(dataSize-fExpEndBins));
238  expFit.FixParameter(1,decayConst);
239  expFit.SetParameter(0,fitAmplitude);
240  expTailData.Fit(&expFit,"QWN","",dataSize-fExpEndBins,dataSize);
241  expFit.SetRange(dataSize,transformSize);
242  for(bin = 0; bin < dataSize; ++bin)
243  holder[dataSize+bin]= expFit.Eval(bin+dataSize);
244  }
245  // This is actually deconvolution, by way of convolution with the inverted
246  // kernel. This code assumes the response function has already been
247  // been transformed and inverted. This way a complex multiplication, rather
248  // than a complex division is performed saving 2 multiplications and
249  // 2 divsions
250 
251  // the example below is for MicroBooNE, experiments should
252  // adapt as appropriate
253 
254  // Figure out which kernel to use (0=induction, 1=collection).
255  geo::SigType_t sigtype = geom->SignalType(channel);
256  size_t k;
257  if(sigtype == geo::kInduction)
258  k = 0;
259  else if(sigtype == geo::kCollection)
260  k = 1;
261  else
262  throw cet::exception("CalWire") << "Bad signal type = " << sigtype << "\n";
263  if (k >= kernel.size())
264  throw cet::exception("CalWire") << "kernel size < " << k << "!\n";
265 
266  fFFT->Convolute(holder,kernel[k]);
267 
268  holder.resize(dataSize,1e-5);
269  //This restores the DC component to signal removed by the deconvolution.
270  if(fPostsample) {
271  double average=0.0;
272  for(bin=0; bin < (unsigned int)fPostsample; ++bin)
273  average+=holder[holder.size()-1-bin]/(double)fPostsample;
274  for(bin = 0; bin < holder.size(); ++bin) holder[bin]-=average;
275  }
276  wirecol->push_back(recob::WireCreator(holder,*digitVec).move());
277  // add an association between the last object in wirecol
278  // (that we just inserted) and digitVec
279  if (!util::CreateAssn(*this, evt, *wirecol, digitVec, *WireDigitAssn)) {
281  << "Can't associate wire #" << (wirecol->size() - 1)
282  << " with raw digit #" << digitVec.key();
283  } // if failed to add association
284  } // for raw digits
285 
286  if(wirecol->size() == 0)
287  mf::LogWarning("CalWire") << "No wires made for this event.";
288 
289  evt.put(std::move(wirecol));
290  evt.put(std::move(WireDigitAssn));
291 
292  return;
293  }
294 
295 } // end namespace caldata
296 
297 
298 namespace caldata{
299 
301 
302 } // 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
std::string fResponseFile
std::string string
Definition: nybbler.cc:12
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
std::vector< int > fKernMapR
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
std::vector< double > fDecayConstsR
uint8_t channel
Definition: CRTFragment.hh:201
SigType_t SignalType(geo::PlaneID const &pid) const
Returns the type of signal on the channels of specified TPC plane.
Class managing the creation of a new recob::Wire object.
Definition: WireCreator.h:53
creation of calibrated signals on wires
art framework interface to geometry description
std::vector< double > fDecayConstsS
bool isRealData() const
T abs(T value)
int FFTSize() const
Definition: LArFFT.h:69
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
Definition: DataViewImpl.h:633
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
Signal from induction planes.
Definition: geo_types.h:145
enum geo::_plane_sigtype SigType_t
const double a
def move(depos, offset)
Definition: depos.py:107
key_type key() const noexcept
Definition: Ptr.h:216
ProductID put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: DataViewImpl.h:686
void Convolute(std::vector< T > &input, std::vector< T > &respFunc)
Definition: LArFFT.h:173
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.
std::vector< std::vector< TComplex > > fKernelR
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
std::string fDigitModuleLabel
module that made digits
std::vector< std::vector< TComplex > > fKernelS
int fExpEndBins
number of end bins to consider for tail fit
#define MF_LOG_DEBUG(id)
QTextStream & bin(QTextStream &s)
static bool * b
Definition: config.cpp:1043
Declaration of basic channel signal object.
int fPostsample
number of postsample bins
void produce(art::Event &evt)
TCEvent evt
Definition: DataStructs.cxx:7
std::vector< int > fKernMapS
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
CalWire(fhicl::ParameterSet const &pset)
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
Signal from collection planes.
Definition: geo_types.h:146