ProfileChi2.cc
Go to the documentation of this file.
1 #include "ProfileChi2.h"
2 
3 #include <fevt/EyeRecData.h>
4 
5 #include <evt/GaisserHillas4Parameter.h>
6 
7 #include "../FdEnergyDepositFinderKG/ProfileFitFunctions.h"
8 #include "../FdEnergyDepositFinderKG/GHShapeParameters.h"
9 
10 
11 #include <utl/ErrorLogger.h>
12 #include <limits>
13 
14 #include <fwk/CentralConfig.h>
15 #include <det/Detector.h>
16 
17 using namespace fevt;
18 using namespace utl;
19 using namespace std;
20 
21 namespace FdProfileConstrainedGeometryFit {
22 
24  Branch topBranch = fwk::CentralConfig::GetInstance()->GetTopBranch("FdProfileConstrainedGeometryFit");
25  topBranch.GetChild("leavingAtmoIsError").GetData(fLeavingAtmoIsError);
26  topBranch.GetChild("antiAliasingFilterCorrection").GetData(fAafCorrection); //VN: fitter is static
27  topBranch.GetChild("useNoiseBins").GetData(fUseNoiseBins);
28  ostringstream info;
29  info<<"\n\t leavingAtmoIsError is " << (fLeavingAtmoIsError?"on":"off");
30  info<<"\n\t antiAliasingFilterCorrection is " << (fAafCorrection?"on":"off");
31  info<<"\n\t useNoiseBins is " << (fUseNoiseBins?"on":"off");
32  INFO(info);
33  topBranch.GetChild("onlyDirectLight").GetData(fOnlyDirectLight);
34 
35  const Branch profileBranch = topBranch.GetChild("profile");
36  //VN to manage static fitter
37  //FdEnergyDepositFinderKG::GHShapeParameters ghPars(profileBranch);
38 
39  //fProfileFitter.SetShapePars(ghPars);
40  ghPars = FdEnergyDepositFinderKG::GHShapeParameters(profileBranch);
41 
42  Branch kUnivBranch = profileBranch.GetChild("kUnivConstrained");
43 
44  if (kUnivBranch.GetChild("constrained").Get<bool>()) {
45  fIsUnivConstrained = true;
46  kUnivBranch.GetChild("function").GetData(fUnivFunction);
47  kUnivBranch.GetChild("ksigma").GetData(fksigma);
48  }
49 }
50 
51 double
52 ProfileChi2::operator()(Eye &eyeCopy)
53 {
54  fCFMatrixCalculator.SetMethod(FdEnergyDepositFinderKG::CFMatrixCalculator::eFast);
55  fCFMatrixCalculator.SetOnlyDirect(fOnlyDirectLight);
56  fCFMatrixCalculator.SetVerbosity(-1);
57 
58  try {
59  fCFMatrixCalculator.BuildMatrix(eyeCopy, fLeavingAtmoIsError);
60  } catch (OutOfBoundException&) {
61  return numeric_limits<double>::infinity();
62  }
63  if (!fCFMatrixCalculator.HasMatrix()
64  || fCFMatrixCalculator.GetSize() < 2)
65  {
66  ERROR( "No Matrix built.");
67  return numeric_limits<double>::infinity();
68  }
69 
70  fProfileCalculator.CalculateProfile(eyeCopy, fCFMatrixCalculator, true);
71 
72  if (!fUseLightFlux) {
73  if (FitProfile(eyeCopy,FdEnergyDepositFinderKG::eGH4dEdXChi2)
74  /*&& FitProfile(eyeCopy,FdEnergyDepositFinderKG::eGH2dEdXChi2)*/)
75  {
76  const double chi2pfl =
77  eyeCopy.GetRecData().GetFRecShower().GetGHParameters().GetChiSquare();
78  // /eyeCopy.GetRecData().GetFRecShower().GetGHParameters().GetNdof();
79  return chi2pfl;
80  }
81  else return numeric_limits<double>::infinity();
82  } else {
83  if (/*FitProfile(eyeCopy,FdEnergyDepositFinderKG::eGH2dEdXChi2)
84  &&*/ FitProfile(eyeCopy,FdEnergyDepositFinderKG::eGH4dEdXChi2) //VN GH2 not precise enough -> Xmax bias
85  && FitProfile(eyeCopy,FdEnergyDepositFinderKG::eGH4LightLogLike))
86  {
87  const double chi2pfl = fProfileFitter.GetLHChi2();
88  //VN from Chi2 to Likelihood
89  //eyeCopy.GetRecData().GetFRecShower().GetGHParameters().GetChiSquare();
90  // /eyeCopy.GetRecData().GetFRecShower().GetGHParameters().GetNdof();
91  return chi2pfl;
92  }
93  else return numeric_limits<double>::infinity();
94  }
95 }
96 
97 
98 bool
99 ProfileChi2::FitProfile(Eye& eye,
101 {
102  using namespace FdEnergyDepositFinderKG;
103  using namespace evt;
104 
105  ShowerFRecData& shower = eye.GetRecData().GetFRecShower();
106  //shower.SetEnergyCutoff(fElectronEnergyThreshold);
107  fProfileFitter.ResetStartParameters();
108 
109  if (!shower.HasGHParameters()) {
110  shower.MakeGHParameters(GaisserHillas4Parameter(ghPars.Type()));
111  if (!GuessGHParameters(shower))
112  return false;
113  //DumpCurrentParameters(shower, false, eUndefined);
114  }
115  GaisserHillas4Parameter& gh4Pars = shower.GetGHParameters<GaisserHillas4Parameter>();
116 
117 cout<<"Type: "<< GetFunctionTypeName(ghPars.Type())<<endl;
118 cout<<"L: "<< gh4Pars.GetShapeParameter(gh::eUSP_L)/(g/cm2)<< endl;
119 cout<<"R: "<< gh4Pars.GetShapeParameter(gh::eUSP_R)<< endl;
120 
121 
122  //if (!shower.HasGHParameters<GaisserHillas4Parameter>()) {
123  // return false; // we really need GH4 parameters
124  //}
125 
126 
127  fProfileFitter.SetFitMode(ProfileFitter::eNMax);
128  {
129  const GaisserHillas4Parameter& gh4Pars = shower.GetGHParameters<GaisserHillas4Parameter>();
130  fProfileFitter.SetStartParameters(gh4Pars);
131  }
132  fProfileFitter.SetLightFluxData(fCFMatrixCalculator);
133 
134  fProfileFitter.SetProfileData(shower.GetEnergyDeposit());
135  fProfileFitter.SetFunctionType(type);
136 
137  //VN: in separate inicialization function because fitter is static
138  //fProfileFitter.SetUnivConstrained(fIsUnivConstrained, fA1, fA2, fksigma);
139 
140  if(fProfileFitter.Fit()){
141  GaisserHillas4Parameter& gh4Pars = shower.GetGHParameters<GaisserHillas4Parameter>();
142  fProfileFitter.FillGHParameters(gh4Pars);
143 
144  // return true;
145  double calorEnergy = gh4Pars.GetIntegral();
146  double calorEnergyErr = calorEnergy * gh4Pars.GetIntegralError();
147 
148  // refit at final step using energy as parameter instead of Xmax
149  if (type == eFinalFitStep) {
150  fProfileFitter.SetFitMode(ProfileFitter::eIntegral);
151  fProfileFitter.SetStartParameters(gh4Pars);
152  if (fProfileFitter.Fit())
153  fProfileFitter.GetEnergy(calorEnergy, calorEnergyErr);
154  else
155  return false;
156  }
157  shower.SetEmEnergy(calorEnergy, calorEnergyErr, ShowerFRecData::eStatistical);
158  shower.SetEmEnergyError(0, ShowerFRecData::eAtmospheric);
159 
160  // rebuilt matrix if Xmax changed too much and not in final round
161  const double currXmax = gh4Pars.GetXMax();
162  const double maxDeltaXmax = 10*g/cm2;
163  const bool updateCFM =
164  fabs(currXmax-fCFMatrixCalculator.GetXmax()) > maxDeltaXmax &&
165  type != eFinalFitStep;
166 
167  if (updateCFM)
168  fCFMatrixCalculator.BuildMatrix(eye, fLeavingAtmoIsError);
169 
170  if (fCFMatrixCalculator.HasMatrix()) {
171 
172  fProfileCalculator.CalculateProfile(eye, fCFMatrixCalculator);
173  //DumpCurrentParameters(shower, updateCFM, type);
174 
175  return true;
176  }
177  }
178 
179  return false;
180 }
181 
182 bool
183 ProfileChi2::GuessGHParameters(evt::ShowerFRecData& shower)
184  const
185 {
186  using namespace FdEnergyDepositFinderKG;
187  using namespace evt;
188 
190 
191  // running average
192 
193  const TabulatedFunctionErrors& energyDeposit = shower.GetEnergyDeposit();
194  const unsigned int nPoints = energyDeposit.GetNPoints();
195  const double depthBin = 50*g/cm2;
196 
197  bool firstTime = true;
198  double xMax = 0;
199  double dEdXmax = 0;
200  for (unsigned int i = 0; i < nPoints; ++i) {
201  const double firstDepth = energyDeposit.GetX(i);
202  unsigned int nAverage = 0;
203  double wSum = 0;
204  double ywSum = 0;
205  double xSum = 0;
206  for (unsigned int j = i; j < nPoints; ++j) {
207  const double thisDepth = energyDeposit.GetX(j);
208  ++nAverage;
209  xSum += thisDepth;
210  const double w = 1 / pow(energyDeposit.GetYErr(j), 2);
211  wSum += w;
212  ywSum += energyDeposit.GetY(j)*w;
213 
214  if (thisDepth - firstDepth > depthBin) {
215  const double yAverage = ywSum/wSum;
216  if (firstTime || yAverage > dEdXmax) {
217  dEdXmax = yAverage;
218  xMax = xSum / nAverage;
219  firstTime = false;
220  }
221  break;
222  }
223  }
224  }
225 
226  if (ghPars.Type() == gh::eClassic) {
227  //VN: values better for low energies?
228  gh4Pars.SetShapeParameter(gh::eX0, /*-120*/1*g/cm2, 0);
229  gh4Pars.SetShapeParameter(gh::eLambda, /*61*/86*g/cm2, 0);
230  }
231 
232  if (ghPars.Type() == gh::eUSP) {
233 
234  gh4Pars.SetShapeParameter(ghPars.Type(eOne),
235  ghPars.Mean(eOne,1e18*eV), ghPars.Sigma(eOne,1e18*eV));
236  gh4Pars.SetShapeParameter(ghPars.Type(eTwo),
237  ghPars.Mean(eTwo,1e18*eV), ghPars.Sigma(eTwo,1e18*eV));
238  }
239 
240  if (firstTime) {
241  WARNING("can not estimate start parameters");
242  gh4Pars.SetXMax(700*g/cm2, 0);
243  gh4Pars.SetNMax(1*PeV/g*cm2, 0, true);
244  //gh4Pars.SetNMax(energyDeposit.GetY(0), 0, true);
245  return false;
246  } else {
247  gh4Pars.SetXMax(xMax, 0);
248  gh4Pars.SetNMax(dEdXmax, 0, true);
249  }
250  return true;
251 }
252 
253 } //namespace
const double eV
Definition: GalacticUnits.h:35
unsigned int GetNPoints() const
const double PeV
double GetIntegralError() const
return relative error of integral
Fluorescence Detector Eye Event.
Definition: FEvent/Eye.h:29
#define INFO(message)
Macro for logging informational messages.
Definition: ErrorLogger.h:161
void Init()
Initialise the registry.
Branch GetChild(const std::string &childName) const
Get child of this Branch by child name.
Definition: Branch.cc:211
double pow(const double x, const unsigned int i)
std::string GetFunctionTypeName(const EFunctionType type)
Exception for reporting variable out of valid range.
T Get() const
Definition: Branch.h:271
Class representing a document branch.
Definition: Branch.h:107
evt::ShowerFRecData & GetFRecShower()
Reconstructed shower info for this eye.
Definition: EyeRecData.h:323
constexpr double g
Definition: AugerUnits.h:200
#define WARNING(message)
Macro for logging warning messages.
Definition: ErrorLogger.h:163
void GetData(bool &b) const
Overloads of the GetData member template function.
Definition: Branch.cc:644
static CentralConfig * GetInstance()
Use this the first time you get an instance of central configuration.
Interface class to access to Fluorescence reconstruction of a Shower.
double GetIntegral() const
calculate integral
Gaisser Hillas with 4 parameters.
void MakeGHParameters(const VGaisserHillasParameter &gh)
#define ERROR(message)
Macro for logging error messages.
Definition: ErrorLogger.h:165
utl::Branch GetTopBranch(const std::string &id)
Get top branch for moduleConfigLink with given id (XML files)
utl::TabulatedFunctionErrors & GetEnergyDeposit()
retrieve dE/dX
constexpr double cm2
Definition: AugerUnits.h:118
fevt::EyeRecData & GetRecData()
Reconstructed data for this eye.
Definition: FEvent/Eye.cc:130

, generated on Tue Sep 26 2023.