GSI Object Oriented Online Offline (Go4) GO4-6.4.0
Loading...
Searching...
No Matches
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
20
21#ifndef __CINT__
22
23#include "TH1.h"
24#include "TFile.h"
25#include "TApplication.h"
26#include "TCollection.h"
27
28#include "TGo4Fitter.h"
30#include "TGo4FitModelPolynom.h"
31#include "TGo4FitModelGauss1.h"
32#include "TGo4FitAxisTrans.h"
33#include "TGo4FitLinearTrans.h"
34#include "TGo4FitterConfig.h"
35
36void Example6();
37
38int main(int argc, char **argv)
39{
40 TApplication theApp("Application", nullptr, nullptr);
41
42 Example6();
43
44 theApp.Run();
45
46 return 0;
47}
48
49#endif
50
51// routine to read histogram from examples file
52TH1D *GetHistogram(const char *HistogramName)
53{
54 TFile *f = TFile::Open("histograms.root");
55 if (!f) return nullptr;
56 TH1D *histo = nullptr;
57 f->GetObject(HistogramName, histo);
58 if (histo) histo->SetDirectory(nullptr);
59 delete f;
60 return histo;
61}
62
63// construct transformation object, which recalculate bin numbers to new scale values
64// here simple linear transformation is used
66{
67 TGo4FitLinearTrans* trans = new TGo4FitLinearTrans("trans","linear axis transformation");
68 trans->SetCoefByRange(3800,0.,3.8);
69 return trans;
70}
71
73{
74// create fitter and select function to fit
75 TGo4Fitter *fitter = new TGo4Fitter("Fitter",TGo4Fitter::ff_ML_Poisson, kFALSE);
76
77// create object to fit for first histogram, but specify histogram later
78 TGo4FitDataHistogram *data1 = new TGo4FitDataHistogram("data1", nullptr);
79 data1->SetUseBinScale(kTRUE);
80 data1->SetRange(0,2.2,2.9);
81 fitter->AddData(data1);
82
83// create four components for model of the first histogram
84 fitter->AddModel( "data1", new TGo4FitModelPolynom("Pol1_0",0.) );
85 fitter->AddModel( "data1", new TGo4FitModelPolynom("Pol1_1",1.) );
86 fitter->AddModel( "data1", new TGo4FitModelGauss1("Gauss1_1",2.553,0.015) );
87 fitter->AddModel( "data1", new TGo4FitModelGauss1("Gauss2_1",2.672,0.015) );
88
89// create object to fit for second histogram, but specify histogram later
90 TGo4FitDataHistogram *data2 = new TGo4FitDataHistogram("data2", nullptr);
91 data2->SetUseBinScale(kTRUE);
92 data2->SetRange(0,2.2,2.9);
93 fitter->AddData(data2);
94
95// create six components for model of the first histogram
96 fitter->AddModel( "data2", new TGo4FitModelPolynom("Pol2_0",0.) );
97 fitter->AddModel( "data2", new TGo4FitModelPolynom("Pol2_1",1.) );
98 fitter->AddModel( "data2", new TGo4FitModelGauss1("Gauss1_2",2.553,0.015) );
99 fitter->AddModel( "data2", new TGo4FitModelGauss1("Gauss2_2",2.672,0.015) );
100 fitter->AddModel( "data2", new TGo4FitModelGauss1("Gauss3_2",2.597,0.014) );
101 fitter->AddModel( "data2", new TGo4FitModelGauss1("Gauss4_2",2.717,0.014) );
102
103// specify init value and dependency between parameters
104 TGo4FitterConfig* config = new TGo4FitterConfig("Config","fitter configuration");
105 config->SetParInit("Pol1_0.Ampl",40);
106 config->SetParInit("Pol1_1.Ampl",-0.01);
107 config->SetParInit("Pol2_0.Ampl",500);
108 config->SetParInit("Pol2_1.Ampl",-0.1);
109 config->SetParDepend("Gauss1_2.Pos","Gauss1_1.Pos");
110 config->SetParDepend("Gauss1_2.Width","Gauss1_1.Width");
111 config->SetParDepend("Gauss2_2.Pos","Gauss2_1.Pos");
112 config->SetParDepend("Gauss2_2.Width","Gauss2_1.Width");
113 fitter->AddAction(config);
114
115// add standard actions to fitter
116 fitter->AddStandardActions();
117
118// set usage of buffers only for data objects, not for models
119// this highly increase speed of evaluations
120 fitter->SetMemoryUsage(1);
121
122 return fitter;
123}
124
125// store fitter with all supplied objects
127{
128 TFile *f = TFile::Open("Example6.root","recreate");
129 if (f) fitter->Write("Fitter");
130 delete f;
131}
132
133// read fitter from file
135{
136 TFile *f = TFile::Open("Example6.root");
137 if (!f) return nullptr;
138 TGo4Fitter *fitter = nullptr;
139 f->GetObject("Fitter", fitter);
140 delete f;
141 return fitter;
142}
143
145{
146// create fitter
147 TGo4Fitter *fitter = BuildFitter();
148
149// construct axis transformation object and set it for both data object, first will be owner
150 auto trans = ConstructTrans();
151 fitter->FindData("data1")->AddAxisTrans(trans, kTRUE);
152 fitter->FindData("data2")->AddAxisTrans(trans, kFALSE);
153
154// assign histograms to fitter with ownership flag
155 fitter->SetObject("data1", GetHistogram("hDeg120_P_c"), kTRUE);
156 fitter->SetObject("data2", GetHistogram("hDeg120_CND"), kTRUE);
157
158// do fit
159 fitter->DoActions();
160
161// store fitter to file and destroy it
162 StoreFitter(fitter);
163 delete fitter;
164
165
166// restore fitter from file
167// logically this is independent part and can be placed anywhere, just in another program
168 fitter = RestoreFitter();
169
170// show results of fitting
171 fitter->PrintPars();
172 fitter->Draw("#data1,Gauss1_1,Gauss2_1");
173 fitter->Draw("#data2,Gauss1_2,Gauss2_2,Gauss3_2,Gauss4_2");
174
175 delete fitter;
176}
void Example6()
Definition Example6.cxx:144
int main(int argc, char **argv)
Definition Example6.cxx:38
TGo4Fitter * RestoreFitter()
Definition Example6.cxx:134
TGo4FitAxisTrans * ConstructTrans()
Definition Example6.cxx:65
TH1D * GetHistogram(const char *HistogramName)
Definition Example6.cxx:52
TGo4Fitter * BuildFitter()
Definition Example6.cxx:72
void StoreFitter(TGo4Fitter *fitter)
Definition Example6.cxx:126
Base class for axis transformation objects.
void SetRange(Int_t naxis, Double_t min, Double_t max)
Includes axis range in consideration.
Data objects, which provides access to generic TH1 ROOT histogram.
void SetUseBinScale(Bool_t iUseBinScale)
If kTRUE, binary numbers will be used as scale values.
Definition TGo4FitData.h:74
void AddAxisTrans(TGo4FitAxisTrans *Trans, Bool_t TransOwned=kFALSE)
Add transformation object to data.
The linear transformation of selected data axis.
void SetCoefByRange(Int_t nbins, Double_t y1, Double_t y2)
One dimensional gaussian peak.
Model objects, which reproduce component of polynomial function.
void PrintPars() const
Print only value and error of all parameters in list.
TGo4FitSlot * SetObject(TObject *obj, Bool_t iOwned=kFALSE)
Set object to first suitable slot with defined ownership flag.
void DoActions(Bool_t AllowFitterChange=kFALSE, TObjArray *Actions=nullptr)
Executes actions list.
void AddAction(TGo4FitterAction *Action)
Add action to action list.
Fitter configuration action.
void SetParInit(const char *FullName, Double_t iValue)
Set initial value for parameter.
void SetParDepend(const char *FullName, const char *iExpression)
Set dependency expression for parameter.
Central class of Go4Fit package.
Definition TGo4Fitter.h:38
TGo4FitData * AddData(TGo4FitData *d)
Add data object to fitter.
void SetMemoryUsage(Int_t iMemoryUsage)
Set value of memory usage.
void Draw(Option_t *option) override
Draw fitter on current canvas.
void AddStandardActions()
Add list of standard actions to fitter.
TGo4FitModel * AddModel(TGo4FitModel *m)
Add model component to fitter.
TGo4FitData * FindData(const char *DataName)
Return data object with given name.