ATTPCROOT  0.3.0-alpha
A ROOT-based framework for analyzing data from active target detectors
AtPatternFission.cxx
Go to the documentation of this file.
1 #include "AtPatternFission.h"
2 
3 #include "AtPattern.h" // for AtPatterns
4 
5 #include <FairLogger.h> // for Logger, LOG
6 
7 #include <Math/Functor.h>
8 #include <TString.h>
9 
10 #include <Fit/FitConfig.h> // for FitConfig
11 #include <Fit/FitResult.h> // for FitResult
12 #include <Fit/Fitter.h>
13 #include <Fit/ParameterSettings.h> // for ParameterSettings
14 #include <algorithm> // for max
15 #include <array> // for array
16 #include <memory> // for allocator, allocator_traits<>::va...
17 
18 using namespace AtPatterns;
19 
21 
22 void AtPatternFission::FitPattern(const std::vector<XYZPoint> &points, const std::vector<double> &charge)
23 {
24  // Based on the example from ROOT documentation https://root.cern.ch/doc/master/line3Dfit_8C_source.html
25 
26  bool weighted = points.size() == charge.size();
27  LOG(debug) << "Fitting with" << (weighted ? " " : "out ") << "charge weighting";
28 
29  // This functor is what we are minimizing. It takes in the model parameters and defines an example
30  // of this pattern based on the model parameters. It then loops through every hit associated with
31  // pattern and calculates the chi2.
32  auto func = [&points, &charge, weighted, this](const double *par) {
33  AtPatternY pat;
34  pat.DefinePattern(std::vector<double>(par, par + 12));
35  std::array<double, 3> chi2 = {0, 0, 0};
36  std::array<double, 3> qTot = {0, 0, 0};
37 
38  for (int i = 0; i < points.size(); ++i) {
39  auto q = weighted ? charge[i] : 1;
40  auto pointAss = this->GetPointAssignment(points[i]);
41 
42  // We can skip including points if they were originally FF points, or they were beam points
43  // and are now FF points and are too close to the center of the detector.
44  bool origFF = pointAss < 2;
45  origFF |= (pointAss == 2 && pat.GetPointAssignment(points[i]) < 2);
46 
47  if (origFF && points[i].Rho() < 20)
48  continue;
49  LOG(debug) << q << " " << pat.DistanceToPattern(points[i]);
50  chi2[pointAss] += pat.DistanceToPattern(points[i]) * pat.DistanceToPattern(points[i]) * q;
51  qTot[pointAss] += q;
52  }
53 
54  double retVal = 0;
55 
56  // Only include the chi2 for the two fission fragments
57  for (int i = 0; i < 2; ++i) {
58  LOG(debug) << i << ": " << chi2[i] << "/" << qTot[i] << " = " << chi2[i] / qTot[i];
59  if (qTot[i] != 0)
60  retVal += chi2[i] / qTot[i];
61  }
62  LOG(debug) << "Obj: " << retVal;
63  return retVal;
64  };
65 
66  auto functor = ROOT::Math::Functor(func, 12);
67 
68  ROOT::Fit::Fitter fitter;
69  auto iniPar = GetPatternPar();
70 
71  LOG(debug) << "Initial parameters";
72  for (int i = 0; i < iniPar.size(); ++i)
73  LOG(debug) << Form("Par_%d", i) << "\t = " << iniPar[i];
74 
75  fitter.SetFCN(functor, iniPar.data());
76 
77  // Constrain the Z direction to be the same
78  for (int i = 0; i < 3; ++i)
79  fitter.Config().ParSettings(5 + i * 3).Fix();
80 
81  for (int i = 0; i < 12; ++i)
82  fitter.Config().ParSettings(i).SetStepSize(.01);
83 
84  bool ok = fitter.FitFCN();
85  if (!ok) {
86  LOG(error) << "Failed to fit the pattern, using result of SAC";
87  DefinePattern(iniPar);
88  return;
89  }
90 
91  auto &result = fitter.Result();
92  DefinePattern(result.Parameters());
93  fChi2 = result.MinFcnValue();
94  fNFree = points.size() - result.NPar();
95 }
AtPatterns::AtPatternY::GetPatternPar
virtual std::vector< double > GetPatternPar() const override
Get list or parameters that describe the pattern.
Definition: AtPatternY.cxx:102
AtPatterns::AtPattern::fNFree
Int_t fNFree
Definition: AtPattern.h:47
AtPattern.h
AtPatterns::AtPatternY::GetPointAssignment
int GetPointAssignment(const XYZPoint &point) const
Definition: AtPatternY.cxx:90
ClassImp
ClassImp(AtPatternFission)
AtPatternFission.h
AtPatterns::AtPattern::fChi2
Double_t fChi2
Definition: AtPattern.h:46
AtPatterns::AtPatternFission
Describes a fission event.
Definition: AtPatternFission.h:22
AtPatterns::AtPatternY::DefinePattern
virtual void DefinePattern(const std::vector< XYZPoint > &points) override
Define based on points.
Definition: AtPatternY.cxx:150
AtPatterns
Definition: AtFissionEvent.h:21
AtPatterns::AtPatternY::DistanceToPattern
virtual Double_t DistanceToPattern(const XYZPoint &point) const override
Closest distance to pattern.
Definition: AtPatternY.cxx:81
AtPatterns::AtPatternY
Describes a Y track.
Definition: AtPatternY.h:29
AtPatterns::AtPatternFission::FitPattern
virtual void FitPattern(const std::vector< XYZPoint > &points, const std::vector< double > &charge) override
Definition: AtPatternFission.cxx:22