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