ProtoDUNECalibration.cxx
Go to the documentation of this file.
1 #include "ProtoDUNECalibration.h"
2 #include "cetlib/search_path.h"
3 #include "cetlib/filesystem.h"
5 
6 
8  : //planeID(pset.get<unsigned int>("PlaneID")),
9  betap(pset.get<double>("betap")),
10  Rho(pset.get<double>("Rho")),
11  Wion(pset.get<double>("Wion")),
12  alpha(pset.get<double>("alpha")),
13  //norm_factor(pset.get<double>("norm_factor")),
14  //calib_factor(pset.get<double>("calib_factor")),
15  X_correction_name(pset.get<std::string>("X_correction")),
16  YZ_correction_name(pset.get<std::string>("YZ_correction")),
17  E_field_correction_name(pset.get<std::string>("E_field_correction")) {
18 
19 
20  /*
21  X_correction_file = new TFile(X_correction_name.c_str(), "OPEN");
22  YZ_correction_file = new TFile(YZ_correction_name.c_str(), "OPEN");
23  E_field_file = new TFile(E_field_correction_name.c_str(), "OPEN");
24  */
28 
29  std::vector<fhicl::ParameterSet> PlaneParameters =
30  pset.get<std::vector<fhicl::ParameterSet>>("PlaneParameters");
31 
32  for (size_t i = 0; i < PlaneParameters.size(); ++i) {
33  size_t planeID = PlaneParameters[i].get<size_t>("PlaneID");
34  norm_factors[planeID] = PlaneParameters[i].get<double>("norm_factor");
35  calib_factors[planeID] = PlaneParameters[i].get<double>("calib_factor");
36 
37  std::string hist_name = "dqdx_X_correction_hist_" + std::to_string(planeID);
38  X_correction_hists[planeID] = (TH1F*)X_correction_file->Get( hist_name.c_str() );
39 
40  std::string YZ_neg_name = "correction_dqdx_ZvsY_negativeX_hist_" +
41  std::to_string(planeID);
42  YZ_neg_hists[planeID] = (TH2F*)YZ_correction_file->Get(YZ_neg_name.c_str());
43 
44  std::string YZ_pos_name = "correction_dqdx_ZvsY_positiveX_hist_" +
45  std::to_string(planeID);
46  YZ_pos_hists[planeID] = (TH2F*)YZ_correction_file->Get(YZ_pos_name.c_str());
47  }
48 
49  ex_neg = (TH3F*)E_field_file->Get("Reco_ElecField_X_Neg");
50  ey_neg = (TH3F*)E_field_file->Get("Reco_ElecField_Y_Neg");
51  ez_neg = (TH3F*)E_field_file->Get("Reco_ElecField_Z_Neg");
52  ex_pos = (TH3F*)E_field_file->Get("Reco_ElecField_X_Pos");
53  ey_pos = (TH3F*)E_field_file->Get("Reco_ElecField_Y_Pos");
54  ez_pos = (TH3F*)E_field_file->Get("Reco_ElecField_Z_Pos");
55 
56 
57  /*
58  std::cout << "Calibration" << std::endl;
59  std::cout << planeID << std::endl;
60  std::cout << betap << std::endl;
61  std::cout << Rho << std::endl;
62  std::cout << Wion << std::endl;
63  std::cout << norm_factor << std::endl;
64  std::cout << calib_factor << std::endl;
65  */
66 }
67 
69  const recob::Track &track, art::Event const &evt,
70  const std::string trackModule, const std::string caloModule,
71  size_t planeID, double negativeZFix) {
72 
73  std::vector<float> calibrated_dEdx;
74 
75  //If we can't find the plane ID in the configuration, return empty vector
76  if (norm_factors.find(planeID) == norm_factors.end())
77  return calibrated_dEdx;
78 
79  //Get the Calorimetry vector from the track
80  std::vector< anab::Calorimetry > caloVector = trackUtil.GetRecoTrackCalorimetry( track, evt, trackModule, caloModule );
81 
82  size_t calo_position;
83  bool found_plane = false;
84  for( size_t i = 0; i < caloVector.size(); ++i ){
85  unsigned int thePlane = caloVector.at(i).PlaneID().Plane;
86  if( thePlane == planeID ){
87  calo_position = i;
88  found_plane = true;
89  break;
90  }
91  }
92 
93  if( !found_plane ){
94  std::cout << "Could not find the correct plane in the calorimetry vector" << std::endl;
95  return calibrated_dEdx;
96  }
97 
98  std::vector< float > dQdX = caloVector.at( calo_position).dQdx();
99  auto theXYZPoints = caloVector.at( calo_position).XYZ();
100  std::vector< float > resRange = caloVector.at( calo_position ).ResidualRange();
101 
102  //Get the hits from the track from a specific plane
103  const std::vector< const recob::Hit* > hits = trackUtil.GetRecoTrackHitsFromPlane( track, evt, trackModule, planeID );
104  if( hits.size() == 0 ){
105  std::cout << "Got empty hits vector" << std::endl;
106  return calibrated_dEdx;
107  }
108 
109  if (negativeZFix > 0.) {
110  return calibrated_dEdx;
111  }
112 
113  double z_check = negativeZFix;
114 
115  //Do Ajib's correction
116  for( size_t i = 0; i < dQdX.size(); ++i ){
117  float hit_x = theXYZPoints[i].X();
118  float hit_y = theXYZPoints[i].Y();
119  float hit_z = theXYZPoints[i].Z();
120 
121  //if( hit_y < 0. || hit_y > 600. ) continue;
122  //if( hit_z < z_check || hit_z > 695. ) continue;
123  if (hit_y < 0. || hit_y > 600. || hit_z < z_check || hit_z > 695.) {
124  calibrated_dEdx.push_back(-999.);
125  continue;
126  }
127 
128  //Set the z position to 0. for small (configurable) negative positions
129  if (negativeZFix < hit_z && hit_z < 0.) {
130  //std::cout << "Fixing: " << hit_z << " " << negativeZFix << std::endl;
131  hit_z = 0.;
132  }
133 
134 
135  int X_bin = X_correction_hists[planeID]->FindBin(hit_x);
136  float X_correction = X_correction_hists[planeID]->GetBinContent(X_bin);
137 
138  TH2F * YZ_hist = (hit_x < 0 ?
139  YZ_neg_hists[planeID] :
140  YZ_pos_hists[planeID]);
141  int YZ_bin = YZ_hist->FindBin(hit_z, hit_y);
142  double YZ_correction = YZ_hist->GetBinContent(YZ_bin);
143 
144 
145  float corrected_dq_dx = dQdX[i] * X_correction *
146  YZ_correction * norm_factors[planeID];
147  float scaled_corrected_dq_dx = corrected_dq_dx / calib_factors[planeID];
148 
149  double Efield = tot_Ef( hit_x, hit_y, hit_z );
150 
151 
152  float cal_de_dx = calc_dEdX( scaled_corrected_dq_dx, betap, Rho, Efield, Wion, alpha );
153 
154  calibrated_dEdx.push_back( cal_de_dx );
155  }
156 
157 
158  return calibrated_dEdx;
159 }
160 
162  const recob::Track &track, art::Event const &evt,
163  const std::string trackModule, const std::string caloModule,
164  size_t planeID, double negativeZFix) {
165  std::vector<double> calibrated_dQdX;
166 
167  //If we can't find the plane ID in the configuration, return empty vector
168  if (norm_factors.find(planeID) == norm_factors.end())
169  return calibrated_dQdX;
170 
171  //Get the Calorimetry vector from the track
172  std::vector< anab::Calorimetry > caloVector = trackUtil.GetRecoTrackCalorimetry( track, evt, trackModule, caloModule );
173 
174  size_t calo_position;
175  bool found_plane = false;
176  for( size_t i = 0; i < caloVector.size(); ++i ){
177  unsigned int thePlane = caloVector.at(i).PlaneID().Plane;
178  if( thePlane == planeID ){
179  calo_position = i;
180  found_plane = true;
181  break;
182  }
183  }
184 
185  if( !found_plane ){
186  std::cout << "Could not find the correct plane in the calorimetry vector" << std::endl;
187  return calibrated_dQdX;
188  }
189 
190  std::vector< float > dQdX = caloVector.at( calo_position).dQdx();
191  auto theXYZPoints = caloVector.at( calo_position).XYZ();
192 
193  //Get the hits from the track from a specific plane
194  const std::vector< const recob::Hit* > hits = trackUtil.GetRecoTrackHitsFromPlane( track, evt, trackModule, planeID );
195  if( hits.size() == 0 ){
196  std::cout << "Got empty hits vector" << std::endl;
197  return calibrated_dQdX;
198  }
199 
200  if (negativeZFix > 0.) {
201  return calibrated_dQdX;
202  }
203 
204  double z_check = negativeZFix;
205 
206  //Do Ajib's correction
207  for( size_t i = 0; i < dQdX.size(); ++i ){
208  double hit_x = theXYZPoints[i].X();
209  double hit_y = theXYZPoints[i].Y();
210  double hit_z = theXYZPoints[i].Z();
211 
212  //if( hit_y < 0. || hit_y > 600. ) continue;
213  //if( hit_z < z_check || hit_z > 695. ) continue;
214  if (hit_y < 0. || hit_y > 600. || hit_z < z_check || hit_z > 695.) {
215  calibrated_dQdX.push_back(-999.);
216  continue;
217  }
218 
219  //Set the z position to 0. for small (configurable) negative positions
220  if (negativeZFix < hit_z && hit_z < 0.) {
221  hit_z = 0.;
222  }
223 
224 
225  int X_bin = X_correction_hists[planeID]->FindBin(hit_x);
226  double X_correction = X_correction_hists[planeID]->GetBinContent(X_bin);
227 
228  TH2F * YZ_hist = (hit_x < 0 ?
229  YZ_neg_hists[planeID] :
230  YZ_pos_hists[planeID]);
231  int YZ_bin = YZ_hist->FindBin(hit_z, hit_y);
232  double YZ_correction = YZ_hist->GetBinContent(YZ_bin);
233 
234 
235  double corrected_dq_dx = dQdX[i] * X_correction *
236  YZ_correction * norm_factors[planeID];
237  double scaled_corrected_dq_dx = corrected_dq_dx / calib_factors[planeID];
238 
239  calibrated_dQdX.push_back(scaled_corrected_dq_dx);
240  }
241  return calibrated_dQdX;
242 }
243 
244 float protoana::ProtoDUNECalibration::calc_dEdX(double dqdx, double betap, double Rho, double Efield, double Wion, double alpha){
245  return ( exp( dqdx * ( betap / ( Rho * Efield ) * Wion ) ) -alpha ) / ( betap / ( Rho*Efield ) );
246 }
247 
248 double protoana::ProtoDUNECalibration::tot_Ef( double x, double y, double z ){
249 
250  if( x >= 0 ){
251  double ex = 0.5 + 0.5 * ex_pos->GetBinContent( ex_pos->FindBin( x, y, z ) );
252  double ey = 0.5 * ey_pos->GetBinContent( ey_pos->FindBin( x, y, z ) );
253  double ez = 0.5 * ez_pos->GetBinContent( ez_pos->FindBin( x, y, z ) );
254  return sqrt( (ex*ex) + (ey*ey) + (ez*ez) );
255  }
256  else if( x < 0 ){
257  double ex= 0.5 + 0.5 * ex_neg->GetBinContent( ex_neg->FindBin( x, y, z ) );
258  double ey= 0.5 * ey_neg->GetBinContent( ey_neg->FindBin( x, y, z ) );
259  double ez= 0.5 * ez_neg->GetBinContent( ez_neg->FindBin( x, y, z ) );
260  return sqrt( (ex*ex) + (ey*ey) + (ez*ez) );
261  }
262  else return 0.5;
263 }
264 
266  const recob::Track &track, art::Event const &evt,
267  const std::string trackModule, const std::string caloModule,
268  size_t planeID, double negativeZFix) {
269  std::vector<double> results;
270 
271  //Get the Calorimetry vector from the track
272  std::vector< anab::Calorimetry > caloVector = trackUtil.GetRecoTrackCalorimetry( track, evt, trackModule, caloModule );
273 
274  size_t calo_position;
275  bool found_plane = false;
276  for( size_t i = 0; i < caloVector.size(); ++i ){
277  unsigned int thePlane = caloVector.at(i).PlaneID().Plane;
278  if( thePlane == planeID ){
279  calo_position = i;
280  found_plane = true;
281  break;
282  }
283  }
284 
285  if( !found_plane ){
286  std::cout << "Could not find the correct plane in the calorimetry vector" << std::endl;
287  return results;
288  }
289 
290  auto theXYZPoints = caloVector.at( calo_position).XYZ();
291 
292  //Get the hits from the track from a specific plane
293  const std::vector< const recob::Hit* > hits = trackUtil.GetRecoTrackHitsFromPlane( track, evt, trackModule, planeID );
294  if( hits.size() == 0 ){
295  std::cout << "Got empty hits vector" << std::endl;
296  return results;
297  }
298 
299  if (negativeZFix > 0.) {
300  return results;
301  }
302 
303  double z_check = negativeZFix;
304 
305  for( size_t i = 0; i < theXYZPoints.size(); ++i ){
306  float hit_x = theXYZPoints[i].X();
307  float hit_y = theXYZPoints[i].Y();
308  float hit_z = theXYZPoints[i].Z();
309 
310  if( hit_y < 0. || hit_y > 600. || hit_z < z_check || hit_z > 695.) {
311  results.push_back(-999.);
312  continue;
313  }
314 
315  //Set the z position to 0. for small (configurable) negative positions
316  if (negativeZFix < hit_z && hit_z < 0.) {
317  //std::cout << "Fixing: " << hit_z << " " << negativeZFix << std::endl;
318  hit_z = 0.;
319  }
320  results.push_back(tot_Ef( hit_x, hit_y, hit_z ));
321  }
322 
323  return results;
324 }
325 
327  const art::Ptr<recob::Hit> hit, double X, double Y, double Z,
328  double recomb_factor) {
329 
330  //Only do collection plane
331  //if( hit->View() != 2 ) return 0.;
332  size_t planeID = hit->View();
333  if (norm_factors.find(planeID) == norm_factors.end())
334  return 0.;
335 
336  int X_bin = X_correction_hists[planeID]->FindBin(X);
337  double X_factor = X_correction_hists[planeID]->GetBinContent(X_bin);
338  double YZ_factor = 1.;
339  if(X < 0.){
340  int YZ_bin = YZ_neg_hists[planeID]->FindBin(Z, Y);
341  YZ_factor = YZ_neg_hists[planeID]->GetBinContent(YZ_bin);
342  }
343  else{
344  int YZ_bin = YZ_pos_hists[planeID]->FindBin(Z, Y);
345  YZ_factor = YZ_pos_hists[planeID]->GetBinContent(YZ_bin);
346  }
347 
348  double energy = hit->Integral();
349  energy *= norm_factors[planeID];
350  energy *= Wion/*23.6e-6*/;
351  energy /= calib_factors[planeID];
352  energy *= X_factor;
353  energy *= YZ_factor;
354  energy /= recomb_factor;
355 
356  return energy;
357 
358 }
359 
361  TFile * theFile = 0x0;
362  mf::LogInfo("protoana::ProtoDUNECalibration::OpenFile") << "Searching for " << filename;
363  if (cet::file_exists(filename)) {
364  mf::LogInfo("protoana::ProtoDUNECalibration::OpenFile") << "File exists. Opening " << filename;
365  theFile = new TFile(filename.c_str());
366  if (!theFile ||theFile->IsZombie() || !theFile->IsOpen()) {
367  delete theFile;
368  theFile = 0x0;
369  throw cet::exception("ProtoDUNECalibration.cxx") << "Could not open " << filename;
370  }
371  }
372  else {
373  mf::LogInfo("protoana::ProtoDUNECalibration::OpenFile") << "File does not exist here. Searching FW_SEARCH_PATH";
374  cet::search_path sp{"FW_SEARCH_PATH"};
375  std::string found_filename;
376  auto found = sp.find_file(filename, found_filename);
377  if (!found) {
378  throw cet::exception("ProtoDUNECalibration.cxx") << "Could not find " << filename;
379  }
380 
381  mf::LogInfo("protoana::ProtoDUNECalibration::OpenFile") << "Found file " << found_filename;
382  theFile = new TFile(found_filename.c_str());
383  if (!theFile ||theFile->IsZombie() || !theFile->IsOpen()) {
384  delete theFile;
385  theFile = 0x0;
386  throw cet::exception("ProtoDUNECalibration.cxx") << "Could not open " << found_filename;
387  }
388  }
389  return theFile;
390 }
std::vector< double > GetEFieldVector(const recob::Track &track, art::Event const &evt, const std::string trackModule, const std::string caloModule, size_t planeID, double negativeZFix=0.)
double betap
Definition: doAna.cpp:14
std::string string
Definition: nybbler.cc:12
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
std::map< size_t, TH2F * > YZ_neg_hists
std::vector< anab::Calorimetry > GetRecoTrackCalorimetry(const recob::Track &track, art::Event const &evt, const std::string trackModule, const std::string caloModule) const
Get the Calorimetry(s) from a given reco track.
STL namespace.
float calc_dEdX(double dqdx, double betap, double Rho, double Efield, double Wion, double alpha)
float Integral() const
Integral under the calibrated signal waveform of the hit, in tick x ADC units.
Definition: Hit.h:224
geo::View_t View() const
View for the plane of the hit.
Definition: Hit.h:232
string filename
Definition: train.py:213
std::map< size_t, TH1F * > X_correction_hists
std::map< size_t, double > calib_factors
double HitToEnergy(const art::Ptr< recob::Hit > hit, double X, double Y, double Z, double recomb_factor=.6417)
std::vector< double > CalibratedQdX(const recob::Track &track, art::Event const &evt, const std::string trackModule, const std::string caloModule, size_t planeID, double negativeZFix)
T get(std::string const &key) const
Definition: ParameterSet.h:271
double Rho
Definition: doAna.cpp:13
double alpha
Definition: doAna.cpp:15
Detector simulation of raw signals on wires.
std::map< size_t, TH2F * > YZ_pos_hists
const std::vector< const recob::Hit * > GetRecoTrackHitsFromPlane(const recob::Track &track, art::Event const &evt, const std::string trackModule, unsigned int planeID) const
Get the hits from a given reco track from a specific plane.
std::map< size_t, double > norm_factors
double tot_Ef(double, double, double)
list x
Definition: train.py:276
bool file_exists(std::string const &qualified_filename)
Definition: filesystem.cc:14
TCEvent evt
Definition: DataStructs.cxx:7
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
double Wion
Definition: doAna.cpp:16
std::vector< float > GetCalibratedCalorimetry(const recob::Track &track, art::Event const &evt, const std::string trackModule, const std::string caloModule, size_t planeID, double negativeZFix=0.)
TFile * OpenFile(const std::string filename)
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34
Track from a non-cascading particle.A recob::Track consists of a recob::TrackTrajectory, plus additional members relevant for a "fitted" track:
Definition: Track.h:49
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
QTextStream & endl(QTextStream &s)