// ----------------------------------------------------------------------------------- // /* ROOT macro for reading text (ASCII) files and fitting data written. Author: Troels C. Petersen (NBI) Email: petersen@nbi.dk Date: 4th of October 2011 */ // ----------------------------------------------------------------------------------- // #include #include #include // ----------------------------------------------------------------------------------- // void HarmonicOscillator_Fys2Lab_FitData() { // ----------------------------------------------------------------------------------- // // Setting of general plotting style: gStyle->SetCanvasColor(0); gStyle->SetFillColor(0); // Setting what to be shown in statistics box: gStyle->SetOptStat("e"); gStyle->SetOptFit(1111); int NeventsMax = 3617; int Nevents = 0; double t, x; double t_min = 0.0; double t_max = 0.01 * double(NeventsMax-1); TH1F* Hist_Osc = new TH1F("Hist_Osc", "Hist_Osc", NeventsMax, t_min-0.005, t_max+0.005); // File to read: ifstream infile("Exp_Osc_V2.txt"); if (infile.is_open()) { while ((!infile.eof()) && (Nevents < NeventsMax)) { infile >> t >> x; Hist_Osc->SetBinContent(Nevents+1, x); // Value measured at time t Hist_Osc->SetBinError(Nevents+1, 0.0025); // Error ESTIMATED for this value! // Write out the first 10 measurements: if (Nevents < 10) printf(" %2d. events read: %6.3f %6.3f \n", Nevents, t, x); Nevents++; } } infile.close(); printf(" Number of entries in total: %6d \n", Nevents); //---------------------------------------------------------------------------------- // Fit and Plot: //---------------------------------------------------------------------------------- TCanvas* c0 = new TCanvas("c0", "", 50, 20, 1200, 500); // Fitting histogram with oscillating function (NOTE: Give reasonable starting values!!!): TF1 *fitOscExp = new TF1("fitOscExp", "[0] + [1]*cos([2]*x + [3])", 1.005, 3.005); fitOscExp->SetParameters(0.0, 0.15, 9.0, 0.0); fitOscExp->SetLineWidth(2); fitOscExp->SetNpx(1000); // Setting range, line width and color and axis titles of histograms: Hist_Osc->GetXaxis()->SetRangeUser(-0.005, 36.165); Hist_Osc->GetXaxis()->SetTitle("Time (seconds)"); Hist_Osc->GetYaxis()->SetTitle("Oscillation (arbitrary scale)"); Hist_Osc->SetLineWidth(2); Hist_Osc->SetLineColor(4); // Fitting and drawing histograms with errors: Hist_Osc->Fit("fitOscExp", "R"); Hist_Osc->Draw("e"); c0->Update(); // c0->SaveAs("Fys2Lab_HarmonicOscillator_OscExpFit.png"); } // ----------------------------------------------------------------------------------- // /* Questions: ---------- 1) Look at the data file and see if you can by eye estimate the size of the uncertainty of the points. It is not easy, but you should be able to get it to within a factor of 2-3. 2) Play with the fitting function and introduce terms to reduce the Chi2. See if you in a fit of the range [0.005,36.005] can get a better Chi2 than 6600. 3) If you were to measure the oscillation frequency for (infinitely) small oscillations, what would your estimate be? Can you make a confidence interval? Advanced questions: ------------------- 1) Can you detect a change in the error as a function of time? I.e. does the errors needed to get a reasonable/constant Chi2 get better or worse if you fit small ranges in time. */ // ----------------------------------------------------------------------------------- //