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