void ThreeDFit() { const int n = 1000; double x[n], y[n], z[n], v[n]; double ev = 0.1; // generate the data TRandom2 r; for (int i = 0; i < n; ++i) { x[i] = r.Uniform(0,10); y[i] = r.Uniform(0,10); z[i] = r.Uniform(0,10); v[i] = sin(x[i] ) + cos(y[i]) + z[i] + r.Gaus(0,ev); } // create a 3d binned data structure ROOT::Fit::BinData data(n,3); double xx[3]; for(int i = 0; i < n; ++i) { xx[0] = x[i]; xx[1] = y[i]; xx[2] = z[i]; // add the 3d-data coordinate, the predictor value (v[i]) and its errors data.Add(xx, v[i], ev); } TF3 * f3 = new TF3("f3","[0] * sin(x) + [1] * cos(y) + [2] * z",0,10,0,10,0,10); f3->SetParameters(2,2,2); ROOT::Fit::Fitter fitter; // wrapped the TF1 in a IParamMultiFunction interface for teh Fitter class ROOT::Math::WrappedMultiTF1 wf(*f3,3); fitter.SetFunction(wf); // bool ret = fitter.Fit(data); if (ret) { const ROOT::Fit::FitResult & res = fitter.Result(); // print result (should be around 1) res.Print(std::cout); // copy all fit result info (values, chi2, etc..) in TF3 f3->SetFitResult(res); // test fit p-value (chi2 probability) double prob = res.Prob(); if (prob < 1.E-2) Error("exampleFit3D","Bad data fit - fit p-value is %f",prob); else std::cout << "Good fit : p-value = " << prob << std::endl; } else Error("exampleFit3D","3D fit failed"); }
TH1* GenGeResponse(double keV0,TH1* hist,int N,double reskeV,double frakpeak,double fraccomp,double contshap,double cont){ TF1* GeResponse=GenGeResponseA(keV0,frakpeak,fraccomp,contshap,cont); TH1* response=hist; if(!response)response=new TH1D("GeResponse","GeResponse",8000,0,2000); double RES=reskeV*sqrt(keV0)/sqrt(500);//define res as res at 500keV TRandom2 rand;rand.SetSeed(); for(int i=0;i<N;i++){ double E=GeResponse->GetRandom(); //Res at peak is normal, everything below photopeak = very messy double res=pow(E/keV0,4);if(res>1)res=1; res=(3-(res*2))*reskeV; E+=rand.Gaus(0,res); response->Fill(E); } delete GeResponse; return response; }
void write(int n) { TRandom2 R; TStopwatch timer; R.SetSeed(1); timer.Start(); double s = 0; for (int i = 0; i < n; ++i) { s += R.Gaus(0,10); s += R.Gaus(0,10); s += R.Gaus(0,10); s += R.Gaus(100,10); } timer.Stop(); std::cout << s/double(n) << std::endl; std::cout << " Time for Random gen " << timer.RealTime() << " " << timer.CpuTime() << std::endl; TFile f1("mathcoreVectorIO_1.root","RECREATE"); // create tree TTree t1("t1","Tree with new LorentzVector"); XYZTVector *v1 = new XYZTVector(); t1.Branch("LV branch","ROOT::Math::XYZTVector",&v1); R.SetSeed(1); timer.Start(); for (int i = 0; i < n; ++i) { double Px = R.Gaus(0,10); double Py = R.Gaus(0,10); double Pz = R.Gaus(0,10); double E = R.Gaus(100,10); //CylindricalEta4D<double> & c = v1->Coordinates(); //c.SetValues(Px,pY,pZ,E); v1->SetCoordinates(Px,Py,Pz,E); t1.Fill(); } f1.Write(); timer.Stop(); std::cout << " Time for new Vector " << timer.RealTime() << " " << timer.CpuTime() << std::endl; t1.Print(); // create tree with old LV TFile f2("mathcoreVectorIO_2.root","RECREATE"); TTree t2("t2","Tree with TLorentzVector"); TLorentzVector * v2 = new TLorentzVector(); TLorentzVector::Class()->IgnoreTObjectStreamer(); TVector3::Class()->IgnoreTObjectStreamer(); t2.Branch("TLV branch","TLorentzVector",&v2,16000,2); R.SetSeed(1); timer.Start(); for (int i = 0; i < n; ++i) { double Px = R.Gaus(0,10); double Py = R.Gaus(0,10); double Pz = R.Gaus(0,10); double E = R.Gaus(100,10); v2->SetPxPyPzE(Px,Py,Pz,E); t2.Fill(); } f2.Write(); timer.Stop(); std::cout << " Time for old Vector " << timer.RealTime() << " " << timer.CpuTime() << endl; t2.Print(); }
void readinhist(){ ifstream infile("sn124p_250.out"); ifstream infile("gampi_sn120_n_fsu30_187.out"); Double_t t1[1800],t2[1800],t3[1800],t4[1800],t5[1800],t6[1800]; char dummy; for(int j = 0; j< 1800; j++){ t1[j] = .0; t2[j] = .0; t3[j] = .0; t4[j] = .0; t6[j] = 0.0; } if(!infile){ cout << "Cannot open file!!!" << endl; } else { for(int i=0; i< 1800; i++){ //while(!infile.eof()){ infile >> dummy >> t1[i] >> t2[i] >> t3[i] >> t4[i]; t5[i] = t1[i] - 0.05; } } for(i=0; i<1800; i++) cout << "t1[" << i << "]: " << t1[i] << ", t2[" << i << "]: " << t2[i] <<endl; TGraph *gr1 = new TGraph(1800,t5,t2); TRandom2 *fRandom = new TRandom2(0); Double_t sigma_res = 2.0; Double_t mean_res = 90.05; // just to have everything inside bin center is bin 901 TH1F *testH1 = new TH1F("testH1","testH1",1800,0.0,180.0); Int_t n_trials = 100000; Double_t val_random; //Creating random distribution with the right sigma for (int i=0 ; i<n_trials; i++) { val_random = fRandom->Gaus(mean_res,sigma_res); testH1->Fill(val_random); } Int_t n_counts = int(3*sigma_res/testH1->GetBinWidth(1)); // I will count from -3sigma to 3sigma for (int i=0; i<1800; i++) { for (int j=-n_counts; j<n_counts ; j++) { if ((i+j)>=0 && (i+j)<1800) { t6[i] = t6[i] + t2[i+j] * double(testH1->GetBinContent(901+j)) / double(n_trials); } } } TGraph *gr2 = new TGraph(1800,t5,t6); TFile *fout = NULL; if( fout )delete fout; fout = new TFile("sn124p_250.root","RECREATE"); gr2->Write(); TCanvas *c1 = NULL; if( c1 )delete c1; c1 = new TCanvas("c1"); gr2->Draw(""); }
// this is the main program, no need to do anything here int main(int argc, char* argv[]) { // sum of squares error info for histograms TH1D::SetDefaultSumw2(); // create analysis // NoiseSystematics analyser(argc, argv); // analyser.run(); TFile* ofile = new TFile("NoiseSystematics.root", "RECREATE"); std::cout << "Output file : " << ofile->GetName() << std::endl; if (ofile->IsZombie()) { std::exit(2); } TH1F hcosmic("hcosmic","",10,0,10); TH1F hobs("hobs",";# expected events;",10,0,10); TH1F hnoise("hnoise","",10,0,10); TRandom2 rndm; for (int i = 0; i < 10000; i++) { // DT by RPC background: 0.96061 +/- 0.559146 double ncos = rndm.Poisson(rndm.Gaus (0.96, 0.56)); double nobs = rndm.PoissonD(2.); hcosmic.Fill(ncos); hobs.Fill(nobs); hnoise.Fill(nobs-ncos); //std::cout << ncos << "\t" << nobs << "\t" << nobs-ncos << std::endl; } double nq = 1; Double_t xq[1] = {0.68}; // position where to compute the quantiles in [0,1] Double_t yq[1] = {0.}; // array to contain the quantiles hnoise.GetQuantiles(nq,yq,xq); std::cout << xq[0] << "\t" << yq[0] << std::endl; ofile->cd(); ofile->Write("",TObject::kOverwrite); TCanvas c("c"); hnoise.SetStats(0); hobs.SetStats(0); hcosmic.SetStats(0); hcosmic.SetLineColor(kRed); //hcosmic.Scale(1.4); hobs.SetLineColor(kBlue); hnoise.SetLineColor(kBlack); hcosmic.SetMaximum(hcosmic.GetMaximum()*1.1); hcosmic.Draw("hist"); hobs.Draw("hist same"); hnoise.Draw("hist same"); c.Update(); TLine line(yq[0],0,yq[0],hcosmic.GetMaximum()); line.SetLineColor(kBlack); line.SetLineStyle(5); line.SetLineWidth(2); line.Draw(); TLegend leg(0.6, 0.70, 0.88, 0.85); leg.SetFillColor(kWhite); leg.AddEntry("hcosmic", "N_{cosmic}", "l"); leg.AddEntry("hobs", "N_{obs} [#lambda = 2]","l"); leg.AddEntry("hnoise", "n_{noise} = N_{obs} - N_{cosmic}","l"); leg.SetBorderSize(0); leg.Draw(); c.SaveAs("NoiseSystematics.pdf"); return 0; }
static inline double GausAdd2(double val, double sigma) { return rnd.Gaus(val, val*sigma); };
static inline double GausAdd(double val, double sigma) { return rnd.Gaus(val, sqrt(val)*sigma); };
static inline double Gaus(double mean = 0, double sigma = 1) { return rnd.Gaus(mean, sigma); };
void limit() { //This program demonstrates the computation of 95 % C.L. limits. //It uses a set of randomly created histograms. // //Author: [email protected] on 21.08.02 // Create a new canvas. TCanvas *c1 = new TCanvas("c1","Dynamic Filling Example",200,10,700,500); c1->SetFillColor(42); // Create some histograms TH1D* background = new TH1D("background","The expected background",30,-4,4); TH1D* signal = new TH1D("signal","the expected signal",30,-4,4); TH1D* data = new TH1D("data","some fake data points",30,-4,4); background->SetFillColor(48); signal->SetFillColor(41); data->SetMarkerStyle(21); data->SetMarkerColor(kBlue); background->Sumw2(); // needed for stat uncertainty signal->Sumw2(); // needed for stat uncertainty // Fill histograms randomly TRandom2 r; Float_t bg,sig,dt; for (Int_t i = 0; i < 25000; i++) { bg = r.Gaus(0,1); sig = r.Gaus(1,.2); background->Fill(bg,0.02); signal->Fill(sig,0.001); } for (Int_t i = 0; i < 500; i++) { dt = r.Gaus(0,1); data->Fill(dt); } THStack *hs = new THStack("hs","Signal and background compared to data..."); hs->Add(background); hs->Add(signal); hs->Draw("hist"); data->Draw("PE1,Same"); c1->Modified(); c1->Update(); c1->GetFrame()->SetFillColor(21); c1->GetFrame()->SetBorderSize(6); c1->GetFrame()->SetBorderMode(-1); c1->Modified(); c1->Update(); gSystem->ProcessEvents(); // Compute the limits cout << "Computing limits... " << endl; TLimitDataSource* mydatasource = new TLimitDataSource(signal,background,data); TConfidenceLevel *myconfidence = TLimit::ComputeLimit(mydatasource,50000); cout << "CLs : " << myconfidence->CLs() << endl; cout << "CLsb : " << myconfidence->CLsb() << endl; cout << "CLb : " << myconfidence->CLb() << endl; cout << "< CLs > : " << myconfidence->GetExpectedCLs_b() << endl; cout << "< CLsb > : " << myconfidence->GetExpectedCLsb_b() << endl; cout << "< CLb > : " << myconfidence->GetExpectedCLb_b() << endl; // Add stat uncertainty cout << endl << "Computing limits with stat systematics... " << endl; TConfidenceLevel *mystatconfidence = TLimit::ComputeLimit(mydatasource,50000,true); cout << "CLs : " << mystatconfidence->CLs() << endl; cout << "CLsb : " << mystatconfidence->CLsb() << endl; cout << "CLb : " << mystatconfidence->CLb() << endl; cout << "< CLs > : " << mystatconfidence->GetExpectedCLs_b() << endl; cout << "< CLsb > : " << mystatconfidence->GetExpectedCLsb_b() << endl; cout << "< CLb > : " << mystatconfidence->GetExpectedCLb_b() << endl; // Add some systematics cout << endl << "Computing limits with systematics... " << endl; TVectorD errorb(2); TVectorD errors(2); TObjArray* names = new TObjArray(); TObjString name1("bg uncertainty"); TObjString name2("sig uncertainty"); names->AddLast(&name1); names->AddLast(&name2); errorb[0]=0.05; // error source 1: 5% errorb[1]=0; // error source 2: 0% errors[0]=0; // error source 1: 0% errors[1]=0.01; // error source 2: 1% TLimitDataSource* mynewdatasource = new TLimitDataSource(); mynewdatasource->AddChannel(signal,background,data,&errors,&errorb,names); TConfidenceLevel *mynewconfidence = TLimit::ComputeLimit(mynewdatasource,50000,true); cout << "CLs : " << mynewconfidence->CLs() << endl; cout << "CLsb : " << mynewconfidence->CLsb() << endl; cout << "CLb : " << mynewconfidence->CLb() << endl; cout << "< CLs > : " << mynewconfidence->GetExpectedCLs_b() << endl; cout << "< CLsb > : " << mynewconfidence->GetExpectedCLsb_b() << endl; cout << "< CLb > : " << mynewconfidence->GetExpectedCLb_b() << endl; // show canonical -2lnQ plots in a new canvas // - The histogram of -2lnQ for background hypothesis (full) // - The histogram of -2lnQ for signal and background hypothesis (dashed) TCanvas *c2 = new TCanvas("c2"); myconfidence->Draw(); // clean up (except histograms and canvas) delete myconfidence; delete mydatasource; delete mystatconfidence; delete mynewconfidence; delete mynewdatasource; }