TkN 2.1
Toolkit for Nuclei
benford.C
1#include "Rtypes.h"
2#include "TH1F.h"
3#include "TF1.h"
4
5#include "tkmanager.h"
6#include "tkstring.h"
7#include "tkunit.h"
8#include "TStyle.h"
9#include "TMath.h"
10#include "TROOT.h"
11#include "TCanvas.h"
12
13using namespace tkn;
14
15int first_digit(double value);
16TH1* benford_level(std::function<double(shared_ptr<tklevel>)>const& eval, tkstring name="");
17
18void benford()
19{
20 gROOT->SetStyle("tkn-histo");
21
22 TF1 benford_law("Benford law","100*TMath::Log10(1+1/x)",0.5,9.5);
23 benford_law.SetTitle("P(n) = log_{10}(1+1/n) ");
24 benford_law.SetMarkerStyle(0);
25 benford_law.SetMarkerColor(benford_law.GetLineColor());
26 benford_law.SetLineStyle(9);
27
28 auto *cc = new TCanvas("benford","Benford's law on lifetimes",1800,500);
29 cc->Divide(3,1);
30
31 auto histo = benford_level([](auto lvl) {return lvl->get_lifetime(tkunit_manager::units_keys::s);},
32 "lifetime (in s)");
33
34 cc->cd(1);
35 histo->Draw("");
36 benford_law.DrawClone("same");
37 cc->cd(1)->BuildLegend(0.332776,0.779412,0.934783,0.936975);
38
39 histo = benford_level([](auto lvl) {return lvl->get_lifetime(tkunit_manager::units_keys::keV);},
40 "lifetime (in keV)");
41
42 cc->cd(2);
43 histo->Draw("");
44 benford_law.DrawClone("same");
45 cc->cd(2)->BuildLegend(0.332776,0.779412,0.934783,0.936975);
46
47 histo = benford_level([](auto lvl) {if(!lvl->get_lifetime_measure()) return 0.; return lvl->get_lifetime_measure()->get_error(tkunit_manager::units_keys::keV);},
48 "lifetime error (in keV)");
49
50 cc->cd(3);
51 histo->Draw("");
52 benford_law.DrawClone("same");
53 cc->cd(3)->BuildLegend(0.332776,0.779412,0.934783,0.936975);
54 cc->SaveAs("benford.png");
55}
56
57TH1* benford_level(std::function<double(shared_ptr<tklevel>)>const& eval, tkstring name)
58{
59 glog.set_warnings(false);
60
61 auto histo = new TH1F("digit",name.data(),9,.5,9.5);
62 histo->SetName(name.remove_all(" ").data());
63 histo->GetXaxis()->SetTitle("first digit (n)");
64 histo->GetXaxis()->SetNdivisions(10);
65 histo->GetYaxis()->SetTitle("P(n) (in %)");
66
67 for(const auto &nuc : gmanager->get_nuclei()) {
68 auto levelscheme = nuc->get_level_scheme();
69 for(const auto &lvl : nuc->get_level_scheme()->get_levels()) {
70 int fd = first_digit(eval(lvl));
71 if(fd>0) histo->Fill(fd);
72 }
73 }
74 histo->Sumw2();
75 histo->Scale(100./histo->Integral());
76
77 return histo;
78}
79
80int first_digit(double value)
81{
82 if(value<=0.) return 0;
83 tkstring tmp = tkstring::form("%e",value);
84 return tmp.substr(0,1).atoi();
85}
86
std::string with usefull tricks from TString (ROOT) and KVString (KaliVeda) and more....
Definition: tkstring.h:54
tkstring & remove_all(const tkstring &_s1)
Definition: tkstring.h:215
tkstring substr(size_type __pos=0, size_type __n=npos) const
Inlines.
Definition: tkstring.h:179
int atoi() const
Converts a string to integer value.
Definition: tkstring.cpp:175
Definition: tklog.cpp:39