FilterWF_module.cc
Go to the documentation of this file.
1 #ifndef FILTERWF_H
2 #define FILTERWF_H
3 
4 ////////////////////////////////////////////////////////////////////////
5 //
6 // FilterWF_module
7 //
8 // bkirby@bnl.gov
9 //
10 // Updated by m.wallbank@sheffield.ac.uk
11 //
12 ////////////////////////////////////////////////////////////////////////
13 
14 // framework
16 #include "art_root_io/TFileService.h"
17 #include "art_root_io/TFileDirectory.h"
20 //dla #include "art/Framework/Services/Optional/detail/TH1AddDirectorySentry.h"
21 //#include "art/Framework/Services/Optional/detail/RootDirectorySentry.h"
29 #include "fhiclcpp/ParameterSet.h"
31 
32 
33 // larsoft
36 
37 //#include "DetectorInfoServices/DetectorPropertiesService.h"
38 
39 // lbne-raw-data
40 #include "lbne-raw-data/Services/ChannelMap/ChannelMapService.h"
41 
42 // C++
43 #include <string>
44 #include <vector>
45 #include <iostream>
46 #include <memory>
47 #include <fstream>
48 #include <sstream>
49 
50 // ROOT
51 #include <TProfile.h>
52 #include <TTree.h>
53 #include <TMath.h>
54 #include "TH3.h"
55 #include "TH2.h"
56 #include "TH1.h"
57 #include "TNtuple.h"
58 #include "TFile.h"
59 //#include <TFileDirectory.h>
60 
61 namespace lbne {
62  class FilterWF;
63 }
64 
66 
67 public:
68 
69  explicit FilterWF(fhicl::ParameterSet const& pset);
70  virtual ~FilterWF();
71 
72  virtual void produce(art::Event& evt) override;
73  void reconfigure(fhicl::ParameterSet const& pset);
74  void beginRun(art::Run& run) override;
75  void beginJob() override;
76  void endRun(art::Run& run) override;
77 
78 private:
79 
81  //std::string fOutputModuleLabel;
83  bool fSkipTicks;
85 
86  int fMethod; //0: group by regulator
87  //1: group by ASIC
88 
91 
92  //TH2D* Channel_vs_tick[3][128];
93 };
94 
95 //-------------------------------------------------------------------
97  this->reconfigure(pset);
98  produces<std::vector<raw::RawDigit> >();
99 }
100 
101 //-------------------------------------------------------------------
103 }
104 
105 //-------------------------------------------------------------------
107  fRawDigitModuleLabel = pset.get<std::string>("RawDigitModuleLabel");
108  fRawDigitModuleInstance = pset.get<std::string>("RawDigitModuleInstance");
109  //fOutputModuleLabel = "filterwf";
110  fSkipStuckCodes = pset.get<bool>("SkipStuckCodes",true);
111  fSkipTicks = pset.get<bool>("SkipTicks",false); // MW: added option to ignore certain tick ranges -- turned off by default
112  fLowerSkipTick = pset.get<int>("LowerSkipTick",0);
113  fUpperSkipTick = pset.get<int>("UpperSkipTick",0);
114  fMethod = pset.get<int>("Method",0); //0: group by regulator
115  //1: group by ASIC
116 }
117 
118 //-------------------------------------------------------------------
120  return;
121 }
122 //-------------------------------------------------------------------
125  /*
126  int NHisto =0;
127  for (int Plane=0; Plane<3; Plane++) {
128  for (int Reg=0; Reg < 128; ++Reg) {
129  ++NHisto;
130  std::stringstream oss;
131  oss << "Channel_vs_tick_" << Plane << "_" << Reg;
132  std::string Title = oss.str();
133  std::cout << oss.str() << " " << Title << std::endl;
134  Channel_vs_tick[Plane][Reg] = tfs->make<TH2D>(Title.c_str(),"Plot of Channel vs Tick; Tick; Channel", 6000, 0., 6000., 14, -0.5, 13.5);
135  }
136  }
137  */
138 }
139 
140 //-------------------------------------------------------------------
142  return;
143 }
144 
145 //-------------------------------------------------------------------
147 
150  std::vector<raw::RawDigit> const& rawDigitVector(*rawDigitHandle);
151 
152  // define temporary vector to hold filtered waveforms
153  const unsigned int n_channels = fGeom->Nchannels();
154  //const unsigned int n_channels = fGeom->rawDigitVector.size();
155  std::vector<std::vector<short> > filterWf(n_channels); // MW: I had to make this a vector of shorts to make it compile (the RawDigit constructor does not accept ADC as float)
156 
157  // raw digit map
158  std::map<int,raw::RawDigit> rawDigitMap;
159  std::map<int,float> pedestalMap;
160  unsigned int maxNumBins = 0;
161  for (std::vector<raw::RawDigit>::const_iterator digitIt = rawDigitVector.begin(); digitIt != rawDigitVector.end(); ++digitIt) {
162  rawDigitMap[digitIt->Channel()] = *digitIt;
163  pedestalMap[digitIt->Channel()] = digitIt->GetPedestal();
164  if (digitIt->NADC() > maxNumBins) maxNumBins = digitIt->NADC();
165  }
166  //std::cout<<"Method = "<<fMethod<<std::endl;
167  // define set of induction and collection channels in each regulator group
168  std::vector<std::vector<std::vector<unsigned int> > > Chs; //Chs[plane id][group id][channel id]
169 
170  if (fMethod == 0){ //group by regulators
171  Chs.resize(3);
172  for (size_t i = 0; i<3; ++i){
173  Chs[i].resize(32);
174  }
175  for (unsigned int i = 0; i<n_channels; ++i){//online channels
176  unsigned int plane = fChannelMap->PlaneFromOnlineChannel(i);
177  unsigned int rce = fChannelMap->RCEFromOnlineChannel(i);
178  unsigned int regulator = fChannelMap->RegulatorFromOnlineChannel(i);
179  Chs[plane][rce*2+regulator].push_back(i);
180  }
181  }
182  else if (fMethod == 1){
183  Chs.resize(3);
184  for (size_t i = 0; i<3; ++i){
185  Chs[i].resize(128);
186  }
187  for (unsigned int i = 0; i<n_channels; ++i){//online channels
188  unsigned int plane = fChannelMap->PlaneFromOnlineChannel(i);
189  unsigned int rce = fChannelMap->RCEFromOnlineChannel(i);
190  unsigned int asic = fChannelMap->ASICFromOnlineChannel(i);
191  //std::cout<<i<<" "<<plane<<" "<<rce<<" "<<asic<<std::endl;
192  Chs[plane][rce*8+asic].push_back(i);
193  }
194  }
195  // derive correction factors - require raw adc waveform and pedestal for each channel
196  std::vector<Double_t> corrVals;
197  // loop through time slices
198  for (unsigned int s = 0; s < maxNumBins; s++) {
199  for (size_t i = 0; i<Chs.size(); ++i){
200  for (size_t j = 0; j<Chs[i].size(); ++j){
201  //if (s==0) std::cout << "Looking at " << s << " " << i << " " << j << " " << Chs[i][j].size() << std::endl;
202  corrVals.clear();
203  for (size_t k = 0; k<Chs[i][j].size(); ++k){
204 
205  unsigned int offlineChan = fChannelMap->Offline(Chs[i][j][k]);
206  if (rawDigitMap.count(offlineChan) == 0)
207  continue;
208  int adc = rawDigitMap.at(offlineChan).ADC(s);
209  if ( fSkipStuckCodes && ( (adc & 0x3F) == 0x0 || (adc & 0x3F) == 0x3F ) )
210  continue;
211  if (adc < 10) //skip "sample dropping" problem
212  continue;
213  double mean = pedestalMap.at(offlineChan);
214  if (mean < 10)
215  continue;
216  corrVals.push_back(adc - mean);
217  }
218 
219  unsigned int corrValSize = corrVals.size();
220  sort(corrVals.begin(),corrVals.end());
221  double correction = 0;
222  if (corrValSize < 2)
223  correction = 0.0;
224  else if ((corrValSize % 2) == 0)
225  correction = (corrVals[corrValSize/2] + corrVals[(corrValSize/2)-1])/2.0;
226  else
227  correction = corrVals[(corrValSize-1)/2];
228 
229  for (size_t k = 0; k<Chs[i][j].size(); ++k){
230 
231  unsigned int offlineChan = fChannelMap->Offline(Chs[i][j][k]);
232  if (rawDigitMap.count(offlineChan) == 0)
233  continue;
234  int adc = rawDigitMap.at(offlineChan).ADC(s);
235  double newAdc = adc - correction;
236  if ( fSkipStuckCodes && ( (adc & 0x3F) == 0x0 || (adc & 0x3F) == 0x3F ) )
237  newAdc = adc; //don't do anything about stuck code, will run stuck code removal later
238  if ( fSkipTicks and s > fLowerSkipTick and s < fUpperSkipTick )
239  newAdc = adc;
240  // if the code unsticker is run first, then don't skip the stuck codes.
241  // if( adc < 10 ) //skip "sample dropping" problem
242  // newAdc = 0;
243  filterWf.at(offlineChan).push_back(newAdc);
244  //if (newAdc < 1000)
245  //Channel_vs_tick[i][j] -> SetBinContent( Channel_vs_tick[i][j]->FindBin(s,k), newAdc);
246  //if (s==0)
247  //std::cout << "Set Channel_vs_tick["<<i<<"]["<<j<<"] bin (" << s << ", " << k << ") to " << Channel_vs_tick[i][j]->GetBinContent( Channel_vs_tick[i][j]->FindBin(s, k) ) << " it should have been " << newAdc << std::endl;
248  }
249  }//all groups
250  }//all planes
251  }// all ticks
252 
253  // loop over channels - save filtered waveforms into digits
254  std::vector<raw::RawDigit> filterRawDigitVector;
255  for (unsigned int ich = 0; ich < n_channels; ich++) {
256  if (rawDigitMap.count(ich) == 0)
257  continue;
258  raw::RawDigit theRawDigit(ich, filterWf.at(ich).size(), filterWf.at(ich));
259  theRawDigit.SetPedestal(rawDigitMap[ich].GetPedestal(),
260  rawDigitMap[ich].GetSigma());
261  filterRawDigitVector.push_back(theRawDigit);
262  }
263 
264  // save filtered waveforms to event
265  evt.put(std::make_unique<decltype(filterRawDigitVector)>(std::move(filterRawDigitVector)));
266 }
267 
269 
270 #endif //FILTERWF_H
void beginRun(art::Run &run) override
Collection of charge vs time digitized from a single readout channel.
Definition: RawDigit.h:69
std::string fRawDigitModuleLabel
std::string string
Definition: nybbler.cc:12
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.h:20
Definition of basic raw digits.
intermediate_table::const_iterator const_iterator
void reconfigure(fhicl::ParameterSet const &pset)
art::ServiceHandle< geo::Geometry > fGeom
Definition: Run.h:21
unsigned int Nchannels() const
Returns the number of TPC readout channels in the detector.
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
Definition: DataViewImpl.h:446
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:69
def move(depos, offset)
Definition: depos.py:107
T get(std::string const &key) const
Definition: ParameterSet.h:231
FilterWF(fhicl::ParameterSet const &pset)
unsigned int fUpperSkipTick
art::ServiceHandle< lbne::ChannelMapService > fChannelMap
void SetPedestal(float ped, float sigma=1.)
Set pedestal and its RMS (the latter is 0 by default)
Definition: RawDigit.cxx:68
void beginJob() override
TCEvent evt
Definition: DataStructs.cxx:7
void endRun(art::Run &run) override
ProductID put(std::unique_ptr< PROD > &&edp, FullSemantic< Level::Run > const semantic)
Definition: DataViewImpl.h:730
double mean(sqlite3 *db, std::string const &table_name, std::string const &column_name)
Definition: statistics.cc:15
virtual void produce(art::Event &evt) override
static QCString * s
Definition: config.cpp:1042
unsigned int fLowerSkipTick
unsigned int run
std::string fRawDigitModuleInstance