VMinMethodFunctor.cc
Go to the documentation of this file.
1 #include "VMinMethodFunctor.h"
2 
3 #include <TMath.h>
4 #include <iostream>
5 #include <cassert>
6 
7 using std::cout;
8 using std::endl;
9 
10 namespace MdLDFFinderAG {
11 
13 {
14 };
15 
16 ROOT::Minuit2::FunctionMinimum
17  VMinMethodFunctor::Minimize(std::vector<double>& pars, std::vector<double>& epars, const std::vector<bool>& fpars)
18 const
19 {
20  using namespace ROOT::Minuit2;
21 
22  DEBUGLOG("Starting");
23 
24  std::ostringstream info("");
25 
26  info.str("");
27  info << "Candidate counters: " << fCandidateCounters << endl;
28  info << "Silent counters: " << fSilentCounters;
29  INFO(info.str());
30 
31  // Check the LDF
32  assert ((*fLDFunction).GetName() == "KascadeGrandeLDF");
33 
34  DEBUGLOG("Start Minuit2 minimization process (LDF \"a la\" KascadeGrande)");
35  MnUserParameters mnpars;
36  mnpars.Add("Nmu" , pars.at(eShowerSize), epars.at(eShowerSize) );
37  mnpars.Add("Beta" , pars.at(eBeta), epars.at(eBeta), 0.0, 4.0 );
38  mnpars.Add("xcore" , pars.at(eCoreX), epars.at(eCoreX) );
39  mnpars.Add("ycore" , pars.at(eCoreY), epars.at(eCoreY) );
40 
41 /* NOTE from http://root.cern.ch/root/html/TMinuit.html
42 
43 The result of all the above is that for parameters with limits,
44 the error reported by Minuit is not exactly equal to the square
45 root of the diagonal element of the error matrix. The difference
46 is a measure of how much the limits deform the problem.
47 If possible, it is suggested not to use limits on parameters,
48 and the problem goes away.
49 If for some reason limits are necessary, and you are sensitive to
50 the difference between the two ways of calculating the errors,
51 it is suggested to use Minos errors which take into account the non-linearities much more precisely.
52 
53 */
54  //Limits and fixed parameters
55  mnpars.SetLowerLimit("Nmu", 0); // Number of muons must be positive
57 // mnpars.SetLowerLimit("Beta",0.0);
58 // mnpars.SetUpperLimit("Beta",4.0); // Disallow steep LDFs
59 
60  //Fix criteria
61  if (fpars.at(eBeta)) {
62  mnpars.Fix("Beta");
63  }
64  if (fpars.at(eCoreX)) {
65  mnpars.Fix("xcore");
66  }
67  if (fpars.at(eCoreY)) {
68  mnpars.Fix("ycore");
69  }
70  DEBUGLOG("After fixing the fit parameters");
71 
72  //Construct from FCNBase + MnUserParameters
73  MnMinimize minuit_mn( static_cast<const FCNBase&>((*this)), mnpars );
74  DEBUGLOG("After creating the minimizer");
75 
76  // Run the minimization
77  //FunctionMinimum is a class holding the full result of the minimization;
78  //both internal and external (MnUserParameterState) representation available
79  //for the parameters at the Minimum
80  FunctionMinimum fnc_min = minuit_mn(10000);
81  DEBUGLOG("After running the minimization");
82 
83  if(!fnc_min.IsValid()) {
84  return fnc_min;
85  }
86 
87 /*
88  if (fnc_min.IsAboveMaxEdm() || fnc_min.HasReachedCallLimit() || std::isnan(fnc_min.UserParameters().Params()[0])) {
89  std::ostringstream info("");
90  info << "\n**************** Minimize FAILED ****************\n"
91  << fnc_min
92  << "**************** Minimize FAILED ****************\n\n";
93  INFO(info.str());
94  return false;
95  }
96 */
97 
98 
99  info.str("");
100  info << "Found minimum after " << fnc_min.NFcn() << " evaluations";
101  INFO(info.str());
102 
103  // Calculate the covariance matrix
104  MnHesse hesseMtrx(1);
105  hesseMtrx(minuit_mn.Fcnbase(), fnc_min);
106 
107  info.str("");
108  info << fnc_min;
109  INFO(info.str());
110 
111  //Get fit results
112  pars = fnc_min.UserParameters().Params();
113  epars = fnc_min.UserParameters().Errors();
114 
115  DEBUGLOG("Finishing");
116 
117  return fnc_min;
118 
119 }
120 
121 void
123 {
124  rAxis=axis;
125  const utl::CoordinateSystemPtr siteCS = det::Detector::GetInstance().GetSiteCoordinateSystem();
126  cosTheta = axis.GetCosTheta(siteCS);
127 }
128 
129 double
130  VMinMethodFunctor::CalculateSilentLikelihood(const std::vector<double>& par)
131 const
132 {
133 
134  double nLogLikelihood = 0;
135 
136  const mdet::MDetector& mDetector = det::Detector::GetInstance().GetMDetector();
137 
138  for (CounterList::const_iterator ic = fSilentCounters.begin();
139  ic != fSilentCounters.end(); ++ic) {
140 
141  mevt::Counter* counter = *ic;
142  unsigned int counterId = counter->GetId();
143 
144  // calculate the number of muons expected in the SD station
145 
146  // core position
147  const double xcore = par[2];
148  const double ycore = par[3];
149  const utl::Point core = utl::Point(xcore, ycore, zcore, siteCS);
150 
151  utl::Point counterPosition = mDetector.GetCounter(counterId).GetPosition();
152 
153  const double coreDistance = DistanceInShowerPlane(counterPosition-core);
154 
155  // muon density in the shower plane
156  const double muonDensity = (*fLDFunction)( coreDistance, &par[0] );
157 
158  // area of the SD station in m2
159  const double sdArea = 10;
160 
161  // average muon signal in the SD, indedpendent of zenith angle!
162  const double muonsInSd = muonDensity * sdArea;
163 
164  // calculate the likelihood of 1 counter
165  double x = 1; // argument of ln
166  for(unsigned int i = 1; i <= fSilentLimit; ++i) {
167  x += TMath::Power(muonsInSd,int(i)) / TMath::Factorial(i);
168  }
169  const double nLogLikelihood1 = muonsInSd - TMath::Log(x);
170 
171  nLogLikelihood += nLogLikelihood1;
172  }
173 
174  return nLogLikelihood;
175 }
176 
177 // CounterList methods
178 
179 std::ostream& operator<<(std::ostream& os, const CounterList& cl)
180 {
181 
182  for (CounterList::const_iterator ic = cl.begin(); ic != cl.end(); ++ic) {
183  os << (*ic)->GetId() << " ";
184  }
185 
186  return os;
187 }
188 
189 
190 
191 
192 
193 } // end namespace
Point object.
Definition: Point.h:32
std::ostream & operator<<(std::ostream &os, const CounterList &cl)
Counter level event data.
ROOT::Minuit2::FunctionMinimum Minimize(std::vector< double > &par, std::vector< double > &epar, const std::vector< bool > &fpar) const
double GetCosTheta(const CoordinateSystemPtr &coordinateSystem) const
cos of zenith (theta) angle
Definition: BasicVector.h:251
double DistanceInShowerPlane(const utl::Vector v) const
#define INFO(message)
Macro for logging informational messages.
Definition: ErrorLogger.h:161
utl::Point GetPosition() const
Detector associated to muon detector hierarchy.
Definition: MDetector.h:32
boost::shared_ptr< const CoordinateTransformer > CoordinateSystemPtr
Shared pointer for coordinate systems.
utl::CoordinateSystemPtr siteCS
#define DEBUGLOG(message)
Macro for logging debugging messages.
Definition: ErrorLogger.h:157
double CalculateSilentLikelihood(const std::vector< double > &par) const
Likelihood of silent counters.
int GetId() const
The id of the counter.
Vector object.
Definition: Vector.h:30
const Counter & GetCounter(int id) const
Retrieve Counter by id.
Definition: MDetector.h:68
virtual ~VMinMethodFunctor()
Meant to be used as base class: virtual destructor.

, generated on Tue Sep 26 2023.