Like to fit with Sinus, but got some Problems!

Hi!
I have a problem. I like to read an ASCII file with one colume and make a Graph or a Histogramm withe the values over the bins, afte that i like to do a Sinus-fit. Mz problem is, that i did it with Histograms, so when i fit the Histogramm, he couldn´t find the middle of the oscillation, because there is more data in below. Is there a possibility to fill just the amplitude of each bin in a Histogram or a TGraph?
I tried it with just give him the middle of the Graph, but that won´t work i think, because the Histogramm goes down to zero somewhere so my minimum would be zero, anyway here is what i did until now:

#include <Riostream.h>
#include <fstream.h>
#include <stdio.h>
#include <TH1D.h>
#include <TH2D.h>
#include "TTree.h"
#include "TFile.h"
#include "TROOT.h"
#include <iostream>

void reading1()
{

Int_t counts;


ifstream in;
in.open("c:/root/macros/Datei.txt");


TFile *f = new TFile("c:/root/macros/Datei.root","RECREATE");
TH1D *h1 = new TH1D("h1","test",1000,0,1000);
Int_t min = 999999999999;
Int_t max = 0;

Int_t nlines = 0;
while(!in.eof())
{
in >> counts;
if(counts<min) min = counts;
if(counts>max) max = counts;
if (!in.good()) break;
h1->Fill(nlines,counts);


nlines++;
}

Double_t mid = (max-min)/2;



//MyFunction *half = new MyFunction("(h1->GetMaximum-h1->GetMinimum)/2");
 Double_t myfunction(Double_t *x, Double_t *par, Double_t mid)
   {
	  Float_t xx =x[0];
      Double_t f = mid+par[0]*sin(2*pi*(xx-par[1])/par[2]);
      return f;
   }


TF1 *fit = new TF1("fit",myfunction,0,2);
h1->Fit("fit","+QEM");

in.close();

h1->Draw();

f->Write();
}

Could you post your Datei.txt input file?

Rene

sure!

1 0 3 3 12 22 68 59 69 68 63 72 75 76 91 73 87 99 95 85 79 106 110 109 92 121 94 134 108 135 126 121 108 121 118 114 120 118 136 154 142 125 145 144 147 152 138 115 169 130 134 134 127 134 124 130 124 117 107 123 98 101 104 89 77 94 101 73 98 83 63 86 72 55 65 56 62 66 75 62 53 61 47 71 58 46 59 56 61 46 70 67 73 77 80 74 57 59 63 69 62 77 80 82 79 79 77 80 78 77 81 80 108 102 93 90 100 111 124 127 110 134 120 117 137 122 117 127 118 122 119 136 129 146 140 144 119 133 136 140 139 132 116 151 118 119 146 122 128 113 111 102 102 93 110 99 110 119 98 94 96 96 81 92 90 79 78 65 76 75 74 78 69 79 65 77 56 66 67 70 70 56 63 63 69 72 84 64 78 63 83 72 74 70 75 79 74 52 70 90 74 77 87 80 84 94 79 85 90 90 82 77 91 76 98 95 89 119 96 93 111 120 120 110 119 104 138 112 139 141 136 131 126 123 142 139 130 160 128 140 140 139 134 130 132 103 120 110 124 127 100 97 110 87 80 108 92 105 92 76 72 76 73 67 74 62 66 64 74 66 54 56 47 62 71 56 63 68 60 64 68 69 73 63 64 83 80 77 93 85 81 97 66 86 83 74 90 83 102 82 87 84 93 76 101 100 101 88 95 108 115 108 107 102 123 99 120 130 115 117 120 143 117 118 104 117 118 141 130 133 132 118 133 120 119 138 112 125 112 126 121 123 137 115 99 104 94 103 112 85 93 105 99 78 87 78 82 65 73 65 68 71 68 72 78 61 62 61 65 64 69 67 58 61 63 70 70 56 62 63 66 56 78 100 75 88 81 82 106 69 83 79 95 89 94 89 90 94 97 103 95 112 118 117 113 116 120 119 125 135 123 121 132 112 130 140 140 133 124 148 136 111 117 146 130 119 128 103 112 125 138 109 117 122 109 94 102 95 90 93 100 88 98 91 88 96 76 86 76 76 80 58 86 59 79 60 52 55 63 71 71 69 67 69 56 55 61 58 56 74 66 75 57 51 64 75 81 83 68 75 84 94 69 86 100 72 88 95 95 92 93 94 118 99 105 122 120 113 121 137 128 113 106 128 127 136 116 152 111 144 141 142 117 124 152 130 101 141 125 103 117 110 108 102 100 100 116 108 96 95 114 99 74 83 89 76 95 91 77 75 79 83 73 73 63 66 62 63 67 66 59 71 60 61 66 57 71 65 69 58 56 62 71 62 55 76 77 82 80 79 82 83 81 79 92 84 87 88 88 93 101 92 124 111 113 112 104 96 107 103 116 130 119 134 99 108 127 152 127 125 125 125 134 122 132 139 125 120 133 126 129 127 125 115 125 124 118 126 110 106 104 111 88 114 104 105 89 105 87 104 94 104 104 86 90 75 85 86 70 63 74 74 73 77 71 74 61 66 59 74 55 65 72 62 67 60 68 65 62 68 86 79 65 80 77 66 74 73 83 98 89 96 97 74 99 114 104 117 104 119 115 99 120 97 127 110 119 120 109 118 137 93 133 137 118 121 130 108 134 131 133 132 117 131 126 141 130 126 116 111 120 117 128 108 128 124 108 92 90 91 98 98 102 87 83 93 85 86 75 86 65 87 57 70 69 69 58 52 51 65 73 71 47 58 73 79 70 61 56 62 75 72 51 64 68 60 81 72 80 82 80 93 81 98 117 96 106 80 108 105 100 106 120 116 109 13

Hi,

here attached is a macro which fills the histogram and performs the fit. However the fit will not work correctly. In order to have t he fit working you might need an estimation of the error for each data point.

Best Regards

Lorenzo
mk.C (1.02 KB)

Hi,
the estimation of your fit parameters is not quite accurate.
First, the mean value is not calculated correctly, because the constant, you’ve given, is too large to be hold by an Int_t

Fix1:
< Int_t min = 999999999999;

Int_t min = kMaxInt;

Because your fit function is quite problematic, the estimate for the frequency needs to be better.
Fix2:
< fit->SetParameters(40,50,10,mid);

fit->SetParameters(40,50,100,mid);

Your fit function is problematic, because it has several minima. If you apply a shift of the multiple of the frequency, you find an additional minimum. You should probably reformulate the shift as a “phase” and limit the “phase”-parameter with help of the TF1::SetParLimits method to ]-pi;pi].

Finally you should not include the empty region of the histogram:
Fix3:
< h1->Fit(“fit”,"");

h1->Fit(“fit”,"","",0+20,nlines-20);

Alternatively you might include the empty regions but assure, that these bins get zero weight in the fit by giving these bins an infinite error.

(@ROOT experts : Is there a “ROOT way” to “exclude” empty bins from a fit?)

Cheers, J.
mk.C (1.04 KB)

Empty bins are always excluded by default in a standard least square fits of an histogram.

Lorenzo

Thanks for your help, does not work until now, but i do my best!
Would the fit be better if i would use a TGraph? Maybe than it wouldnt’be necessary to do that mid-stuff.

Greets MK

Hi,

yes, in your case, where each point does not represent counts, it is better in general to use a TGraph (or TGraphErrors if you have error in x and/or y).
When fitting a TGraph (without error), the weight of each data point is 1.
However your data have many bad measurements, so I guess it will be difficult to have a successful fit also in that case.

Regards

Lorenzo