Subversion Repositories f9daq

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
344 f9daq 1
function StoreAndDrawFitFunction(TH1, TF1, range, delOldFunction, divid){
2
    //   - Store fitted function in histogram functions list and draw
3
    // works for 1D histogram
4
 
5
    var ndim = 1;
6
    var xmin = 0, xmax = 0, ymin = 0, ymax = 0;
7
    if(range.length >= 2){
8
      xmin = range[0];
9
      xmax = range[1]
10
    }else{
11
      return 0
12
    };//add more if needed?
13
 
14
    var funcList = GetListOfFunctions(TH1);
15
 
16
    if ((funcList.length == 0) || (funcList == undefined)){
17
      throw "StoreAndDrawFitFunction: Function list has not been created - cannot store the fitted function";
18
    }
19
    //console.log("funcList", funcList);
20
    // delete the function in the list only if
21
    // the function we are fitting is not in that list
22
    // If this is the case we re-use that function object and
23
    // we do not create a new one (if delOldFunction is true)
24
    //NEW: always remove old and add new!!!
25
    var reuseOldFunction = 0;
26
    if (delOldFunction){
27
      var i=1
28
      while(i<funcList.arr.length){
29
        //skip first beacuse it is TPaveStats
30
        //console.log(i, TF1, funcList);
31
        if((funcList.arr[i].fName != TF1.fName) || (funcList.arr[i].fFormula.fFormula != TF1.fFormula.fFormula)){
32
          //console.log("diff");
33
          funcList.RemoveAt(i);
34
 
35
        }else{
36
          //console.log("same");
37
          funcList.RemoveAt(i); //added
38
          reuseOldFunction = 1;
39
          i++;
40
        }
41
      }
42
    }
43
 
44
    var fnew1 = JSROOT.Create("TF1");
45
    fnew1 = TF1;
46
 
47
    funcList.Add(fnew1);
48
    //console.log(JSROOT.GetMainPainter(divid));
49
    SetRange(fnew1, xmin,xmax);
50
    Save(fnew1, TH1, xmin, xmax);
51
 
52
    //check if TH1's fit function is plotted
53
    var isFitPlotted = TH1.fFitPlotted;
54
    //console.log(isFitPlotted)
55
    if(!isFitPlotted){
56
      JSROOT.draw(divid, fnew1, "", function(obj){
57
        divid = obj.divid.id;
58
        var a = JSROOT.GetMainPainter(divid).draw_object;
59
        a.fFitPlotted = 1;
60
      });
61
    }else{
62
      JSROOT.redraw(divid, fnew1, "");
63
    }
64
}
65
 
66
function GetListOfFunctions(TH1){
67
    //returns TList of functions
68
    return TH1.fFunctions
69
}
70
 
71
function SetRange(TF1, xmin, xmax){
72
    TF1.Xmax = xmax;
73
    TF1.Xmin = xmin;
74
}
75
 
76
function Save(TF1, TH1, xmin, xmax){
77
    //Save values of function in array fSave
78
    //var bin1 = TH1.fXaxis.FindBin(xmin, 0);
79
    //var bin2 = TH1.fXaxis.FindBin(xmax, 0);
80
    //console.log("bin1:", bin1, "bin2", bin2);
81
    if(!TF1.fNpx) TF1.fNpx = 501; //number of  points to be plotted 
82
    //var fNsave = TF1.fNpx + 3; //number of  points to be plotted
83
    var dx = (xmax - xmin) / TF1.fNpx;
84
 
85
    var fSave = [];
86
 
87
    for(var i = 0; i <= TF1.fNpx; i++){
88
      fSave.push(TF1.evalPar(xmin + dx * i));
89
    }
90
    fSave.push(xmin);
91
    fSave.push(xmax);
92
    TF1.fSave = fSave;
93
}
94
 
95
function GetParameters(TF1){
96
    //returns parameters from fFormula
97
    var fNpar = TF1.fNpar;
98
    var parameters = [];
99
    for(var i = 0; i<fNpar; i++){
100
        parameters.push(TF1.GetParValue(i));
101
    }
102
    return parameters;      
103
}
104
 
105
function CreateTF1Fit(param, sframe){
106
  //This fuction can create gaus, pol0-5, exp and Breit-Wigner functions, or their combinations.
107
  var func = JSROOT.Create("TF1");
108
  var formula = JSROOT.Create("TFormula");
109
  var Npar = 0;
110
  formula.fClingParameters = [];
111
  formula.fFormula = "";
112
  formula.fParams = [];
113
  formula.fNparam = Npar;
114
  var funList = getFunList(sframe);
115
  if(funList[0]){
116
    //Gaus function
117
    var gausParameters = ["N", "#mu", "#sigma"];
118
    formula.fClingParameters.push(param[0]);
119
    formula.fClingParameters.push(param[1]);
120
    formula.fClingParameters.push(param[2]);
121
    formula.fFormula =  "[N] * TMath::Gaus(x, [#mu], [#sigma])";
122
 
123
    for(var i = 0; i<3;i++){
124
      formula.fParams.push(JSROOT.Create("pair<TString,int,TFormulaParamOrder>"));
125
      formula.fParams[i].first = gausParameters[i];
126
      formula.fParams[i].second = i;
127
      Npar += 1;
128
    }
129
  }
130
 
131
  if(funList[1]){
132
    //pol function
133
    var n = parseInt(document.getElementById("polOrderDisplay"+sframe).value);
134
 
135
    for(var i=0; i<=n; ++i){
136
      if((i>0) || (Npar > 0)){
137
        formula.fFormula += " + ";
138
      }
139
 
140
      if(i==0){
141
        formula.fFormula += "[p" + String(i) + "]";
142
      }else{
143
        if(i==1){
144
          formula.fFormula += "[p" + String(i) + "] * x";
145
        }else{
146
          formula.fFormula += "[p" + String(i) + "] * x^" + String(i);
147
        }
148
      }
149
 
150
      formula.fClingParameters.push(param[3+i]);
151
      formula.fParams.push(JSROOT.Create("pair<TString,int,TFormulaParamOrder>"));
152
      formula.fParams[i+Npar].first = "p" + String(i);
153
      formula.fParams[i+Npar].second = i+Npar;
154
    }
155
    Npar += n+1;
156
  }
157
 
158
  if(funList[2]){
159
    var expParameters = ["N_{exp}", "K"]
160
    if((Npar > 0)){
161
      formula.fFormula += " + ";
162
    }
163
    formula.fFormula += "[N_{exp}] * exp([K] * x)";
164
    formula.fClingParameters.push(param[8]);
165
    formula.fClingParameters.push(param[9]);
166
 
167
    for(var i = 0; i<2;i++){
168
      formula.fParams.push(JSROOT.Create("pair<TString,int,TFormulaParamOrder>"));
169
      formula.fParams[i+Npar].first = expParameters[i];
170
      formula.fParams[i+Npar].second = i+Npar;
171
    }
172
    Npar += 2;
173
  }
174
 
175
  if(funList[3]){
176
    var BWParameters = ["N_{BW}", "#Gamma", "Mean_{BW}"]
177
    if((Npar > 0)){
178
      formula.fFormula += " + ";
179
    }
180
    formula.fFormula += "[N_{BW}] * TMath::BreitWigner(x, [Mean_{BW}], [#Gamma])"; // * [Gamma] / ((x - [MeanBW])^2 + ([Gamma]/2)^2)
181
    formula.fClingParameters.push(param[10]);
182
    formula.fClingParameters.push(param[11]);
183
    formula.fClingParameters.push(param[12]);
184
 
185
    for(var i = 0; i<3;i++){
186
      formula.fParams.push(JSROOT.Create("pair<TString,int,TFormulaParamOrder>"));
187
      formula.fParams[i+Npar].first = BWParameters[i];
188
      formula.fParams[i+Npar].second = i+Npar;
189
    }
190
    Npar += 3;
191
  }
192
 
193
  formula.fName = "Fit function";
194
  //formula.fNdim = 1;
195
  formula.fTitle = "Fit function";
196
  formula.fNparam = Npar;
197
  //func.fParErrors = [1, 1, 1]; // to display parameters' error use this line
198
  func.fFormula = formula;
199
  func.fNpar = formula.fNparam;
200
  func.fTitle = formula.fTitle;
201
  func.fName = formula.fName;
202
  func.fLineColor = 2;
203
  func.fLineWidth = 2;
204
  return func
205
}