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