All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SignalShapingICARUSService_service.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \file SignalShapingICARUSService_service.cc
3 /// \author H. Greenlee
4 /// Modified by X. Qian 1/6/2015
5 /// if histogram is used, inialize
6 /// Response_Offset, Response_Sampling, FieldBins from histogram
7 ////////////////////////////////////////////////////////////////////////
8 
9 #include <cmath>
11 #include "art/Framework/Services/Registry/ServiceHandle.h"
12 #include "art/Framework/Services/Registry/ServiceDefinitionMacros.h"
13 #include "messagefacility/MessageLogger/MessageLogger.h"
14 #include "cetlib_except/exception.h"
15 #include "larcore/CoreUtils/ServiceUtil.h" // lar::providerFrom<>()
16 #include "art_root_io/TFileService.h"
19 #include "TFile.h"
20 
21 // LArSoft include
25 
26 #include "art/Utilities/make_tool.h"
27 #include "tools/IResponse.h"
28 #include "tools/IFieldResponse.h"
30 #include "tools/IFilter.h"
31 
32 #include "tbb/spin_mutex.h"
33 
34 #include <fstream>
35 
36 namespace icarusutil
37 {
38 
39 tbb::spin_mutex signalShapingSpinMutex;
40 
41 //----------------------------------------------------------------------
42 // Constructor.
44  art::ActivityRegistry& /* reg */)
45 : fInit(false)
46 {
47  reconfigure(pset);
48 }
49 
50 //----------------------------------------------------------------------
51 // Reconfigure method.
52 void SignalShapingICARUSService::reconfigure(const fhicl::ParameterSet& pset)
53 {
54  mf::LogInfo("fSignalShapingICARUSService") << " reconfiguring setup " ;
55 
56  // If called again, then we need to clear out the existing tools...
57  fPlaneToResponseMap.clear();
58 
59  // Implement the tools for handling the responses
60  const fhicl::ParameterSet& responseTools = pset.get<fhicl::ParameterSet>("ResponseTools");
61 
62  for(const std::string& responseTool : responseTools.get_pset_names())
63  {
64  const fhicl::ParameterSet& responseToolParamSet = responseTools.get<fhicl::ParameterSet>(responseTool);
65  size_t planeIdx = responseToolParamSet.get<size_t>("Plane");
66 
67  fPlaneToResponseMap[planeIdx].push_back(art::make_tool<icarus_tool::IResponse>(responseToolParamSet));
68  }
69 
70  fPlaneForNormalization = pset.get<size_t>( "PlaneForNormalization");
71  fPrintResponses = pset.get<bool>( "PrintResponses" );
72  fDeconNorm = pset.get<double>( "DeconNorm" );
73  fInitialFFTSize = pset.get<size_t>( "InitialFFTSize" );
74  fNoiseFactVec = pset.get<DoubleVec2>("NoiseFactVec" );
75  fStoreHistograms = pset.get<bool>( "StoreHistograms" );
76 
77  fInit = false;
78 
79  return;
80 }
81 
82 //----------------------------------------------------------------------
83 // Accessor for single-plane signal shaper.
84 //const SignalShapingICARUS& SignalShapingICARUSService::SignalShaping(size_t channel) const
85 //{
86 // if(!fInit) init();
87 //
88 // art::ServiceHandle<geo::Geometry> geom;
89 //
90 // //use channel number to set some useful numbers
91 // size_t planeIdx = geom->ChannelToWire(channel)[0].Plane;
92 //
93 // const SignalShapingICARUS& signalShaping = fPlaneToResponseMap.at(planeIdx).front()->getSignalShapingICARUS();
94 //
95 // return signalShaping;
96 //}
97 
99 {
100  if (!fInit) init();
101 
102  art::ServiceHandle<geo::Geometry> geom;
103 
104  //use channel number to set some useful numbers
105  size_t planeIdx = geom->ChannelToWire(channel)[0].Plane;
106 
107  return *fPlaneToResponseMap.at(planeIdx).front();
108 }
109 
110 
111 //----------------------------------------------------------------------
112 // Initialization method.
113 // Here we do initialization that can't be done in the constructor.
114 // All public methods should ensure that this method is called as necessary.
116 {
117  // First get a lock to make sure we are clear to run
118  tbb::spin_mutex::scoped_lock lock(signalShapingSpinMutex);
119 
120  if(!fInit)
121  {
122  // Do ICARUS-specific configuration of SignalShaping by providing
123  // ICARUS response and filter functions.
124  art::ServiceHandle<geo::Geometry> geo;
125 
126  auto const samplingRate = sampling_rate(art::ServiceHandle<detinfo::DetectorClocksService const>()->DataForJob());
127 
128  // Get the normalization from the field response for the collection plane
129  double integral = fPlaneToResponseMap.at(fPlaneForNormalization).front().get()->getFieldResponse()->getIntegral();
130  double weight = 1. / integral;
131 
132  for(size_t planeIdx = 0; planeIdx < geo->Nplanes(); planeIdx++)
133  {
134  fPlaneToResponseMap[planeIdx].front().get()->setResponse(samplingRate, weight);
135  }
136 
137  // Check to see if we want histogram output
138  if (fStoreHistograms)
139  {
140  art::ServiceHandle<art::TFileService> tfs;
141 
142  // Make sure we are at the top level
143  tfs->file().cd();
144 
145  // Make a directory for these histograms
146  art::TFileDirectory dir = tfs->mkdir("SignalShaping");
147 
148  // Loop through response tools first
149  for(const auto& response: fPlaneToResponseMap) response.second.front().get()->outputHistograms(samplingRate, dir);
150 
151  }
152 
153  fInit = true;
154  }
155 
156  return;
157 }
158 
159 void SignalShapingICARUSService::SetDecon(double const samplingRate,
160  size_t fftsize, size_t channel)
161 {
162  art::ServiceHandle<geo::Geometry> geo;
163 
164  if (!fInit) init();
165 
166  // Assume we need to reset the kernels
167  double integral = fPlaneToResponseMap.at(fPlaneForNormalization).front().get()->getFieldResponse()->getIntegral();
168  double weight = 1. / integral;
169 
170  for(size_t planeIdx = 0; planeIdx < geo->Nplanes(); planeIdx++)
171  {
172  fPlaneToResponseMap.at(planeIdx).front()->setResponse(samplingRate, weight);
173  }
174 }
175 
176 //-----Give Gain Settings to SimWire-----
177 double SignalShapingICARUSService::GetASICGain(unsigned int channel) const
178 {
179  static const double fcToElectrons(6241.50975);
180 
181  art::ServiceHandle<geo::Geometry> geom;
182  size_t planeIdx = geom->ChannelToWire(channel)[0].Plane;
183 
184  double gain = fPlaneToResponseMap.at(planeIdx).front()->getElectronicsResponse()->getFCperADCMicroS() * fcToElectrons;
185 
186  return gain;
187 }
188 
189 //-----Give Shaping time to SimWire-----
190 double SignalShapingICARUSService::GetShapingTime(unsigned int planeIdx) const
191 {
192  double shaping_time = fPlaneToResponseMap.at(planeIdx).front()->getElectronicsResponse()->getASICShapingTime();
193 
194  return shaping_time;
195 }
196 
197 double SignalShapingICARUSService::GetRawNoise(unsigned int const channel) const
198 {
199  art::ServiceHandle<geo::Geometry> geom;
200  size_t planeIdx = geom->ChannelToWire(channel)[0].Plane;
201 
202  double gain = fPlaneToResponseMap.at(planeIdx).front()->getElectronicsResponse()->getFCperADCMicroS();
203  double shaping_time = fPlaneToResponseMap.at(planeIdx).front()->getElectronicsResponse()->getASICShapingTime();
204  int temp;
205 
206  if (std::abs(shaping_time - 0.6)<1e-6){
207  temp = 0;
208  }else if (std::abs(shaping_time - 1.3)<1e-6){
209  temp = 1;
210  }else if (std::abs(shaping_time - 2.0)<1e-6){
211  temp = 2;
212  }else{
213  temp = 3;
214  }
215 
216  double rawNoise;
217 
218  auto tempNoise = fNoiseFactVec.at(planeIdx);
219  rawNoise = tempNoise.at(temp);
220 
221  rawNoise *= gain/4.7;
222  return rawNoise;
223 }
224 
225 double SignalShapingICARUSService::GetDeconNoise(unsigned int const channel) const
226 {
227  art::ServiceHandle<geo::Geometry> geom;
228  size_t planeIdx = geom->ChannelToWire(channel)[0].Plane;
229 
230  double shaping_time = fPlaneToResponseMap.at(planeIdx).front()->getElectronicsResponse()->getASICShapingTime();
231  int temp;
232 
233  if (std::abs(shaping_time - 0.6)<1e-6){
234  temp = 0;
235  }else if (std::abs(shaping_time - 1.3)<1e-6){
236  temp = 1;
237  }else if (std::abs(shaping_time - 2.0)<1.e-6){
238  temp = 2;
239  }else{
240  temp = 3;
241  }
242  auto tempNoise = fNoiseFactVec.at(planeIdx);
243  double deconNoise = tempNoise.at(temp);
244 
245  //deconNoise = deconNoise /4096.*2000./4.7 *6.241*1000/fDeconNorm; <== I don't know where these numbers come from...
246 
247  return deconNoise;
248 }
249 
250 int SignalShapingICARUSService::ResponseTOffset(unsigned int const channel) const
251 {
252  if (!fInit) init();
253 
254  art::ServiceHandle<geo::Geometry> geom;
255 
256  size_t planeIdx = geom->ChannelToWire(channel)[0].Plane;
257 
258  return fPlaneToResponseMap.at(planeIdx).front()->getTOffset();
259 }
260 }
261 
262 namespace icarusutil {
263 
264  DEFINE_ART_SERVICE(SignalShapingICARUSService)
265 
266 }
double GetASICGain(unsigned int const channel) const
Utilities related to art service access.
double GetRawNoise(unsigned int const channel) const
SignalShapingICARUSService(const fhicl::ParameterSet &pset, art::ActivityRegistry &reg)
tbb::spin_mutex signalShapingSpinMutex
double GetShapingTime(unsigned int const planeIdx) const
This is the interface class for a tool to handle the field response It is assumed that the field resp...
DoubleVec2 fNoiseFactVec
RMS noise in ADCs for lowest gain setting.
size_t fPlaneForNormalization
Normalize responses to this plane.
T abs(T value)
std::vector< DoubleVec > DoubleVec2
size_t fInitialFFTSize
Size we initially initalize the responses.
double GetDeconNoise(unsigned int const channel) const
This is the interface class for a tool to handle a filter for the overall response.
void reconfigure(const fhicl::ParameterSet &pset)
tuple dir
Definition: dropbox.py:28
Encapsulate the construction of a single detector plane.
do i e
const icarus_tool::IResponse & GetResponse(size_t channel) const
art::ServiceHandle< art::TFileService > tfs
This is the interface class for a tool to handle the field response It is assumed that the field resp...
This is the interface class for a tool to handle the electronics response.
double sampling_rate(DetectorClocksData const &data)
Returns the period of the TPC readout electronics clock.
int ResponseTOffset(unsigned int const channel) const
void SetDecon(double samplingRate, size_t fftsize, size_t channel)
art framework interface to geometry description
Encapsulate the construction of a single detector plane.