BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
TH1Set.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2023.
4
5This file is part of BDSIM.
6
7BDSIM is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published
9by the Free Software Foundation version 3 of the License.
10
11BDSIM is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
18*/
19#include "HistSparse1D.hh"
20#include "TH1Set.hh"
21
22#include "TH1.h"
23#include "TMultiGraph.h"
24#include "TClass.h"
25#include "TList.h"
26#include "TMath.h"
27
28ClassImp(TH1Set)
29
30TH1Set::TH1Set()
31{;}
32
33TH1Set::TH1Set(const char* name,
34 const char* title):
35 TH1D(name, title, 1, 0, 1)
36{;}
37
38TH1Set::~TH1Set()
39{;}
40
41Int_t TH1Set::Fill(Double_t x, Double_t w)
42{
43 fEntries++;
44
45 Int_t bin = 0;
46 if (data.HasAbscissa(x))
47 {bin = abscissaToBinIndex[x];}
48 else
49 {bin = AddNewBin(x);}
50
51 data.Fill(x,w); // fill our data structure
52
53 // fill theirs
54 if (!fSumw2.fN && w != 1.0 && !TestBit(TH1::kIsNotW) ) Sumw2(); // must be called before AddBinContent
55 if (fSumw2.fN) fSumw2.fArray[bin] += w*w;
56 AddBinContent(bin, w);
57 if (bin == 0 || bin > fXaxis.GetNbins())
58 {
59 //if (!TH1::GetStatOverflowsBehaviour())
60 // {return -1;}
61 }
62 Double_t z= w;
63 fTsumw += z;
64 fTsumw2 += z*z;
65 fTsumwx += z*x;
66 fTsumwx2 += z*x*x;
67 return bin;
68}
69
70void TH1Set::DoFillN(Int_t ntimes, const Double_t *x, const Double_t *w, Int_t stride)
71{
72 ntimes *= stride;
73 for (Int_t i=0;i<ntimes;i+=stride)
74 {
75 Fill(x[i], w[i]);
76 }
77}
78
79Int_t TH1Set::AddNewBin(long long int x)
80{
81 Int_t nBinsNew = fXaxis.GetNbins()+1;
82 fXaxis.Set(nBinsNew, fXaxis.GetXmin(), fXaxis.GetXmax()+1);
83 abscissaToBinIndex[x] = nBinsNew;
84 fNcells += 1;
85 return nBinsNew;
86}
87
88Bool_t TH1Set::Add(const TH1 *h1, Double_t c1)
89{
90 Bool_t result = TH1D::Add(h1, c1);
91
92 auto casted = dynamic_cast<const TH1Set*>(h1);
93 if (!casted)
94 {return kFALSE;}
95 else
96 {
97 auto& d = casted->data;
98 for (const auto& bin : d)
99 {data[bin.first] += bin.second;}
100 }
101
102 return result;
103}
104
105Double_t TH1Set::GetBinContentByAbscissa(long long int x) const
106{
107 return data.HasAbscissa(x) ? GetBinContent(abscissaToBinIndex.at(x)) : 0;
108}
109
110Double_t TH1Set::GetBinErrorByAbscissa(long long int x) const
111{
112 return data.HasAbscissa(x) ? GetBinError(abscissaToBinIndex.at(x)) : 0;
113}
114
115void TH1Set::SetBinContentByAbscissa(long long int x, Double_t newValue)
116{
117 if (data.HasAbscissa(x))
118 {SetBinContent(abscissaToBinIndex.at(x), newValue);}
119 else
120 {SetBinContent(AddNewBin(x), newValue);}
121}
122
123void TH1Set::SetBinErrorByAbscissa(long long int x, Double_t newError)
124{
125 if (data.HasAbscissa(x))
126 {SetBinError(abscissaToBinIndex.at(x), newError);}
127 else
128 {SetBinError(AddNewBin(x), newError);}
129}
TH1D but with a category axis.
Definition: TH1Set.hh:39