GSI Object Oriented Online Offline (Go4)  GO4-5.3.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Example7.cxx
Go to the documentation of this file.
1 // $Id: Example7.cxx 555 2010-01-27 12:40:54Z linev $
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 für 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 N7
15  Simultaneous fit of two histogram
16 */
17 
18 #ifndef __CINT__
19 
20 #include "TH1.h"
21 #include "TFile.h"
22 #include "TCollection.h"
23 #include "TApplication.h"
24 
25 #include "TGo4FitMinuit.h"
26 #include "TGo4Fitter.h"
27 #include "TGo4FitDataHistogram.h"
28 #include "TGo4FitModelPolynom.h"
29 #include "TGo4FitModelGauss1.h"
30 #include "TGo4FitAxisTrans.h"
31 #include "TGo4FitLinearTrans.h"
32 #include "TGo4FitterOutput.h"
33 
34 void Example7();
35 
36 int main(int argc, char **argv)
37 {
38  TApplication theApp("Application", 0, 0);
39 
40  Example7();
41 
42  theApp.Run();
43 
44  return 0;
45 }
46 
47 #endif
48 
49 // routine to read histogram from examples file
50 TH1D* GetHistogram(const char* HistogramName)
51 {
52  TFile* f1 = TFile::Open("histograms.root");
53  if (f1==0) return 0;
54  TH1D* histo = (TH1D*) f1->Get(HistogramName);
55  if (histo) histo->SetDirectory(0);
56  return histo;
57 }
58 
59 // construct transformation object, which recalculate bin numbers to new scale values
60 // here simple linear transformation is used
62 {
63  TGo4FitLinearTrans* trans = new TGo4FitLinearTrans("trans","linear axis transformation");
64  trans->SetCoefByRange(3800,0.,3.8);
65  return trans;
66 }
67 
69 {
70 // create fitter and select function to fit
71  TGo4Fitter *fitter = new TGo4Fitter("Fitter", TGo4Fitter::ff_ML_Poisson, kFALSE);
72 
73 // create object to fit for first histogram, but specify histogram later
74  TGo4FitDataHistogram *data1 = new TGo4FitDataHistogram("data1",0);
75  data1->SetUseBinScale(kTRUE);
76  data1->SetRange(0,2.2,2.9);
77  data1->SetNumberOfTransSlots(1);
78  fitter->AddData(data1);
79 
80 // create object to fit for second histogram, but specify histogram later
81  TGo4FitDataHistogram *data2 = new TGo4FitDataHistogram("data2",0);
82  data2->SetUseBinScale(kTRUE);
83  data2->SetRange(0,2.2,2.9);
84  data2->SetNumberOfTransSlots(1);
85  fitter->AddData(data2);
86 
87  fitter->ConnectSlots("data1.Trans0","data2.Trans0");
88 
89  TGo4FitModel* gauss1 = new TGo4FitModelGauss1("Gauss1",2.553,0.015);
90  gauss1->AssignToData("data1"); gauss1->AssignToData("data2",1.2);
91 
92  TGo4FitModel* gauss2 = new TGo4FitModelGauss1("Gauss2",2.672,0.015);
93  gauss2->AssignToData("data1"); gauss2->AssignToData("data2",1.2);
94 
95  fitter->AddModel( "data1", new TGo4FitModelPolynom("Pol1_0",0.) );
96  fitter->AddModel( "data1", new TGo4FitModelPolynom("Pol1_1",1.) );
97  fitter->AddModel( "data2", new TGo4FitModelPolynom("Pol2_0",0.) );
98  fitter->AddModel( "data2", new TGo4FitModelPolynom("Pol2_1",1.) );
99 
100  fitter->AddModel(gauss1);
101  fitter->AddModel(gauss2);
102  fitter->AddModel( "data2", new TGo4FitModelGauss1("Gauss3",2.597,0.014) );
103  fitter->AddModel( "data2", new TGo4FitModelGauss1("Gauss4",2.717,0.014) );
104 
105 // add ampl estimation action
106  fitter->AddAmplEstimation(1);
107 
108 // create minimizer and set it to fitter
109  TGo4FitMinuit* fMinuit = new TGo4FitMinuit("Minuit");
110  fMinuit->AddCommand("MIGRAD 500 1");
111  fMinuit->AddCommand("MINOS 20");
112  fitter->AddAction(fMinuit);
113 
114 // set usage of buffers only for data objects, not for models
115 // this highly increase speed of evaluations
116  fitter->SetMemoryUsage(1);
117 
118  return fitter;
119 }
120 
121 // store fitter with all supplied objects
122 void StoreFitter(TGo4Fitter* fitter)
123 {
124  TFile* f = TFile::Open("Example7.root","recreate");
125  if (f!=0) fitter->Write("Fitter");
126  delete f;
127 }
128 
129 // read fitter from file
131 {
132  TFile* f = TFile::Open("Example7.root");
133  TGo4Fitter* fitter = (TGo4Fitter*) (f ? f->Get("Fitter") : 0);
134  delete f;
135  return fitter;
136 }
137 
138 void Example7()
139 {
140 // create fitter
141  TGo4Fitter* fitter = BuildFitter();
142 
143 // store empty fitter to file and restore again
144  StoreFitter(fitter);
145  delete fitter;
146 
147  fitter = RestoreFitter();
148 
149 // construct axis transformation object and set it for both data object
150 // slot of both data object was connected
151  fitter->SetObject(ConstructTrans(), kTRUE);
152 
153 // assign histograms to fitter with ownership flag
154  fitter->SetObject("data1", GetHistogram("hDeg120_P_c"), kTRUE);
155  fitter->SetObject("data2", GetHistogram("hDeg120_CND"), kTRUE);
156 
157 // do fit
158  fitter->DoActions();
159 
160 // store fitter to file and destroy it
161  StoreFitter(fitter);
162 
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->Print("Pars");
172  fitter->Draw("#data1,Gauss1,Gauss2");
173  fitter->Draw("#data2,Gauss1,Gauss2,Gauss3,Gauss4");
174 
175  delete fitter;
176 }
virtual void Print(Option_t *option) const
Definition: TGo4Fitter.cxx:707
TGo4FitSlot * SetObject(TObject *obj, Bool_t iOwned=kFALSE)
void SetMemoryUsage(Int_t iMemoryUsage)
Definition: TGo4Fitter.cxx:72
virtual void Draw(Option_t *option)
Definition: TGo4Fitter.cxx:983
void AddAmplEstimation(Int_t NumIters=1)
Definition: TGo4Fitter.cxx:654
Bool_t SetNumberOfTransSlots(Int_t nslots)
Definition: TGo4FitData.cxx:55
void AssignToData(const char *DataName, Double_t RatioValue=1., Bool_t FixRatio=kFALSE)
int main(int argc, char **argv)
Definition: Example7.cxx:36
void AddCommand(const char *iCommand)
void AddAction(TGo4FitterAction *Action)
TH1D * GetHistogram(const char *HistogramName)
Definition: Example7.cxx:50
TGo4FitData * AddData(TGo4FitData *d)
Definition: TGo4Fitter.cxx:118
void SetCoefByRange(Int_t nbins, Double_t y1, Double_t y2)
Bool_t ConnectSlots(TGo4FitSlot *slot1, TGo4FitSlot *slot2)
void SetRange(Int_t naxis, Double_t min, Double_t max)
void DoActions(Bool_t AllowFitterChange=kFALSE, TObjArray *Actions=0)
void Example7()
Definition: Example7.cxx:138
TGo4Fitter * BuildFitter()
Definition: Example7.cxx:68
TGo4FitAxisTrans * ConstructTrans()
Definition: Example7.cxx:61
TGo4FitModel * AddModel(TGo4FitModel *m)
Definition: TGo4Fitter.cxx:203
TGo4Fitter * RestoreFitter()
Definition: Example7.cxx:130
void SetUseBinScale(Bool_t iUseBinScale)
Definition: TGo4FitData.h:67
void StoreFitter(TGo4Fitter *fitter)
Definition: Example7.cxx:122