GSI Object Oriented Online Offline (Go4) GO4-6.4.0
Loading...
Searching...
No Matches
Example7.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 "TCollection.h"
26#include "TApplication.h"
27
28#include "TGo4FitMinuit.h"
29#include "TGo4Fitter.h"
31#include "TGo4FitModelPolynom.h"
32#include "TGo4FitModelGauss1.h"
33#include "TGo4FitAxisTrans.h"
34#include "TGo4FitLinearTrans.h"
35#include "TGo4FitterOutput.h"
36
37void Example7();
38
39int main(int argc, char **argv)
40{
41 TApplication theApp("Application", nullptr, nullptr);
42
43 Example7();
44
45 theApp.Run();
46
47 return 0;
48}
49
50#endif
51
52// routine to read histogram from examples file
53TH1D* GetHistogram(const char *HistogramName)
54{
55 TFile *f = TFile::Open("histograms.root");
56 if (!f) return nullptr;
57 TH1D *histo = nullptr;
58 f->GetObject(HistogramName, histo);
59 if (histo) histo->SetDirectory(nullptr);
60 delete f;
61 return histo;
62}
63
64// construct transformation object, which recalculate bin numbers to new scale values
65// here simple linear transformation is used
67{
68 TGo4FitLinearTrans* trans = new TGo4FitLinearTrans("trans","linear axis transformation");
69 trans->SetCoefByRange(3800,0.,3.8);
70 return trans;
71}
72
74{
75// create fitter and select function to fit
76 TGo4Fitter *fitter = new TGo4Fitter("Fitter", TGo4Fitter::ff_ML_Poisson, kFALSE);
77
78// create object to fit for first histogram, but specify histogram later
79 TGo4FitDataHistogram *data1 = new TGo4FitDataHistogram("data1", nullptr);
80 data1->SetUseBinScale(kTRUE);
81 data1->SetRange(0,2.2,2.9);
82 data1->SetNumberOfTransSlots(1);
83 fitter->AddData(data1);
84
85// create object to fit for second histogram, but specify histogram later
86 TGo4FitDataHistogram *data2 = new TGo4FitDataHistogram("data2", nullptr);
87 data2->SetUseBinScale(kTRUE);
88 data2->SetRange(0,2.2,2.9);
89 data2->SetNumberOfTransSlots(1);
90 fitter->AddData(data2);
91
92 fitter->ConnectSlots("data1.Trans0","data2.Trans0");
93
94 TGo4FitModel *gauss1 = new TGo4FitModelGauss1("Gauss1",2.553,0.015);
95 gauss1->AssignToData("data1"); gauss1->AssignToData("data2",1.2);
96
97 TGo4FitModel *gauss2 = new TGo4FitModelGauss1("Gauss2",2.672,0.015);
98 gauss2->AssignToData("data1"); gauss2->AssignToData("data2",1.2);
99
100 fitter->AddModel( "data1", new TGo4FitModelPolynom("Pol1_0",0.) );
101 fitter->AddModel( "data1", new TGo4FitModelPolynom("Pol1_1",1.) );
102 fitter->AddModel( "data2", new TGo4FitModelPolynom("Pol2_0",0.) );
103 fitter->AddModel( "data2", new TGo4FitModelPolynom("Pol2_1",1.) );
104
105 fitter->AddModel(gauss1);
106 fitter->AddModel(gauss2);
107 fitter->AddModel( "data2", new TGo4FitModelGauss1("Gauss3",2.597,0.014) );
108 fitter->AddModel( "data2", new TGo4FitModelGauss1("Gauss4",2.717,0.014) );
109
110// add ampl estimation action
111 fitter->AddAmplEstimation(1);
112
113// create minimizer and set it to fitter
114 TGo4FitMinuit* fMinuit = new TGo4FitMinuit("Minuit");
115 fMinuit->AddCommand("MIGRAD 500 1");
116 fMinuit->AddCommand("MINOS 20");
117 fitter->AddAction(fMinuit);
118
119// set usage of buffers only for data objects, not for models
120// this highly increase speed of evaluations
121 fitter->SetMemoryUsage(1);
122
123 return fitter;
124}
125
126// store fitter with all supplied objects
128{
129 TFile *f = TFile::Open("Example7.root","recreate");
130 if (f) fitter->Write("Fitter");
131 delete f;
132}
133
134// read fitter from file
136{
137 TFile *f = TFile::Open("Example7.root");
138 if (!f) return nullptr;
139 TGo4Fitter *fitter = nullptr;
140 f->GetObject("Fitter", fitter);
141 delete f;
142 return fitter;
143}
144
146{
147// create fitter
148 TGo4Fitter *fitter = BuildFitter();
149
150// store empty fitter to file and restore again
151 StoreFitter(fitter);
152 delete fitter;
153
154 fitter = RestoreFitter();
155
156// construct axis transformation object and set it for both data object
157// slot of both data object was connected
158 fitter->SetObject(ConstructTrans(), kTRUE);
159
160// assign histograms to fitter with ownership flag
161 fitter->SetObject("data1", GetHistogram("hDeg120_P_c"), kTRUE);
162 fitter->SetObject("data2", GetHistogram("hDeg120_CND"), kTRUE);
163
164// do fit
165 fitter->DoActions();
166
167// store fitter to file and destroy it
168 StoreFitter(fitter);
169
170 delete fitter;
171
172
173// restore fitter from file
174// logically this is independent part and can be placed anywhere, just in another program
175 fitter = RestoreFitter();
176
177// show results of fitting
178 fitter->Print("Pars");
179 fitter->Draw("#data1,Gauss1,Gauss2");
180 fitter->Draw("#data2,Gauss1,Gauss2,Gauss3,Gauss4");
181
182 delete fitter;
183}
int main(int argc, char **argv)
Definition Example7.cxx:39
TGo4Fitter * RestoreFitter()
Definition Example7.cxx:135
TGo4FitAxisTrans * ConstructTrans()
Definition Example7.cxx:66
TH1D * GetHistogram(const char *HistogramName)
Definition Example7.cxx:53
TGo4Fitter * BuildFitter()
Definition Example7.cxx:73
void Example7()
Definition Example7.cxx:145
void StoreFitter(TGo4Fitter *fitter)
Definition Example7.cxx:127
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.
Bool_t SetNumberOfTransSlots(Int_t nslots)
Sets number of slots for scale transformation objects.
void SetUseBinScale(Bool_t iUseBinScale)
If kTRUE, binary numbers will be used as scale values.
Definition TGo4FitData.h:74
The linear transformation of selected data axis.
void SetCoefByRange(Int_t nbins, Double_t y1, Double_t y2)
Minuit minimization action.
void AddCommand(const char *iCommand)
Add Minuit command to command list.
One dimensional gaussian peak.
Model objects, which reproduce component of polynomial function.
Basic abstract class for representing model components of fitted data.
void AssignToData(const char *DataName, Double_t RatioValue=1., Bool_t FixRatio=kFALSE)
Assign model to specified data object.
TGo4FitSlot * SetObject(TObject *obj, Bool_t iOwned=kFALSE)
Set object to first suitable slot with defined ownership flag.
Bool_t ConnectSlots(TGo4FitSlot *slot1, TGo4FitSlot *slot2)
Connects first slot to second.
void DoActions(Bool_t AllowFitterChange=kFALSE, TObjArray *Actions=nullptr)
Executes actions list.
void AddAction(TGo4FitterAction *Action)
Add action to action list.
Central class of Go4Fit package.
Definition TGo4Fitter.h:38
void Print(Option_t *option="") const override
Print containment of fitter.
TGo4FitData * AddData(TGo4FitData *d)
Add data object to fitter.
void AddAmplEstimation(Int_t NumIters=1)
Add amplitude estimation to actions list.
void SetMemoryUsage(Int_t iMemoryUsage)
Set value of memory usage.
void Draw(Option_t *option) override
Draw fitter on current canvas.
TGo4FitModel * AddModel(TGo4FitModel *m)
Add model component to fitter.