Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

/Go4FitExample/Example7.cxx

Go to the documentation of this file.
00001 //---------------------------------------------------------------
00002 //        Go4 Release Package v2.10-5 (build 21005) 
00003 //                      03-Nov-2005
00004 //---------------------------------------------------------------
00005 //       The GSI Online Offline Object Oriented (Go4) Project
00006 //       Experiment Data Processing at DVEE department, GSI
00007 //---------------------------------------------------------------
00008 //
00009 //Copyright (C) 2000- Gesellschaft f. Schwerionenforschung, GSI
00010 //                    Planckstr. 1, 64291 Darmstadt, Germany
00011 //Contact:            http://go4.gsi.de
00012 //----------------------------------------------------------------
00013 //This software can be used under the license agreements as stated
00014 //in Go4License.txt file which is part of the distribution.
00015 //----------------------------------------------------------------
00016 /* Go4Fit Example N7
00017    Simultanious fit of two histogram
00018 */
00019 
00020 #ifndef __CINT__
00021 
00022 #include "TH1.h"
00023 #include "TFile.h"
00024 #include "TCollection.h"
00025 #include "TApplication.h"
00026 
00027 #include "../Go4Fit/TGo4FitMinuit.h"
00028 #include "../Go4Fit/TGo4Fitter.h"
00029 #include "../Go4Fit/TGo4FitDataHistogram.h"
00030 #include "../Go4Fit/TGo4FitModelPolynom.h"
00031 #include "../Go4Fit/TGo4FitModelGauss1.h"
00032 #include "../Go4Fit/TGo4FitAxisTrans.h"
00033 #include "../Go4Fit/TGo4FitLinearTrans.h"
00034 #include "../Go4Fit/TGo4FitterOutput.h"
00035 
00036 void Example7();
00037 
00038 int main(int argc, char **argv)
00039 {
00040    TApplication theApp("Application", 0, 0);
00041 
00042    Example7();
00043 
00044    theApp.Run();
00045 
00046    return 0;
00047 }
00048 
00049 #endif
00050 
00051 // routine to read histogram from examples file
00052 TH1D* GetHistogram(const char* HistogramName) {
00053    TFile f1("histograms.root");
00054    TH1D* histo = (TH1D*) f1.Get(HistogramName);
00055    histo->SetDirectory(0);
00056    return histo;
00057 }
00058 
00059 // constrcut transformation object, which recalculate bin numbers to new scale values
00060 // here simple linear transformation  is used
00061 TGo4FitAxisTrans* ConstructTrans() {
00062    TGo4FitLinearTrans* trans = new TGo4FitLinearTrans("trans","linear axis transformation");
00063    trans->SetCoefByRange(3800,0.,3.8);
00064    return trans;
00065 }
00066 
00067 TGo4Fitter* BuildFitter() {
00068 // create fitter and select function to fit
00069    TGo4Fitter *fitter = new TGo4Fitter("Fitter", TGo4Fitter::ff_ML_Poisson, kFALSE);
00070 
00071 // create object to fit for first histogram, but specify histogram later
00072    TGo4FitDataHistogram *data1 = new TGo4FitDataHistogram("data1",0);
00073    data1->SetUseBinScale(kTRUE);
00074    data1->SetRange(0,2.2,2.9);
00075    data1->SetNumberOfTransSlots(1);
00076    fitter->AddData(data1);
00077 
00078 // create object to fit for second histogram, but specify histogram later
00079    TGo4FitDataHistogram *data2 = new TGo4FitDataHistogram("data2",0);
00080    data2->SetUseBinScale(kTRUE);
00081    data2->SetRange(0,2.2,2.9);
00082    data2->SetNumberOfTransSlots(1);
00083    fitter->AddData(data2);
00084 
00085    fitter->ConnectSlots("data1.Trans0","data2.Trans0");
00086 
00087    TGo4FitModel* gauss1 = new TGo4FitModelGauss1("Gauss1",2.553,0.015);
00088    gauss1->AssignToData("data1"); gauss1->AssignToData("data2",1.2);
00089 
00090    TGo4FitModel* gauss2 = new TGo4FitModelGauss1("Gauss2",2.672,0.015);
00091    gauss2->AssignToData("data1"); gauss2->AssignToData("data2",1.2);
00092 
00093    fitter->AddModel( "data1", new TGo4FitModelPolynom("Pol1_0",0.) );
00094    fitter->AddModel( "data1", new TGo4FitModelPolynom("Pol1_1",1.) );
00095    fitter->AddModel( "data2", new TGo4FitModelPolynom("Pol2_0",0.) );
00096    fitter->AddModel( "data2", new TGo4FitModelPolynom("Pol2_1",1.) );
00097 
00098    fitter->AddModel(gauss1);
00099    fitter->AddModel(gauss2);
00100    fitter->AddModel( "data2", new TGo4FitModelGauss1("Gauss3",2.597,0.014) );
00101    fitter->AddModel( "data2", new TGo4FitModelGauss1("Gauss4",2.717,0.014) );
00102 
00103 // add ampl estimation action
00104    fitter->AddAmplEstimation(1);
00105 
00106 // create minimizer and set it to fitter
00107    TGo4FitMinuit* fMinuit = new TGo4FitMinuit("Minuit");
00108    fMinuit->AddCommand("MIGRAD 500 1");
00109    fMinuit->AddCommand("MINOS 20");
00110    fitter->AddAction(fMinuit);
00111 
00112 // set usage of buffers only for data objects, not for models
00113 // this highly increase speed of evaluations
00114    fitter->SetMemoryUsage(1);
00115 
00116    return fitter;
00117 }
00118 
00119 // store fitter with all supplied objects
00120 void StoreFitter(TGo4Fitter* fitter) {
00121     TFile *f = new TFile("Example7.root","recreate");
00122     fitter->Write("Fitter");
00123     delete f;
00124 }
00125 
00126 // read fitter from file
00127 TGo4Fitter* RestoreFitter() {
00128     TFile *f = new TFile("Example7.root");
00129     TGo4Fitter* fitter = (TGo4Fitter*) f->Get("Fitter");
00130     delete f;
00131     return fitter;
00132 }
00133 
00134 void Example7() {
00135 // create fitter
00136    TGo4Fitter* fitter = BuildFitter();
00137 
00138 // store empty fitter to file and restore again
00139    StoreFitter(fitter);
00140    delete fitter;
00141 
00142    fitter = RestoreFitter();
00143 
00144 // construct axis transformation object and set it for both data object
00145 // slot of both data object was connected
00146    fitter->SetObject(ConstructTrans(), kTRUE);
00147 
00148 // assign histograms to fitter with ownership flag
00149    fitter->SetObject("data1", GetHistogram("hDeg120_P_c"), kTRUE);
00150    fitter->SetObject("data2", GetHistogram("hDeg120_CND"), kTRUE);
00151 
00152 // do fit
00153    fitter->DoActions();
00154 
00155 // store fitter to file and destroy it
00156    StoreFitter(fitter);
00157 
00158    delete fitter;
00159 
00160 
00161 // restore fitter from file
00162 // logically this is independent part and can be placed anywhere, just in another program
00163    fitter = RestoreFitter();
00164 
00165 // show results of fitting
00166    fitter->Print("Pars");
00167    fitter->Draw("#data1,Gauss1,Gauss2");
00168    fitter->Draw("#data2,Gauss1,Gauss2,Gauss3,Gauss4");
00169 
00170    delete fitter;
00171 }
00172 
00173 //----------------------------END OF GO4 SOURCE FILE ---------------------

Generated on Tue Nov 8 10:55:57 2005 for Go4-v2.10-5 by doxygen1.2.15