Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

Example6.cxx

Go to the documentation of this file.
00001 //-------------------------------------------------------------
00002 //        Go4 Release Package v3.04-01 (build 30401)
00003 //                      28-November-2008
00004 //---------------------------------------------------------------
00005 //   The GSI Online Offline Object Oriented (Go4) Project
00006 //   Experiment Data Processing at EE 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 N6
00017    Simultanious fit of two histogram
00018 */
00019 
00020 #ifndef __CINT__
00021 
00022 #include "TH1.h"
00023 #include "TFile.h"
00024 #include "TApplication.h"
00025 
00026 #include "../Go4Fit/TGo4Fitter.h"
00027 #include "../Go4Fit/TGo4FitDataHistogram.h"
00028 #include "../Go4Fit/TGo4FitModelPolynom.h"
00029 #include "../Go4Fit/TGo4FitModelGauss1.h"
00030 #include "../Go4Fit/TGo4FitAxisTrans.h"
00031 #include "../Go4Fit/TGo4FitLinearTrans.h"
00032 #include "../Go4Fit/TGo4FitterConfig.h"
00033 #include "TCollection.h"
00034 
00035 void Example6();
00036 
00037 int main(int argc, char **argv)
00038 {
00039    TApplication theApp("Application", 0, 0);
00040 
00041    Example6();
00042 
00043    theApp.Run();
00044 
00045    return 0;
00046 }
00047 
00048 #endif
00049 
00050 // routine to read histogram from examples file
00051 TH1D* GetHistogram(const char* HistogramName) {
00052    TFile f1("histograms.root");
00053    TH1D* histo = (TH1D*) f1.Get(HistogramName);
00054    histo->SetDirectory(0);
00055    return histo;
00056 }
00057 
00058 // constrcut transformation object, which recalculate bin numbers to new scale values
00059 // here simple linear transformation  is used
00060 TGo4FitAxisTrans* ConstructTrans() {
00061    TGo4FitLinearTrans* trans = new TGo4FitLinearTrans("trans","linear axis transformation");
00062    trans->SetCoefByRange(3800,0.,3.8);
00063    return trans;
00064 }
00065 
00066 TGo4Fitter* BuildFitter() {
00067 // create fitter and select function to fit
00068    TGo4Fitter *fitter = new TGo4Fitter("Fitter",TGo4Fitter::ff_ML_Poisson, kFALSE);
00069 
00070 // create object to fit for first histogram, but specify histogram later
00071    TGo4FitDataHistogram *data1 = new TGo4FitDataHistogram("data1",0);
00072    data1->SetUseBinScale(kTRUE);
00073    data1->SetRange(0,2.2,2.9);
00074    fitter->AddData(data1);
00075 
00076 // create four components for model of the first histogram
00077    fitter->AddModel( "data1", new TGo4FitModelPolynom("Pol1_0",0.) );
00078    fitter->AddModel( "data1", new TGo4FitModelPolynom("Pol1_1",1.) );
00079    fitter->AddModel( "data1", new TGo4FitModelGauss1("Gauss1_1",2.553,0.015) );
00080    fitter->AddModel( "data1", new TGo4FitModelGauss1("Gauss2_1",2.672,0.015) );
00081 
00082 // create object to fit for second histogram, but specify histogram later
00083    TGo4FitDataHistogram *data2 = new TGo4FitDataHistogram("data2",0);
00084    data2->SetUseBinScale(kTRUE);
00085    data2->SetRange(0,2.2,2.9);
00086    fitter->AddData(data2);
00087 
00088 // create six components for model of the first histogram
00089    fitter->AddModel( "data2", new TGo4FitModelPolynom("Pol2_0",0.) );
00090    fitter->AddModel( "data2", new TGo4FitModelPolynom("Pol2_1",1.) );
00091    fitter->AddModel( "data2", new TGo4FitModelGauss1("Gauss1_2",2.553,0.015) );
00092    fitter->AddModel( "data2", new TGo4FitModelGauss1("Gauss2_2",2.672,0.015) );
00093    fitter->AddModel( "data2", new TGo4FitModelGauss1("Gauss3_2",2.597,0.014) );
00094    fitter->AddModel( "data2", new TGo4FitModelGauss1("Gauss4_2",2.717,0.014) );
00095 
00096 // specify init value and dependency between parameters
00097    TGo4FitterConfig* config = new TGo4FitterConfig("Config","fitter configuration");
00098    config->SetParInit("Pol1_0.Ampl",40);
00099    config->SetParInit("Pol1_1.Ampl",-0.01);
00100    config->SetParInit("Pol2_0.Ampl",500);
00101    config->SetParInit("Pol2_1.Ampl",-0.1);
00102    config->SetParDepend("Gauss1_2.Pos","Gauss1_1.Pos");
00103    config->SetParDepend("Gauss1_2.Width","Gauss1_1.Width");
00104    config->SetParDepend("Gauss2_2.Pos","Gauss2_1.Pos");
00105    config->SetParDepend("Gauss2_2.Width","Gauss2_1.Width");
00106    fitter->AddAction(config);
00107 
00108 // add standard actions to fitter
00109    fitter->AddStandardActions();
00110 
00111 // set usage of buffers only for data objects, not for models
00112 // this highly increase speed of evaluations
00113    fitter->SetMemoryUsage(1);
00114 
00115    return fitter;
00116 }
00117 
00118 // store fitter with all supplied objects
00119 void StoreFitter(TGo4Fitter* fitter) {
00120     TFile *f = new TFile("Example6.root","recreate");
00121     fitter->Write("Fitter");
00122     delete f;
00123 }
00124 
00125 // read fitter from file
00126 TGo4Fitter* RestoreFitter() {
00127     TFile *f = new TFile("Example6.root");
00128     TGo4Fitter* fitter = (TGo4Fitter*) f->Get("Fitter");
00129     delete f;
00130     return fitter;
00131 }
00132 
00133 void Example6() {
00134 // create fitter
00135    TGo4Fitter* fitter = BuildFitter();
00136 
00137 // construct axis transformation object and set it for both data object, first will be owner
00138    TGo4FitAxisTrans* trans = ConstructTrans();
00139    fitter->FindData("data1")->AddAxisTrans(trans, kTRUE);
00140    fitter->FindData("data2")->AddAxisTrans(trans, kFALSE);
00141 
00142 // assign histograms to fitter with ownership flag
00143    fitter->SetObject("data1", GetHistogram("hDeg120_P_c"), kTRUE);
00144    fitter->SetObject("data2", GetHistogram("hDeg120_CND"), kTRUE);
00145 
00146 // do fit
00147    fitter->DoActions();
00148 
00149 // store fitter to file and destroy it
00150    StoreFitter(fitter);
00151    delete fitter;
00152 
00153 
00154 // restore fitter from file
00155 // logically this is independent part and can be placed anywhere, just in another program
00156    fitter = RestoreFitter();
00157 
00158 // show results of fitting
00159    fitter->PrintPars();
00160    fitter->Draw("#data1,Gauss1_1,Gauss2_1");
00161    fitter->Draw("#data2,Gauss1_2,Gauss2_2,Gauss3_2,Gauss4_2");
00162 
00163    delete fitter;
00164 }
00165 
00166 
00167 //----------------------------END OF GO4 SOURCE FILE ---------------------

Generated on Fri Nov 28 12:59:13 2008 for Go4-v3.04-1 by  doxygen 1.4.2