PDSPThinSliceFitter.h
Go to the documentation of this file.
1 #ifndef PDSPTHINSLICEFITTER_hh
2 #define PDSPTHINSLICEFITTER_hh
3 
4 #include <string>
5 #include <vector>
6 #include <map>
7 
8 #include "TTree.h"
9 #include "TArrayD.h"
10 #include "TFile.h"
11 #include "THStack.h"
12 #include "Math/Factory.h"
13 #include "Math/Functor.h"
14 #include "Math/Minimizer.h"
15 #include "TRandom3.h"
16 #include "TGraphAsymmErrors.h"
17 #include "TMatrixD.h"
18 #include "TVectorD.h"
19 #include "TDecompChol.h"
20 
21 
22 #include "ThinSliceSample.h"
23 #include "ThinSliceDataSet.h"
24 #include "ThinSliceDriver.h"
25 #include "ThinSliceSystematic.h"
26 #include "ThinSliceEvent.h"
27 
28 namespace protoana {
29 
31  public:
33  void FillMCEvents();
34  void BuildMCSamples();
35  void SaveMCSamples();
36  void GetNominalFluxes();
37  void BuildDataHists();
38  void InitializeMCSamples();
39  void CompareDataMC(
40  std::string extra_name, TDirectory * xsec_dir, TDirectory * plot_dir,
41  bool post_fit = false);
42  void ScaleMCToData();
43  void ScaleDataToNorm();
44  void RunFitAndSave();
46 
47  private:
48  void NormalFit();
49  void Pulls();
50  void Configure(std::string fcl_file);
51  void DefineFitFunction();
52  void MakeMinimizer();
53  void ParameterScans();
54  void DoThrows(const TH1D & pars, const TMatrixD * cov);
55  void Do1DShifts(const TH1D & pars, bool prefit=false);
56  void SetBestFit();
58  std::map<int, std::vector<TH1*>> & throw_hists,
59  std::map<int, std::vector<TH1*>> & throw_inc_hists,
60  std::map<int, std::vector<TH1*>> & throw_xsec_hists);
61  void PlotThrows(
62  std::map<int, std::vector<TH1*>> & throw_hists,
63  std::map<int, std::vector<TH1*>> & truth_throw_hists,
64  std::map<int, std::vector<TH1*>> & truth_inc_hists,
65  std::map<int, std::vector<TH1*>> & truth_xsec_hists);
66  void BuildFakeDataXSecs(bool use_scales = true);
67  void BuildDataFromToy();
68  double CalcChi2SystTerm(), CalcRegTerm();
69  void MakeThrowsTree(TTree & tree, std::vector<double> & branches);
70  //void MakeThrowsArrays(std::vector<TVectorD *> & arrays);
71 
72  std::vector<double> GetBestFitParsVec();
73 
75  std::map<int, std::vector<std::vector<ThinSliceSample>>> fSamples,
78  std::map<int, bool> fIsSignalSample;
79  TFile fMCFile;
80  TTree * fMCTree;
81  TFile fDataFile;
82  TTree * fDataTree;
83  TFile fOutputFile;
84  ROOT::Math::Functor fFitFunction;
85  std::unique_ptr<ROOT::Math::Minimizer> fMinimizer;
86  std::vector<double> fMinimizerInitVals;
88 
89  //std::map<int, TGraphAsymmErrors> fSignalEfficiencies;
90  //std::map<int, std::pair<TH1D, TH1D>> fSignalEffParts;
91 
92  //TGraphAsymmErrors fIncidentEfficiency;
93  //TH1D fIncidentTotal, ;
94 
95  //std::map<int, TH1D> fSelectedDataHists;
96  //std::map<int, TH1D> fRebinnedSelectedDataHists;
97  //TH1D fIncidentDataHist;
98  //TH1D fRebinnedIncidentDataHist;
99 
100  //THStack * fNominalIncidentMCStack;
101  //THStack * fPostFitIncidentMCStack;
102  //std::map<int, THStack *> fNominalSelectedMCStacks;
103  //std::map<int, THStack *> fPostFitSelectedMCStacks;
104 
105  std::map<int, double> fNominalFluxes, fFakeFluxes;
106  std::map<int, std::vector<std::vector<double>>> fFluxesBySample,
108  std::map<int, std::vector<int>> fFluxParsToSamples;
109  double fDataFlux;
110  double fMCDataScale = 1.;
111 
112  std::map<int, std::vector<double>> fSignalParameters;
113  std::map<int, std::vector<std::string>> fSignalParameterNames;
115 
116  std::map<int, double> fFluxParameters;
117  std::map<int, std::string> fFluxParameterNames;
119 
120  //std::map<int, std::string> fSystParameterNames;
121  std::map<std::string, ThinSliceSystematic> fSystParameters;
122  std::vector<std::string> fSystParameterNames;
123  std::vector<double> fParLimits, fParLimitsUp;
125  std::map<std::string, size_t> fCovarianceBins;
127  double fRegFactor = 0.;
129  TDecompChol * fInputChol;
130 
131  std::map<std::string, double> fToyValues;
132 
133  TRandom3 fRNG;
134  std::map<int, std::vector<double>> fFakeDataScales;
135  std::map<int, std::vector<double>> fBestFitSignalPars;
136  std::map<std::string, ThinSliceSystematic> fBestFitSystPars;
137  std::map<int, double> fBestFitFluxPars;
138  std::map<int, TH1*> fNominalXSecs, fNominalIncs;
139  std::map<int, TH1*> fBestFitXSecs, fBestFitIncs;
140  std::map<int, TH1*> fFakeDataXSecs, fFakeDataIncs;
141 
142  std::map<int, TH1D*> fBestFitSelectionHists;
143  std::map<int, std::vector<double>> fBestFitTruthVals;
144 
145  std::vector<ThinSliceEvent> fEvents, fFakeDataEvents;
146 
147  //Configurable members
151  std::vector<fhicl::ParameterSet> fSelectionSets;
152  std::vector<fhicl::ParameterSet> fSampleSets;
153  std::map<int, std::string> fFluxTypes;
155  size_t fNFitSteps = 0;
156  unsigned int fNScanSteps;
158  std::vector<std::pair<int, int>> fPlotStyle;
164  double fPitch;
169  std::map<std::string, double> fSystsToFix;
172  bool fSplitMC;
173  int fSplitVal = 0;
175  bool fFitUnderOverflow = false;
176  bool fUseFakeSamples = false;
177  bool fFitFlux;
179  std::string fFitType = "Normal";
180  size_t fNPulls;
182  double fDataNorm;
183 
187  std::map<int, std::vector<double>> fSignalBins;
188  //////////////////////////
189 
190  double BetheBloch(double energy, double mass) {
191  double K,rho,Z,A, charge, me, I, gamma, /*momentum ,*/wmax, pitch;
192  long double beta;
193  K = 0.307;
194  rho = 1.4;
195  charge = 1;
196  Z = 18;
197  A = 39.948;
198  I = pow(10,-6)*10.5*18; //MeV
199  me = 0.51; //MeV me*c^2
200  pitch = 1;
201 
202  //momentum = sqrt( pow(energy,2) - pow(massicle,2));
203  //beta = momentum/sqrt(pow(massicle,2) + pow(momentum,2));
204  //gamma = 1/sqrt(1 - pow(beta,2));
205 
206  gamma = (energy + mass) / mass;
207  beta = sqrt( 1 - 1/pow(gamma,2));
208 
209  wmax = 2*me*pow(beta,2)*pow(gamma,2)/(1+2*gamma*me/mass + pow(me,2)/pow(mass,2));
210 
211 
212  double dEdX;
213  //multiply by rho to have dEdX MeV/cm in LAr
214 
215  dEdX = pitch*(rho*K*Z*pow(charge,2))/(A*pow(beta,2))*(0.5*log(2*me*pow(gamma,2)*pow(beta,2)*wmax/pow(I,2)) - pow(beta,2) - densityEffect( beta, gamma )/2 );
216 
217  return dEdX;
218  };
219 
220  double densityEffect(double beta, double gamma) {
221  double lar_C = 5.215, lar_x0 = 0.201, lar_x1 = 3, lar_a = 0.196, lar_k = 3;
222  long double x = log10(beta * gamma);
223 
224  if( x >= lar_x1 ) return 2*log(10)*x - lar_C;
225 
226  else if ( lar_x0 <= x && x < lar_x1) return 2*log(10)*x - lar_C + lar_a * pow(( lar_x1 - x ) , lar_k );
227 
228  else return 0.; //if x < lar_x0
229  };
230 };
231 
232 }
233 #endif
std::vector< ThinSliceEvent > fEvents
std::vector< int > fMeasurementSamples
std::map< int, TH1 * > fBestFitXSecs
std::map< int, std::vector< std::vector< ThinSliceSample > > > fSamples
double beta(double KE, const simb::MCParticle *part)
std::map< int, TH1 * > fFakeDataXSecs
void Configure(std::string fcl_file)
std::vector< double > fTrueIncidentBins
std::map< int, std::vector< int > > fFluxParsToSamples
std::string string
Definition: nybbler.cc:12
std::map< std::string, ThinSliceSystematic > fSystParameters
std::vector< double > fBeamEnergyBins
void MakeThrowsTree(TTree &tree, std::vector< double > &branches)
std::map< int, double > fFluxParameters
std::vector< std::pair< int, int > > fPlotStyle
void GetCurrentTruthHists(std::map< int, std::vector< TH1 * >> &throw_hists, std::map< int, std::vector< TH1 * >> &throw_inc_hists, std::map< int, std::vector< TH1 * >> &throw_xsec_hists)
constexpr T pow(T x)
Definition: pow.h:72
std::map< int, TH1D * > fBestFitSelectionHists
std::map< int, TH1 * > fNominalXSecs
std::vector< double > fIncidentRecoBins
std::vector< ThinSliceEvent > fFakeDataEvents
std::vector< double > fParLimitsUp
std::map< std::string, double > fToyValues
std::map< int, double > fBestFitFluxPars
std::map< int, std::vector< std::vector< double > > > fFakeFluxesBySample
std::map< int, double > fFakeFluxes
std::vector< std::string > fSystParameterNames
std::vector< fhicl::ParameterSet > fSampleSets
std::map< int, std::vector< double > > fBestFitTruthVals
std::map< int, TH1 * > fBestFitIncs
double dEdX(double KE, const simb::MCParticle *part)
std::map< int, std::vector< double > > fBestFitSignalPars
std::map< std::string, ThinSliceSystematic > fBestFitSystPars
std::map< int, std::vector< std::string > > fSignalParameterNames
std::unique_ptr< ROOT::Math::Minimizer > fMinimizer
std::map< std::string, size_t > fCovarianceBins
double densityEffect(double beta, double gamma)
std::map< int, std::vector< double > > fSignalParameters
std::map< int, std::vector< std::vector< ThinSliceSample > > > fFakeSamples
std::map< int, TH1 * > fFakeDataIncs
PDSPThinSliceFitter(std::string fcl_file, std::string output_file)
std::map< int, TH1 * > fNominalIncs
std::map< int, std::vector< std::vector< double > > > fFluxesBySample
double gamma(double KE, const simb::MCParticle *part)
double BetheBloch(double energy, double mass)
std::map< int, std::vector< double > > fSignalBins
void PlotThrows(std::map< int, std::vector< TH1 * >> &throw_hists, std::map< int, std::vector< TH1 * >> &truth_throw_hists, std::map< int, std::vector< TH1 * >> &truth_inc_hists, std::map< int, std::vector< TH1 * >> &truth_xsec_hists)
std::vector< double > fMinimizerInitVals
std::map< int, std::string > fFluxParameterNames
void CompareDataMC(std::string extra_name, TDirectory *xsec_dir, TDirectory *plot_dir, bool post_fit=false)
std::vector< fhicl::ParameterSet > fSelectionSets
#define A
Definition: memgrp.cpp:38
std::vector< double > GetBestFitParsVec()
std::map< int, std::vector< double > > fFakeDataScales
list x
Definition: train.py:276
std::map< std::string, double > fSystsToFix
std::map< int, bool > fIsSignalSample
std::map< int, std::string > fFluxTypes
void BuildFakeDataXSecs(bool use_scales=true)
std::vector< double > fParLimits
void Do1DShifts(const TH1D &pars, bool prefit=false)
void DoThrows(const TH1D &pars, const TMatrixD *cov)
fhicl::ParameterSet fAnalysisOptions
std::map< int, double > fNominalFluxes