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