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