#include "TROOT.h" #include "TFile.h" #include "TF1.h" #include "TH1.h" #include "TObjArray.h" #include "TMath.h" double mygaus(double *x, double *p) { return p[0] * TMath::Gaus(*x, p[1], p[2]); } void example() { TH1D h1("", "", 100, -3, 3); TFile f1("temp1.root", "RECREATE"), f2("temp2.root", "RECREATE"), f3("temp3.root", "RECREATE"); TF1 func2("func2", "[0]*TMath::Gaus(x, [1], [2])", -10, 10); func2.SetParameters(1, 0, 1); func2.SetNpx(1000); TF1 func3("func3", mygaus, -10, 10, 3 ); func3.SetParameters(1, 0, 1); func3.SetNpx(1000); TF1 *gf = (TF1*)gROOT->GetFunction("gaus"); gf->SetNpx(100); // simulate the data, our data are similar to gauss, but deformed and with noise, so in real example I need more complicated function char buff[100]; for(int i = 0; i < 1000; ++i) { sprintf(buff, "histo%d", i); h1.SetName(buff); h1.Reset(); h1.FillRandom("gaus", 1000); //1st is fitted with predefined "gaus" h1.Fit("gaus", "Q"); f1.WriteTObject(&h1); } printf("Test1 completed\n"); for(int i = 0; i < 1000; ++i) { sprintf(buff, "histo%d_", i); h1.SetName(buff); h1.Reset(); h1.FillRandom("gaus", 1000); //2nd is fitted with custom formula using the TMath::Gauss for simplicity h1.Fit(&func2, "Q"); f2.WriteTObject(&h1); } printf("Test2 completed\n"); TObjArray *list3 = new TObjArray(1000); for(int i = 0; i < 1000; ++i) { sprintf(buff, "histo%d__", i); TH1I *hh1 = new TH1I(buff,buff,100,-3,3); list3->AddAt(hh1,i); //hh1-SetName(buff); //h1.Reset(); hh1->FillRandom("gaus", 1000); //3rd is fitted with C function (which just use TMath::Gauss for simplicity) hh1->Fit(&func3, "Qn"); //f3.WriteTObject(&h1); } f3.WriteTObject(list3,"list3","SingleKey"); printf("Test3 completed\n"); //3rd is fitted with C function (which just use TMath::Gauss for simplicity) //h3.Fit(&func3); f1.Close(); f2.Close(); f3.Close(); }