GRSISort "v4.1.1.0"
An extension of the ROOT analysis Framework
Loading...
Searching...
No Matches
TDecay.h
Go to the documentation of this file.
1// Author: Ryan Dunlop 09/15
2#ifndef TDECAY_H
3#define TDECAY_H
4
5/** \addtogroup Fitting Fitting & Analysis
6 * @{
7 */
8
9#include "TNamed.h"
10#include "TFitResult.h"
11#include "TFitResultPtr.h"
12#include "TGraph.h"
13#include "TF1.h"
14#include "TH1.h"
15
16class TVirtualDecay;
17class TSingleDecay;
18class TDecay;
19class TDecayChain;
20
21class TDecayFit : public TF1 {
22public:
23 TDecayFit() = default;
24 TDecayFit(const char* name, const char* formula, Double_t xmin = 0, Double_t xmax = 1)
25 : TF1(name, formula, xmin, xmax)
26 {
28 }
29 TDecayFit(const char* name, Double_t xmin, Double_t xmax, Int_t npar) : TF1(name, xmin, xmax, npar)
30 {
32 }
33 TDecayFit(const char* name, const ROOT::Math::ParamFunctor& f, Double_t xmin = 0, Double_t xmax = 1, Int_t npar = 0)
34 : TF1(name, f, xmin, xmax, npar)
35 {
37 }
38#if !defined(__CINT__) && !defined(__CLING__)
39 TDecayFit(const char* name, Double_t (*fcn)(Double_t*, Double_t*), Double_t xmin = 0, Double_t xmax = 1, Int_t npar = 0)
40 : TF1(name, fcn, xmin, xmax, npar)
41 {
43 }
44 TDecayFit(const char* name, Double_t (*fcn)(const Double_t*, const Double_t*), Double_t xmin = 0, Double_t xmax = 1, Int_t npar = 0)
45 : TF1(name, fcn, xmin, xmax, npar)
46 {
48 }
49#endif
50
51 template <class PtrObj, typename MemFn>
52 TDecayFit(const char* name, const PtrObj& p, MemFn memFn, Double_t xmin, Double_t xmax, Int_t npar)
53 : TF1(name, p, memFn, xmin, xmax, npar)
54 {
56 }
57
58 template <typename Func>
59 TDecayFit(const char* name, Func f, Double_t xmin, Double_t xmax, Int_t npar, const char* className = nullptr)
60 : TF1(name, f, xmin, xmax, npar, className)
61 {
63 }
64 TDecayFit(const TDecayFit&) = default;
65 TDecayFit(TDecayFit&&) noexcept = default;
66 TDecayFit& operator=(const TDecayFit&) = default;
67 TDecayFit& operator=(TDecayFit&&) noexcept = default;
68 ~TDecayFit() = default;
69
70 void SetDecay(TVirtualDecay* decay);
71 TVirtualDecay* GetDecay() const;
72 void DrawComponents() const; // *MENU*
73
74 void Print(Option_t* opt = "") const override;
75 void UpdateResiduals(TH1* hist);
76 void DrawResiduals(); // *MENU*
77 TGraph* GetResiduals() { return &fResiduals; }
78 TFitResultPtr Fit(TH1* hist, Option_t* opt = "");
79
80private:
81 void DefaultGraphs();
82
83 TVirtualDecay* fDecay{nullptr}; // VirtualDecay that made this fit
84 TGraph fResiduals; // Last histogram fit by this function
85
86 /// \cond CLASSIMP
87 ClassDefOverride(TDecayFit, 1); // NOLINT(readability-else-after-return)
88 /// \endcond
89};
90
91class TVirtualDecay : public TNamed {
92public:
93 TVirtualDecay() = default;
94 TVirtualDecay(const TVirtualDecay&) = default;
95 TVirtualDecay(TVirtualDecay&&) noexcept = default;
96 TVirtualDecay& operator=(const TVirtualDecay&) = default;
97 TVirtualDecay& operator=(TVirtualDecay&&) noexcept = default;
98 ~TVirtualDecay() = default;
99
100 virtual void DrawComponents(Option_t* opt = "", Bool_t color_flag = true);
101 void Print(Option_t* opt = "") const override = 0;
102
103private:
104 virtual const TDecayFit* GetFitFunction() = 0;
105
106 /// \cond CLASSIMP
107 ClassDefOverride(TVirtualDecay, 1) // NOLINT(readability-else-after-return)
108 /// \endcond
109};
110
112 friend class TDecayChain;
113 friend class TDecayFit;
114 friend class TDecay;
115 // friend class TDecay;
116
117public:
119 : fDetectionEfficiency(1.0)
120 {
121 }
122 TSingleDecay(UInt_t generation, TSingleDecay* parent, Double_t tlow = 0, Double_t thigh = 10);
123 explicit TSingleDecay(TSingleDecay* parent, Double_t tlow = 0, Double_t thigh = 10);
124 TSingleDecay(const TSingleDecay&) = default;
125 TSingleDecay(TSingleDecay&&) noexcept = default;
126 TSingleDecay& operator=(const TSingleDecay&) = default;
127 TSingleDecay& operator=(TSingleDecay&&) noexcept = default;
128 ~TSingleDecay();
129
130 ///// TF1 Helpers ////
131 UInt_t GetGeneration() const { return fGeneration; }
132 Double_t GetHalfLife() const
133 {
134 return (fDecayFunc->GetParameter(1) > 0.) ? std::log(2.0) / fDecayFunc->GetParameter(1) : 0.0;
135 }
136 Double_t GetDecayRate() const { return fDecayFunc->GetParameter(1); }
137 Double_t GetIntensity() const { return fDecayFunc->GetParameter(0); }
138 Double_t GetEfficiency() const { return fDetectionEfficiency; }
139 Double_t GetHalfLifeError() const
140 {
141 return (GetDecayRate() > 0.) ? GetHalfLife() * GetDecayRateError() / GetDecayRate() : 0.0;
142 }
143 Double_t GetDecayRateError() const { return fDecayFunc->GetParError(1); }
144 Double_t GetIntensityError() const { return fDecayFunc->GetParError(0); }
145 void SetHalfLife(const Double_t& halflife)
146 {
147 fDecayFunc->SetParameter(1, std::log(2.0) / halflife);
148 UpdateDecays();
149 }
150 void SetDecayRate(const Double_t& decayrate)
151 {
152 fDecayFunc->SetParameter(1, decayrate);
153 UpdateDecays();
154 }
155 void SetIntensity(const Double_t& intens)
156 {
157 fDecayFunc->SetParameter(0, intens);
158 UpdateDecays();
159 }
160 void SetEfficiency(const Double_t& eff) { fDetectionEfficiency = eff; }
161 void FixHalfLife(const Double_t& halflife)
162 {
163 fDecayFunc->FixParameter(1, std::log(2) / halflife);
164 UpdateDecays();
165 }
167 {
168 fDecayFunc->FixParameter(1, GetHalfLife());
169 UpdateDecays();
170 }
171 void FixDecayRate(const Double_t& decayrate)
172 {
173 fDecayFunc->FixParameter(1, decayrate);
174 UpdateDecays();
175 }
177 {
178 fDecayFunc->FixParameter(0, GetDecayRate());
179 UpdateDecays();
180 }
181 void FixIntensity(const Double_t& intensity) { fDecayFunc->FixParameter(0, intensity); }
182 void FixIntensity() { fDecayFunc->FixParameter(0, GetIntensity()); }
183 void SetHalfLifeLimits(const Double_t& low, const Double_t& high);
184 void SetIntensityLimits(const Double_t& low, const Double_t& high);
185 void SetDecayRateLimits(const Double_t& low, const Double_t& high);
186 void GetHalfLifeLimits(Double_t& low, Double_t& high) const;
187 void GetIntensityLimits(Double_t& low, Double_t& high) const;
188 void GetDecayRateLimits(Double_t& low, Double_t& high) const;
189 void ReleaseHalfLife() { fDecayFunc->ReleaseParameter(1); }
190 void ReleaseDecayRate() { fDecayFunc->ReleaseParameter(1); }
191 void ReleaseIntensity() { fDecayFunc->ReleaseParameter(0); }
192 void Draw(Option_t* option = "") override;
193 Double_t Eval(Double_t t);
194 Double_t EvalPar(const Double_t* x, const Double_t* par = nullptr);
195 TFitResultPtr Fit(TH1* fithist, Option_t* opt = "");
196 void Fix();
197 void Release();
198 void SetRange(Double_t tlow, Double_t thigh);
199 void SetName(const char* name) override;
200 void SetLineColor(Color_t color) { fTotalDecayFunc->SetLineColor(color); }
201 Color_t GetLineColor() const { return fTotalDecayFunc->GetLineColor(); }
202 void SetMinimum(Double_t min)
203 {
204 fTotalDecayFunc->SetMinimum(min);
205 fDecayFunc->SetMinimum(min);
206 }
207 void SetMaximum(Double_t max)
208 {
209 fTotalDecayFunc->SetMaximum(max);
210 fDecayFunc->SetMaximum(max);
211 }
212
213 void SetDaughterDecay(TSingleDecay* daughter) { fDaughter = daughter; }
214 void SetParentDecay(TSingleDecay* parent) { fParent = parent; }
215 void SetTotalDecayParameters();
216 void SetDecayId(Int_t Id) { fUnId = Id; }
217 Int_t GetDecayId() const { return fUnId; }
218 Int_t GetChainId() const { return fChainId; }
219
220 const TDecayFit* GetDecayFunc() const { return fDecayFunc; }
222 {
223 SetTotalDecayParameters();
224 return fTotalDecayFunc;
225 }
226
227 TSingleDecay* GetParentDecay();
228 TSingleDecay* GetDaughterDecay();
229
230 Double_t ActivityFunc(Double_t* dim, Double_t* par);
231
232 void Print(Option_t* option = "") const override;
233
234private:
235 void SetDecayRateError(Double_t err) { fDecayFunc->SetParError(1, err); }
236 void SetIntensityError(Double_t err) { fDecayFunc->SetParError(0, err); }
237
238 void UpdateDecays();
239
240 void SetChainId(Int_t id) { fChainId = id; }
241
242 const TDecayFit* GetFitFunction() override
243 {
244 SetTotalDecayParameters();
245 return fTotalDecayFunc;
246 }
247
248 UInt_t fGeneration{0}; // Generation from the primary
249 Double_t fDetectionEfficiency{0.}; // The probability that this decay can be detected
250 TDecayFit* fDecayFunc{nullptr}; // Function describing decay
251 TDecayFit* fTotalDecayFunc{nullptr}; // Function used to access other fits
252 TSingleDecay* fParent{nullptr}; // Parent Decay
253 TSingleDecay* fDaughter{nullptr}; // Daughter Decay
254 TSingleDecay* fFirstParent{nullptr}; // FirstParent in the decay
255 Int_t fUnId{0}; // The Unique ID of the Decay
256 static UInt_t fCounter; // Helps set unique Id's
257 Int_t fChainId{-1}; // The chain that the single decay belongs to
258
259 /// \cond CLASSIMP
260 ClassDefOverride(TSingleDecay, 1) // NOLINT(readability-else-after-return)
261 /// \endcond
262};
263
265public:
266 TDecayChain() = default;
267 explicit TDecayChain(UInt_t generations);
268 TDecayChain(const TDecayChain&) = default;
269 TDecayChain(TDecayChain&&) noexcept = default;
270 TDecayChain& operator=(const TDecayChain&) = default;
271 TDecayChain& operator=(TDecayChain&&) noexcept = default;
272 ~TDecayChain();
273
274 TSingleDecay* GetDecay(UInt_t generation);
275 Double_t Eval(Double_t t) const;
276 void Draw(Option_t* opt = "") override;
277 Int_t Size() const { return static_cast<Int_t>(fDecayChain.size()); }
278
279 void Print(Option_t* option = "") const override;
280
281 void SetChainParameters();
282 void SetRange(Double_t xlow, Double_t xhigh);
284 {
285 SetChainParameters();
286 return fChainFunc;
287 }
288 void DrawComponents(Option_t* opt = "", Bool_t color_flag = true) override;
289 TFitResultPtr Fit(TH1* fithist, Option_t* opt = "");
290 Double_t EvalPar(const Double_t* x, const Double_t* par = nullptr);
291
292 Int_t GetChainId() const { return fChainId; }
293
294private:
295 void AddToChain(TSingleDecay* decay);
296 Double_t ChainActivityFunc(Double_t* dim, Double_t* par);
297 static UInt_t fChainCounter;
298 const TDecayFit* GetFitFunction() override
299 {
300 SetChainParameters();
301 return fChainFunc;
302 }
303
304 std::vector<TSingleDecay*> fDecayChain; // The Decays in the Decay Chain
305 TDecayFit* fChainFunc{nullptr}; // Function describing the total chain activity
306 Int_t fChainId{-1};
307
308 /// \cond CLASSIMP
309 ClassDefOverride(TDecayChain, 1) // NOLINT(readability-else-after-return)
310 /// \endcond
311};
312
313////////////////////////////////////////////////////////////////////////
314///
315/// \class TDecay
316///
317/// TDecay is a class for fitting halflives during nuclear decay
318/// A TDecay consists of multiple TDecayChains, where a TDecayChain
319/// is starts at a specific nucleus which has a population before the
320/// decay fit takes place. This could be a nucleus with a daughter.
321/// One TDecayChain would consist of just the daughter while the
322/// the other decay chain would be the parent and daughter.
323/// TDecayChains are made up of multiple TSingleDecays which holds
324/// the nucleus specific information such as name, id, halflife and
325/// intensity. When any of the above classes are fit to a histogram,
326/// they use a TDecayFit. The TDecayFit is a a TF1 with extra information
327/// such as the class that was used to create the TDecayFit. Furthermore,
328/// the function DrawComponents() can be used to draw the activites of the
329/// individual nuclei involved in the TDecayFit.
330///
331////////////////////////////////////////////////////////////////////////
332
333class TDecay : public TVirtualDecay {
334public:
335 TDecay() = default;
336 explicit TDecay(std::vector<TDecayChain*> chainList);
337 TDecay(const TDecay&) = default;
338 TDecay(TDecay&&) noexcept = default;
339 TDecay& operator=(const TDecay&) = default;
340 TDecay& operator=(TDecay&&) noexcept = default;
341 ~TDecay() = default;
342
343 void AddChain(TDecayChain* chain) { fChainList.push_back(chain); }
344 Double_t DecayFit(Double_t* dim, Double_t* par);
345 TDecayChain* GetChain(UInt_t idx);
346
347 void SetHalfLife(Int_t Id, Double_t halflife);
348 void SetHalfLifeLimits(Int_t Id, Double_t low, Double_t high);
349 void SetDecayRateLimits(Int_t Id, Double_t low, Double_t high);
350 void FixHalfLife(Int_t Id, Double_t halflife)
351 {
352 SetHalfLife(Id, halflife);
353 SetHalfLifeLimits(Id, halflife, halflife);
354 }
355 TFitResultPtr Fit(TH1* fithist, Option_t* opt = "");
356
357 void Print(Option_t* opt = "") const override;
358 void PrintMap() const;
359 TDecayFit* GetFitFunc() { return fFitFunc; }
360 void SetBackground(Double_t background) { fFitFunc->SetParameter(0, background); }
361 Double_t GetBackground() const { return fFitFunc->GetParameter(0); }
362 Double_t GetBackgroundError() const { return fFitFunc->GetParError(0); }
363 void SetRange(Double_t xlow, Double_t xhigh);
364 void DrawComponents(Option_t* opt = "", Bool_t color_flag = true) override;
365 void Draw(Option_t* opt = "") override;
366 void DrawBackground(Option_t* opt = "");
367 void FixBackground(const Double_t& background) { fFitFunc->FixParameter(0, background); }
368 void FixBackground() { fFitFunc->FixParameter(0, GetBackground()); }
369 void SetBackgroundLimits(const Double_t& low, const Double_t& high) { fFitFunc->SetParLimits(0, low, high); }
370 void ReleaseBackground() { fFitFunc->ReleaseParameter(0); }
371
372 TGraph* GetResiduals() { return fFitFunc->GetResiduals(); }
373
374private:
375 void RemakeMap();
376 void SetParameters();
377 Double_t ComponentFunc(Double_t* dim, Double_t* par);
378 const TDecayFit* GetFitFunction() override { return fFitFunc; }
379
380 std::vector<TDecayChain*> fChainList;
381 TDecayFit* fFitFunc{nullptr};
382 std::map<Int_t, std::vector<TSingleDecay*>> fDecayMap; //
383
384 /// \cond CLASSIMP
385 ClassDefOverride(TDecay, 1) // NOLINT(readability-else-after-return)
386 /// \endcond
387};
388/*! @} */
389#endif
TH1D * hist
Definition UserFillObj.h:3
std::vector< TSingleDecay * > fDecayChain
Definition TDecay.h:304
TDecayChain(const TDecayChain &)=default
static UInt_t fChainCounter
Definition TDecay.h:297
TDecayChain()=default
const TDecayFit * GetFitFunction() override
Definition TDecay.h:298
TDecayChain(TDecayChain &&) noexcept=default
Int_t GetChainId() const
Definition TDecay.h:292
const TDecayFit * GetChainFunc()
Definition TDecay.h:283
TDecayFit(const char *name, Double_t(*fcn)(Double_t *, Double_t *), Double_t xmin=0, Double_t xmax=1, Int_t npar=0)
Definition TDecay.h:39
TDecayFit()=default
TVirtualDecay * fDecay
Definition TDecay.h:83
void DefaultGraphs()
Definition TDecay.cxx:54
void Print(Option_t *opt="") const override
Definition TDecay.cxx:27
TDecayFit(const char *name, Double_t xmin, Double_t xmax, Int_t npar)
Definition TDecay.h:29
TFitResultPtr Fit(TH1 *hist, Option_t *opt="")
Definition TDecay.cxx:39
TDecayFit(TDecayFit &&) noexcept=default
TDecayFit(const char *name, const PtrObj &p, MemFn memFn, Double_t xmin, Double_t xmax, Int_t npar)
Definition TDecay.h:52
TGraph * GetResiduals()
Definition TDecay.h:77
TGraph fResiduals
Definition TDecay.h:84
void SetDecay(TVirtualDecay *decay)
Definition TDecay.cxx:21
TDecayFit(const char *name, const char *formula, Double_t xmin=0, Double_t xmax=1)
Definition TDecay.h:24
void DrawComponents() const
Definition TDecay.cxx:15
TDecayFit(const char *name, Double_t(*fcn)(const Double_t *, const Double_t *), Double_t xmin=0, Double_t xmax=1, Int_t npar=0)
Definition TDecay.h:44
void UpdateResiduals(TH1 *hist)
Definition TDecay.cxx:61
TDecayFit(const char *name, const ROOT::Math::ParamFunctor &f, Double_t xmin=0, Double_t xmax=1, Int_t npar=0)
Definition TDecay.h:33
void DrawResiduals()
Definition TDecay.cxx:93
TDecayFit(const char *name, Func f, Double_t xmin, Double_t xmax, Int_t npar, const char *className=nullptr)
Definition TDecay.h:59
TDecayFit(const TDecayFit &)=default
TVirtualDecay * GetDecay() const
Definition TDecay.cxx:34
TDecay(TDecay &&) noexcept=default
Double_t GetBackgroundError() const
Definition TDecay.h:362
std::map< Int_t, std::vector< TSingleDecay * > > fDecayMap
Definition TDecay.h:382
std::vector< TDecayChain * > fChainList
Definition TDecay.h:380
void FixBackground()
Definition TDecay.h:368
void SetBackgroundLimits(const Double_t &low, const Double_t &high)
Definition TDecay.h:369
TGraph * GetResiduals()
Definition TDecay.h:372
Double_t GetBackground() const
Definition TDecay.h:361
const TDecayFit * GetFitFunction() override
Definition TDecay.h:378
TDecayFit * GetFitFunc()
Definition TDecay.h:359
TDecay()=default
TDecay(const TDecay &)=default
void FixHalfLife(Int_t Id, Double_t halflife)
Definition TDecay.h:350
void FixBackground(const Double_t &background)
Definition TDecay.h:367
void ReleaseBackground()
Definition TDecay.h:370
void SetBackground(Double_t background)
Definition TDecay.h:360
void SetDecayRate(const Double_t &decayrate)
Definition TDecay.h:150
Int_t GetChainId() const
Definition TDecay.h:218
void FixHalfLife(const Double_t &halflife)
Definition TDecay.h:161
void SetDaughterDecay(TSingleDecay *daughter)
Definition TDecay.h:213
Color_t GetLineColor() const
Definition TDecay.h:201
Int_t GetDecayId() const
Definition TDecay.h:217
static UInt_t fCounter
Definition TDecay.h:256
Double_t GetHalfLife() const
Definition TDecay.h:132
void ReleaseDecayRate()
Definition TDecay.h:190
Double_t GetDecayRateError() const
Definition TDecay.h:143
Double_t GetIntensityError() const
Definition TDecay.h:144
void SetMaximum(Double_t max)
Definition TDecay.h:207
void FixDecayRate(const Double_t &decayrate)
Definition TDecay.h:171
void SetChainId(Int_t id)
Definition TDecay.h:240
void FixIntensity()
Definition TDecay.h:182
Double_t GetEfficiency() const
Definition TDecay.h:138
void FixHalfLife()
Definition TDecay.h:166
const TDecayFit * GetDecayFunc() const
Definition TDecay.h:220
void SetDecayRateError(Double_t err)
Definition TDecay.h:235
void SetMinimum(Double_t min)
Definition TDecay.h:202
void SetIntensity(const Double_t &intens)
Definition TDecay.h:155
void SetLineColor(Color_t color)
Definition TDecay.h:200
const TDecayFit * GetFitFunction() override
Definition TDecay.h:242
void FixIntensity(const Double_t &intensity)
Definition TDecay.h:181
Double_t GetDecayRate() const
Definition TDecay.h:136
void SetDecayId(Int_t Id)
Definition TDecay.h:216
void ReleaseIntensity()
Definition TDecay.h:191
void SetHalfLife(const Double_t &halflife)
Definition TDecay.h:145
Double_t GetHalfLifeError() const
Definition TDecay.h:139
void SetParentDecay(TSingleDecay *parent)
Definition TDecay.h:214
Double_t GetIntensity() const
Definition TDecay.h:137
void FixDecayRate()
Definition TDecay.h:176
void SetIntensityError(Double_t err)
Definition TDecay.h:236
void ReleaseHalfLife()
Definition TDecay.h:189
const TDecayFit * GetTotalDecayFunc()
Definition TDecay.h:221
TSingleDecay(TSingleDecay &&) noexcept=default
void SetEfficiency(const Double_t &eff)
Definition TDecay.h:160
TSingleDecay(const TSingleDecay &)=default
virtual const TDecayFit * GetFitFunction()=0
void Print(Option_t *opt="") const override=0
virtual void DrawComponents(Option_t *opt="", Bool_t color_flag=true)
Definition TDecay.cxx:103
TVirtualDecay(TVirtualDecay &&) noexcept=default
TVirtualDecay()=default
TVirtualDecay(const TVirtualDecay &)=default