GSI Object Oriented Online Offline (Go4)  GO4-6.3.0
Example6.cxx
Go to the documentation of this file.
1 // $Id$
2 //-----------------------------------------------------------------------
3 // The GSI Online Offline Object Oriented (Go4) Project
4 // Experiment Data Processing at EE department, GSI
5 //-----------------------------------------------------------------------
6 // Copyright (C) 2000- GSI Helmholtzzentrum fuer Schwerionenforschung GmbH
7 // Planckstr. 1, 64291 Darmstadt, Germany
8 // Contact: http://go4.gsi.de
9 //-----------------------------------------------------------------------
10 // This software can be used under the license agreements as stated
11 // in Go4License.txt file which is part of the distribution.
12 //-----------------------------------------------------------------------
13 
14 /* Go4Fit Example N6
15  Simultanious fit of two histogram
16 */
17 
18 #ifndef __CINT__
19 
20 #include "TH1.h"
21 #include "TFile.h"
22 #include "TApplication.h"
23 #include "TCollection.h"
24 
25 #include "TGo4Fitter.h"
26 #include "TGo4FitDataHistogram.h"
27 #include "TGo4FitModelPolynom.h"
28 #include "TGo4FitModelGauss1.h"
29 #include "TGo4FitAxisTrans.h"
30 #include "TGo4FitLinearTrans.h"
31 #include "TGo4FitterConfig.h"
32 
33 void Example6();
34 
35 int main(int argc, char **argv)
36 {
37  TApplication theApp("Application", nullptr, nullptr);
38 
39  Example6();
40 
41  theApp.Run();
42 
43  return 0;
44 }
45 
46 #endif
47 
48 // routine to read histogram from examples file
49 TH1D *GetHistogram(const char *HistogramName)
50 {
51  TFile *f = TFile::Open("histograms.root");
52  if (!f) return nullptr;
53  TH1D *histo = nullptr;
54  f->GetObject(HistogramName, histo);
55  if (histo) histo->SetDirectory(nullptr);
56  delete f;
57  return histo;
58 }
59 
60 // construct transformation object, which recalculate bin numbers to new scale values
61 // here simple linear transformation is used
63 {
64  TGo4FitLinearTrans* trans = new TGo4FitLinearTrans("trans","linear axis transformation");
65  trans->SetCoefByRange(3800,0.,3.8);
66  return trans;
67 }
68 
70 {
71 // create fitter and select function to fit
72  TGo4Fitter *fitter = new TGo4Fitter("Fitter",TGo4Fitter::ff_ML_Poisson, kFALSE);
73 
74 // create object to fit for first histogram, but specify histogram later
75  TGo4FitDataHistogram *data1 = new TGo4FitDataHistogram("data1", nullptr);
76  data1->SetUseBinScale(kTRUE);
77  data1->SetRange(0,2.2,2.9);
78  fitter->AddData(data1);
79 
80 // create four components for model of the first histogram
81  fitter->AddModel( "data1", new TGo4FitModelPolynom("Pol1_0",0.) );
82  fitter->AddModel( "data1", new TGo4FitModelPolynom("Pol1_1",1.) );
83  fitter->AddModel( "data1", new TGo4FitModelGauss1("Gauss1_1",2.553,0.015) );
84  fitter->AddModel( "data1", new TGo4FitModelGauss1("Gauss2_1",2.672,0.015) );
85 
86 // create object to fit for second histogram, but specify histogram later
87  TGo4FitDataHistogram *data2 = new TGo4FitDataHistogram("data2", nullptr);
88  data2->SetUseBinScale(kTRUE);
89  data2->SetRange(0,2.2,2.9);
90  fitter->AddData(data2);
91 
92 // create six components for model of the first histogram
93  fitter->AddModel( "data2", new TGo4FitModelPolynom("Pol2_0",0.) );
94  fitter->AddModel( "data2", new TGo4FitModelPolynom("Pol2_1",1.) );
95  fitter->AddModel( "data2", new TGo4FitModelGauss1("Gauss1_2",2.553,0.015) );
96  fitter->AddModel( "data2", new TGo4FitModelGauss1("Gauss2_2",2.672,0.015) );
97  fitter->AddModel( "data2", new TGo4FitModelGauss1("Gauss3_2",2.597,0.014) );
98  fitter->AddModel( "data2", new TGo4FitModelGauss1("Gauss4_2",2.717,0.014) );
99 
100 // specify init value and dependency between parameters
101  TGo4FitterConfig* config = new TGo4FitterConfig("Config","fitter configuration");
102  config->SetParInit("Pol1_0.Ampl",40);
103  config->SetParInit("Pol1_1.Ampl",-0.01);
104  config->SetParInit("Pol2_0.Ampl",500);
105  config->SetParInit("Pol2_1.Ampl",-0.1);
106  config->SetParDepend("Gauss1_2.Pos","Gauss1_1.Pos");
107  config->SetParDepend("Gauss1_2.Width","Gauss1_1.Width");
108  config->SetParDepend("Gauss2_2.Pos","Gauss2_1.Pos");
109  config->SetParDepend("Gauss2_2.Width","Gauss2_1.Width");
110  fitter->AddAction(config);
111 
112 // add standard actions to fitter
113  fitter->AddStandardActions();
114 
115 // set usage of buffers only for data objects, not for models
116 // this highly increase speed of evaluations
117  fitter->SetMemoryUsage(1);
118 
119  return fitter;
120 }
121 
122 // store fitter with all supplied objects
123 void StoreFitter(TGo4Fitter *fitter)
124 {
125  TFile *f = TFile::Open("Example6.root","recreate");
126  if (f) fitter->Write("Fitter");
127  delete f;
128 }
129 
130 // read fitter from file
132 {
133  TFile *f = TFile::Open("Example6.root");
134  if (!f) return nullptr;
135  TGo4Fitter *fitter = nullptr;
136  f->GetObject("Fitter", fitter);
137  delete f;
138  return fitter;
139 }
140 
141 void Example6()
142 {
143 // create fitter
144  TGo4Fitter *fitter = BuildFitter();
145 
146 // construct axis transformation object and set it for both data object, first will be owner
147  auto trans = ConstructTrans();
148  fitter->FindData("data1")->AddAxisTrans(trans, kTRUE);
149  fitter->FindData("data2")->AddAxisTrans(trans, kFALSE);
150 
151 // assign histograms to fitter with ownership flag
152  fitter->SetObject("data1", GetHistogram("hDeg120_P_c"), kTRUE);
153  fitter->SetObject("data2", GetHistogram("hDeg120_CND"), kTRUE);
154 
155 // do fit
156  fitter->DoActions();
157 
158 // store fitter to file and destroy it
159  StoreFitter(fitter);
160  delete fitter;
161 
162 
163 // restore fitter from file
164 // logically this is independent part and can be placed anywhere, just in another program
165  fitter = RestoreFitter();
166 
167 // show results of fitting
168  fitter->PrintPars();
169  fitter->Draw("#data1,Gauss1_1,Gauss2_1");
170  fitter->Draw("#data2,Gauss1_2,Gauss2_2,Gauss3_2,Gauss4_2");
171 
172  delete fitter;
173 }
void PrintPars() const
TGo4FitSlot * SetObject(TObject *obj, Bool_t iOwned=kFALSE)
void SetMemoryUsage(Int_t iMemoryUsage)
Definition: TGo4Fitter.cxx:70
int main(int argc, char **argv)
Definition: Example6.cxx:35
TH1D * GetHistogram(const char *HistogramName)
Definition: Example6.cxx:49
void AddAction(TGo4FitterAction *Action)
void SetParDepend(const char *FullName, const char *iExpression)
TGo4FitData * AddData(TGo4FitData *d)
Definition: TGo4Fitter.cxx:119
void SetCoefByRange(Int_t nbins, Double_t y1, Double_t y2)
void SetRange(Int_t naxis, Double_t min, Double_t max)
void SetParInit(const char *FullName, Double_t iValue)
void Example6()
Definition: Example6.cxx:141
TGo4FitAxisTrans * ConstructTrans()
Definition: Example6.cxx:62
TGo4Fitter * BuildFitter()
Definition: Example6.cxx:69
TGo4FitModel * AddModel(TGo4FitModel *m)
Definition: TGo4Fitter.cxx:210
void SetUseBinScale(Bool_t iUseBinScale)
Definition: TGo4FitData.h:73
void AddAxisTrans(TGo4FitAxisTrans *Trans, Bool_t TransOwned=kFALSE)
Definition: TGo4FitData.cxx:96
void StoreFitter(TGo4Fitter *fitter)
Definition: Example6.cxx:123
TGo4FitData * FindData(const char *DataName)
Definition: TGo4Fitter.cxx:114
void DoActions(Bool_t AllowFitterChange=kFALSE, TObjArray *Actions=nullptr)
void Draw(Option_t *option) override
TGo4Fitter * RestoreFitter()
Definition: Example6.cxx:131
void AddStandardActions()
Definition: TGo4Fitter.cxx:731