LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
CalWireT962_module.cc
Go to the documentation of this file.
1 //
3 // CalWireT962 class
4 //
5 // brebel@fnal.gov
6 //
8 
9 // Framework includes
17 #include "cetlib/search_path.h"
18 #include "cetlib_except/exception.h"
19 #include "fhiclcpp/ParameterSet.h"
21 
22 // LArSoft includes
24 #include "larcoreobj/SimpleTypesAndConstants/RawTypes.h" // raw::ChannelID_t
29 #include "lardataobj/RawData/raw.h" // raw::Uncompress()
31 #include "larevt/CalibrationDBI/Interface/ChannelStatusProvider.h"
32 #include "larevt/CalibrationDBI/Interface/ChannelStatusService.h"
33 
34 // ROOT includes
35 #include <TComplex.h>
36 #include <TF1.h>
37 #include <TFile.h>
38 #include <TH2D.h>
39 
41 namespace caldata {
42 
43  class CalWireT962 : public art::EDProducer {
44 
45  public:
46  // create calibrated signals on wires. this class runs
47  // an fft to remove the electronics shaping.
48  explicit CalWireT962(fhicl::ParameterSet const& pset);
49 
50  void produce(art::Event& evt);
51  void beginJob();
52 
53  private:
54  std::string fResponseFile;
55  // for c2: fDataSize is not used
57  // int fDataSize; ///< size of raw data on one wire
60  std::string fDigitModuleLabel;
61 
62  std::vector<std::vector<TComplex>> fKernelR;
63  std::vector<std::vector<TComplex>> fKernelS;
65  std::vector<double> fDecayConstsR;
67  std::vector<double> fDecayConstsS;
69  std::vector<int> fKernMapR;
71  std::vector<int> fKernMapS;
73  protected:
75  }; // class CalWireT962
76 }
77 
78 namespace caldata {
79 
80  //-------------------------------------------------
82  {
83  fDigitModuleLabel = pset.get<std::string>("DigitModuleLabel", "daq");
84  fExpEndBins = pset.get<int>("ExponentialEndBins");
85  fPostsample = pset.get<int>("PostsampleBins");
86 
87  cet::search_path sp("FW_SEARCH_PATH");
88  sp.find_file(pset.get<std::string>("ResponseFile"), fResponseFile);
89 
90  produces<std::vector<recob::Wire>>();
91  produces<art::Assns<raw::RawDigit, recob::Wire>>();
92  }
93 
94  //-------------------------------------------------
96  {
97 
98  MF_LOG_DEBUG("CalWireT962") << "CalWireT962_module: Opening Electronics Response File: "
99  << fResponseFile.c_str();
100 
101  TFile f(fResponseFile.c_str());
102  if (f.IsZombie())
103  mf::LogWarning("CalWireT962") << "Cannot open response file " << fResponseFile.c_str();
104 
105  TH2D* respRe = dynamic_cast<TH2D*>(f.Get("real/RespRe"));
106  TH2D* respIm = dynamic_cast<TH2D*>(f.Get("real/RespIm"));
107  TH1D* decayHist = dynamic_cast<TH1D*>(f.Get("real/decayHist"));
108  unsigned int wires = decayHist->GetNbinsX();
109  unsigned int bins = respRe->GetYaxis()->GetNbins();
110  unsigned int bin = 0;
111  unsigned int wire = 0;
112  fDecayConstsR.resize(wires);
113  fKernMapR.resize(wires);
114  fKernelR.resize(respRe->GetXaxis()->GetNbins());
115  const TArrayD* edges = respRe->GetXaxis()->GetXbins();
116  for (int i = 0; i < respRe->GetXaxis()->GetNbins(); ++i) {
117  fKernelR[i].resize(bins);
118  for (bin = 0; bin < bins; ++bin) {
119 
120  const TComplex a(respRe->GetBinContent(i + 1, bin + 1),
121  respIm->GetBinContent(i + 1, bin + 1));
122  fKernelR[i][bin] = a;
123  }
124  for (; wire < (*edges)[i + 1]; ++wire) {
125  fKernMapR[wire] = i;
126  fDecayConstsR[wire] = decayHist->GetBinContent(wire + 1);
127  }
128  }
129  respRe = dynamic_cast<TH2D*>(f.Get("sim/RespRe"));
130  respIm = dynamic_cast<TH2D*>(f.Get("sim/RespIm"));
131  decayHist = dynamic_cast<TH1D*>(f.Get("sim/decayHist"));
132  wires = decayHist->GetNbinsX();
133  bins = respRe->GetYaxis()->GetNbins();
134  fDecayConstsS.resize(wires);
135  fKernMapS.resize(wires);
136  fKernelS.resize(respRe->GetXaxis()->GetNbins());
137  const TArrayD* edges1 = respRe->GetXaxis()->GetXbins();
138  wire = 0;
139  for (int i = 0; i < respRe->GetXaxis()->GetNbins(); ++i) {
140  fKernelS[i].resize(bins);
141  for (bin = 0; bin < bins; ++bin) {
142  const TComplex b(respRe->GetBinContent(i + 1, bin + 1),
143  respIm->GetBinContent(i + 1, bin + 1));
144  fKernelS[i][bin] = b;
145  }
146  for (; wire < (*edges1)[i + 1]; ++wire) {
147  fKernMapS[wire] = i;
148  fDecayConstsS[wire] = decayHist->GetBinContent(wire + 1);
149  }
150  }
151 
152  f.Close();
153  }
154 
157  {
158 
159  // get the geometry
161 
162  std::vector<double> decayConsts;
163  std::vector<int> kernMap;
164  std::vector<std::vector<TComplex>> kernel;
165  //Put correct response functions and decay constants in place
166  if (evt.isRealData()) {
167  decayConsts = fDecayConstsR;
168  kernMap = fKernMapR;
169  kernel = fKernelR;
170  }
171  else {
172  decayConsts = fDecayConstsS;
173  kernMap = fKernMapS;
174  kernel = fKernelS;
175  }
176 
177  // get the FFT service to have access to the FFT size
179 
180  // make a collection of Wires
181  std::unique_ptr<std::vector<recob::Wire>> wirecol(new std::vector<recob::Wire>);
182  // ... and an association set
183  std::unique_ptr<art::Assns<raw::RawDigit, recob::Wire>> WireDigitAssn(
185 
186  // Read in the digit List object(s).
188  evt.getByLabel(fDigitModuleLabel, digitVecHandle);
189 
190  if (!digitVecHandle->size()) return;
191  mf::LogInfo("CalWireT962") << "CalWireT962:: digitVecHandle size is " << digitVecHandle->size();
192 
193  // Use the handle to get a particular (0th) element of collection.
194  art::Ptr<raw::RawDigit> digitVec0(digitVecHandle, 0);
195 
196  unsigned int dataSize = digitVec0->Samples(); //size of raw data vectors
197 
198  int transformSize = fFFT->FFTSize();
199  raw::ChannelID_t channel = raw::InvalidChannelID; // channel number
200  unsigned int bin(0); // time bin loop variable
201 
202  lariov::ChannelStatusProvider const& channelStatus =
204 
205  double decayConst = 0.; // exponential decay constant of electronics shaping
206  double fitAmplitude = 0.; //This is the seed value for the amplitude in the exponential tail fit
207  std::vector<float> holder; // holds signal data
208  std::vector<TComplex> freqHolder(transformSize + 1); // temporary frequency data
209 
210  // loop over all wires
211  for (unsigned int rdIter = 0; rdIter < digitVecHandle->size(); ++rdIter) { // ++ move
212  holder.clear();
213 
214  art::Ptr<raw::RawDigit> digitVec(digitVecHandle, rdIter);
216  digitVec->Samples()); // vector holding uncompressed adc values
217  raw::Uncompress(digitVec->ADCs(), rawadc, digitVec->Compression());
218  channel = digitVec->Channel();
219 
220  // skip bad channels
221  if (!channelStatus.IsBad(channel)) {
222  holder.resize(transformSize);
223 
224  for (bin = 0; bin < dataSize; ++bin)
225  holder[bin] = (rawadc[bin] - digitVec->GetPedestal());
226  // fExpEndBins only nonzero for detectors needing exponential tail fitting
227  if (fExpEndBins && std::abs(decayConsts[channel]) > 0.0) {
228 
229  TH1D expTailData(
230  "expTailData", "Tail data for fit", fExpEndBins, dataSize - fExpEndBins, dataSize);
231  TF1 expFit("expFit", "[0]*exp([1]*x)");
232 
233  for (bin = 0; bin < (unsigned int)fExpEndBins; ++bin)
234  expTailData.Fill(dataSize - fExpEndBins + bin, holder[dataSize - fExpEndBins + bin]);
235  decayConst = decayConsts[channel];
236  fitAmplitude =
237  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  fFFT->Convolute(holder, kernel[kernMap[channel]]);
252  } // end if channel is a good channel
253 
254  holder.resize(dataSize, 1e-5);
255  //This restores the DC component to signal removed by the deconvolution.
256  if (fPostsample) {
257  double average = 0.0;
258  for (bin = 0; bin < (unsigned int)fPostsample; ++bin)
259  average += holder[holder.size() - 1 - bin] / (double)fPostsample;
260  for (bin = 0; bin < holder.size(); ++bin)
261  holder[bin] -= average;
262  }
263 
264  // Make a single ROI that spans the entire data size
265  wirecol->push_back(recob::WireCreator(holder, *digitVec).move());
266 
267  // add an association between the last object in wirecol
268  // (that we just inserted) and digitVec
269  if (!util::CreateAssn(evt, *wirecol, digitVec, *WireDigitAssn)) {
271  << "Can't associate wire #" << (wirecol->size() - 1) << " with raw digit #"
272  << digitVec.key();
273  } // if failed to add association
274  }
275 
276  if (wirecol->size() == 0) mf::LogWarning("CalWireT962") << "No wires made for this event.";
277 
278  evt.put(std::move(wirecol));
279  evt.put(std::move(WireDigitAssn));
280 
281  return;
282  }
283 
284 } // end namespace caldata
285 
286 namespace caldata {
287 
289 
290 } // 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
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
int fPostsample
number of postsample bins
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< short > ADCvector_t
Type representing a (compressed) vector of ADC counts.
Definition: RawDigit.h:72
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
std::vector< std::vector< TComplex > > fKernelR
std::vector< double > fDecayConstsS
PutHandle< PROD > put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: Event.h:77
TFile f
Definition: plotHisto.C:6
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
std::string fDigitModuleLabel
module that made digits
Collect all the RawData header files together.
key_type key() const noexcept
Definition: Ptr.h:166
std::vector< int > fKernMapS
void Convolute(std::vector< T > &input, std::vector< T > &respFunc)
Definition: LArFFT.h:170
float bin[41]
Definition: plottest35.C:14
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
std::vector< double > fDecayConstsR
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
Utility object to perform functions of association.
#define MF_LOG_DEBUG(id)
Declaration of basic channel signal object.
TCEvent evt
Definition: DataStructs.cxx:8
unsigned int ChannelID_t
Type representing the ID of a readout channel.
Definition: RawTypes.h:28
void produce(art::Event &evt)
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
CalWireT962(fhicl::ParameterSet const &pset)
std::vector< std::vector< TComplex > > fKernelS
art framework interface to geometry description
std::vector< int > fKernMapR
int fExpEndBins
number of end bins to consider for tail fit