[root] / trunk / hist / hist / src / TMultiGraph.cxx Repository:
ViewVC logotype

Annotation of /trunk/hist/hist/src/TMultiGraph.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31491 - (view) (download) (as text)

1 : brun 24702 // @(#)root/hist:$Id$
2 : brun 754 // Author: Rene Brun 12/10/2000
3 :    
4 :     /*************************************************************************
5 :     * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 :     * All rights reserved. *
7 :     * *
8 :     * For the licensing terms see $ROOTSYS/LICENSE. *
9 :     * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 :     *************************************************************************/
11 :    
12 :     #include "TROOT.h"
13 :     #include "TMultiGraph.h"
14 :     #include "TGraph.h"
15 :     #include "TH1.h"
16 :     #include "TVirtualPad.h"
17 : rdm 3748 #include "Riostream.h"
18 : brun 11226 #include "TVirtualFitter.h"
19 : moneta 25487 #include "TPluginManager.h"
20 : pcanal 14336 #include "TClass.h"
21 : brun 17338 #include "TMath.h"
22 : moneta 25487 #include "TSystem.h"
23 : rdm 22419 #include <stdlib.h>
24 : brun 754
25 : moneta 31207 #include "HFitInterface.h"
26 :     #include "Fit/DataRange.h"
27 :     #include "Math/MinimizerOptions.h"
28 :    
29 : brun 754 #include <ctype.h>
30 :    
31 : brun 11226 extern void H1LeastSquareSeqnd(Int_t n, Double_t *a, Int_t idim, Int_t &ifail, Int_t k, Double_t *b);
32 : brun 754
33 :     ClassImp(TMultiGraph)
34 :    
35 : couet 13304
36 : brun 754 //______________________________________________________________________________
37 : couet 20297 /* Begin_Html
38 :     <center><h2>TMultiGraph class</h2></center>
39 :     A TMultiGraph is a collection of TGraph (or derived) objects
40 :     Use <tt>TMultiGraph::Add</tt> to add a new graph to the list.
41 :     The TMultiGraph owns the objects in the list.
42 :     Drawing options are the same as for TGraph.
43 :     <p>
44 :     Example:
45 : couet 20433 <pre>
46 : couet 20297 TGraph *gr1 = new TGraph(...
47 :     TGraphErrors *gr2 = new TGraphErrors(...
48 :     TMultiGraph *mg = new TMultiGraph();
49 :     mg->Add(gr1,"lp");
50 :     mg->Add(gr2,"cp");
51 :     mg->Draw("a");
52 :     </pre>
53 :     The drawing option for each TGraph may be specified as an optional
54 :     second argument of the Add function.
55 :     If a draw option is specified, it will be used to draw the graph,
56 :     otherwise the graph will be drawn with the option specified in
57 :     <tt>TMultiGraph::Draw</tt>.
58 :     End_Html */
59 : brun 754
60 : couet 13304
61 : brun 754 //______________________________________________________________________________
62 :     TMultiGraph::TMultiGraph(): TNamed()
63 :     {
64 : couet 13304 // TMultiGraph default constructor
65 : brun 754
66 :     fGraphs = 0;
67 : brun 11226 fFunctions = 0;
68 : brun 754 fHistogram = 0;
69 :     fMaximum = -1111;
70 :     fMinimum = -1111;
71 :     }
72 :    
73 : couet 13304
74 : brun 754 //______________________________________________________________________________
75 :     TMultiGraph::TMultiGraph(const char *name, const char *title)
76 :     : TNamed(name,title)
77 :     {
78 : couet 13304 // constructor with name and title
79 :    
80 : brun 754 fGraphs = 0;
81 : brun 11226 fFunctions = 0;
82 : brun 754 fHistogram = 0;
83 :     fMaximum = -1111;
84 :     fMinimum = -1111;
85 :     }
86 :    
87 : couet 20433
88 : brun 15134 //______________________________________________________________________________
89 :     TMultiGraph::TMultiGraph(const TMultiGraph& mg) :
90 :     TNamed (mg),
91 :     fGraphs(mg.fGraphs),
92 :     fFunctions(mg.fFunctions),
93 :     fHistogram(mg.fHistogram),
94 :     fMaximum(mg.fMaximum),
95 :     fMinimum(mg.fMinimum)
96 : couet 20433 {
97 : brun 15171 //copy constructor
98 :     }
99 : couet 13304
100 : couet 20433
101 : brun 754 //______________________________________________________________________________
102 : brun 15134 TMultiGraph& TMultiGraph::operator=(const TMultiGraph& mg)
103 :     {
104 : brun 15740 //assignement operator
105 : brun 15171 if(this!=&mg) {
106 :     TNamed::operator=(mg);
107 :     fGraphs=mg.fGraphs;
108 :     fFunctions=mg.fFunctions;
109 :     fHistogram=mg.fHistogram;
110 :     fMaximum=mg.fMaximum;
111 :     fMinimum=mg.fMinimum;
112 : couet 20433 }
113 : brun 15171 return *this;
114 : brun 15134 }
115 :    
116 : couet 20433
117 : brun 15134 //______________________________________________________________________________
118 : brun 754 TMultiGraph::~TMultiGraph()
119 :     {
120 : couet 13304 // TMultiGraph destructor
121 : brun 754
122 :     if (!fGraphs) return;
123 : brun 5552 TGraph *g;
124 :     TIter next(fGraphs);
125 :     while ((g = (TGraph*) next())) {
126 : couet 13304 g->ResetBit(kMustCleanup);
127 : brun 5552 }
128 : brun 754 fGraphs->Delete();
129 :     delete fGraphs;
130 :     fGraphs = 0;
131 :     delete fHistogram;
132 :     fHistogram = 0;
133 : brun 11226 if (fFunctions) {
134 :     fFunctions->SetBit(kInvalidObject);
135 :     //special logic to support the case where the same object is
136 :     //added multiple times in fFunctions.
137 :     //This case happens when the same object is added with different
138 :     //drawing modes
139 :     TObject *obj;
140 :     while ((obj = fFunctions->First())) {
141 : rdm 22546 while(fFunctions->Remove(obj)) { }
142 : brun 11226 delete obj;
143 :     }
144 :     delete fFunctions;
145 :     }
146 : brun 754 }
147 :    
148 : couet 13304
149 : brun 754 //______________________________________________________________________________
150 : brun 4037 void TMultiGraph::Add(TGraph *graph, Option_t *chopt)
151 : brun 754 {
152 :     // add a new graph to the list of graphs
153 : brun 6639 // note that the graph is now owned by the TMultigraph.
154 :     // Deleting the TMultiGraph object will automatically delete the graphs.
155 :     // You should not delete the graphs when the TMultigraph is still active.
156 : rdm 3742
157 : brun 754 if (!fGraphs) fGraphs = new TList();
158 : brun 5552 graph->SetBit(kMustCleanup);
159 : brun 4037 fGraphs->Add(graph,chopt);
160 : brun 754 }
161 :    
162 : couet 13304
163 : brun 754 //______________________________________________________________________________
164 : couet 16436 void TMultiGraph::Add(TMultiGraph *multigraph, Option_t *chopt)
165 :     {
166 :     // add all the graphs in "multigraph" to the list of graphs.
167 :    
168 :     TList *graphlist = multigraph->GetListOfGraphs();
169 :     if (!graphlist) return;
170 :    
171 :     if (!fGraphs) fGraphs = new TList();
172 : couet 20433
173 : couet 16436 TGraph *gr;
174 :     gr = (TGraph*)graphlist->First();
175 :     fGraphs->Add(gr,chopt);
176 : couet 22379 for(Int_t i = 1; i < graphlist->GetSize(); i++){
177 : couet 16436 gr = (TGraph*)graphlist->After(gr);
178 :     fGraphs->Add(gr,chopt);
179 :     }
180 :     }
181 :    
182 :    
183 :     //______________________________________________________________________________
184 : brun 754 void TMultiGraph::Browse(TBrowser *)
185 :     {
186 : couet 13304 // Browse multigraph.
187 :    
188 :     Draw("alp");
189 :     gPad->Update();
190 : brun 754 }
191 :    
192 : couet 13304
193 : brun 754 //______________________________________________________________________________
194 :     Int_t TMultiGraph::DistancetoPrimitive(Int_t px, Int_t py)
195 :     {
196 : couet 13304 // Compute distance from point px,py to each graph
197 : brun 754
198 : couet 13304 // Are we on the axis?
199 : brun 754 const Int_t kMaxDiff = 10;
200 :     Int_t distance = 9999;
201 :     if (fHistogram) {
202 :     distance = fHistogram->DistancetoPrimitive(px,py);
203 :     if (distance <= 0) return distance;
204 :     }
205 :    
206 : couet 13304 // Loop on the list of graphs
207 : brun 754 if (!fGraphs) return distance;
208 :     TGraph *g;
209 :     TIter next(fGraphs);
210 :     while ((g = (TGraph*) next())) {
211 :     Int_t dist = g->DistancetoPrimitive(px,py);
212 : brun 4915 if (dist <= 0) return 0;
213 : brun 754 if (dist < kMaxDiff) {gPad->SetSelected(g); return dist;}
214 :     }
215 :     return distance;
216 :     }
217 :    
218 : couet 13304
219 : brun 754 //______________________________________________________________________________
220 :     void TMultiGraph::Draw(Option_t *option)
221 :     {
222 : couet 13304 // Draw this multigraph with its current attributes.
223 :     //
224 :     // Options to draw a graph are described in TGraph::PainGraph
225 :     //
226 :     // The drawing option for each TGraph may be specified as an optional
227 :     // second argument of the Add function. You can use GetGraphDrawOption
228 :     // to return this option.
229 :     // If a draw option is specified, it will be used to draw the graph,
230 :     // otherwise the graph will be drawn with the option specified in
231 :     // TMultiGraph::Draw. Use GetDrawOption to return the option specified
232 :     // when drawin the TMultiGraph.
233 : brun 754
234 : couet 26029 if (!fGraphs) {
235 :     Error("Draw", "Cannot draw an empty TMultiGraph");
236 :     return;
237 :     }
238 : couet 13304 AppendPad(option);
239 : brun 754 }
240 :    
241 : couet 13304
242 : brun 754 //______________________________________________________________________________
243 : moneta 31491 TFitResultPtr TMultiGraph::Fit(const char *fname, Option_t *option, Option_t *, Axis_t xmin, Axis_t xmax)
244 : brun 11226 {
245 : couet 13304 // Fit this graph with function with name fname.
246 :     //
247 :     // interface to TF1::Fit(TF1 *f1...
248 : brun 11226
249 :     char *linear;
250 : brun 11231 linear= (char*)strstr(fname, "++");
251 : brun 11226 TF1 *f1=0;
252 :     if (linear)
253 : brun 12643 f1=new TF1(fname, fname, xmin, xmax);
254 : brun 11226 else {
255 :     f1 = (TF1*)gROOT->GetFunction(fname);
256 :     if (!f1) { Printf("Unknown function: %s",fname); return -1; }
257 :     }
258 :    
259 :     return Fit(f1,option,"",xmin,xmax);
260 :     }
261 :    
262 : couet 13304
263 : brun 11226 //______________________________________________________________________________
264 : moneta 31491 TFitResultPtr TMultiGraph::Fit(TF1 *f1, Option_t *option, Option_t *goption, Axis_t rxmin, Axis_t rxmax)
265 : brun 11226 {
266 : couet 13304 // Fit this multigraph with function f1.
267 :     //
268 :     // In this function all graphs of the multigraph are fitted simultaneously
269 :     //
270 :     // f1 is an already predefined function created by TF1.
271 :     // Predefined functions such as gaus, expo and poln are automatically
272 :     // created by ROOT.
273 :     //
274 :     // The list of fit options is given in parameter option.
275 :     // option = "W" Set all errors to 1
276 :     // = "U" Use a User specified fitting algorithm (via SetFCN)
277 :     // = "Q" Quiet mode (minimum printing)
278 :     // = "V" Verbose mode (default is between Q and V)
279 :     // = "B" Use this option when you want to fix one or more parameters
280 :     // and the fitting function is like "gaus","expo","poln","landau".
281 :     // = "R" Use the Range specified in the function range
282 :     // = "N" Do not store the graphics function, do not draw
283 :     // = "0" Do not plot the result of the fit. By default the fitted function
284 :     // is drawn unless the option"N" above is specified.
285 :     // = "+" Add this new fitted function to the list of fitted functions
286 :     // (by default, any previous function is deleted)
287 :     // = "C" In case of linear fitting, not calculate the chisquare
288 :     // (saves time)
289 :     // = "F" If fitting a polN, switch to minuit fitter
290 :     // = "ROB" In case of linear fitting, compute the LTS regression
291 : couet 20433 // coefficients (robust(resistant) regression), using
292 : couet 13304 // the default fraction of good points
293 :     // "ROB=0.x" - compute the LTS regression coefficients, using
294 :     // 0.x as a fraction of good points
295 :     //
296 :     // When the fit is drawn (by default), the parameter goption may be used
297 :     // to specify a list of graphics options. See TGraph::Paint for a complete
298 :     // list of these options.
299 :     //
300 :     // In order to use the Range option, one must first create a function
301 :     // with the expression to be fitted. For example, if your graph
302 :     // has a defined range between -4 and 4 and you want to fit a gaussian
303 :     // only in the interval 1 to 3, you can do:
304 :     // TF1 *f1 = new TF1("f1","gaus",1,3);
305 :     // graph->Fit("f1","R");
306 :     //
307 :     //
308 :     // who is calling this function
309 :     // ============================
310 :     // Note that this function is called when calling TGraphErrors::Fit
311 :     // or TGraphAsymmErrors::Fit ot TGraphBentErrors::Fit
312 :     // see the discussion below on the errors calulation.
313 :     //
314 :     // Setting initial conditions
315 :     // ==========================
316 :     // Parameters must be initialized before invoking the Fit function.
317 :     // The setting of the parameter initial values is automatic for the
318 :     // predefined functions : poln, expo, gaus, landau. One can however disable
319 :     // this automatic computation by specifying the option "B".
320 :     // You can specify boundary limits for some or all parameters via
321 :     // f1->SetParLimits(p_number, parmin, parmax);
322 :     // if parmin>=parmax, the parameter is fixed
323 :     // Note that you are not forced to fix the limits for all parameters.
324 :     // For example, if you fit a function with 6 parameters, you can do:
325 :     // func->SetParameters(0,3.1,1.e-6,0.1,-8,100);
326 :     // func->SetParLimits(4,-10,-4);
327 :     // func->SetParLimits(5, 1,1);
328 :     // With this setup, parameters 0->3 can vary freely
329 :     // Parameter 4 has boundaries [-10,-4] with initial value -8
330 :     // Parameter 5 is fixed to 100.
331 :     //
332 :     // Fit range
333 :     // =========
334 :     // The fit range can be specified in two ways:
335 :     // - specify rxmax > rxmin (default is rxmin=rxmax=0)
336 :     // - specify the option "R". In this case, the function will be taken
337 :     // instead of the full graph range.
338 :     //
339 :     // Changing the fitting function
340 :     // =============================
341 :     // By default the fitting function GraphFitChisquare is used.
342 :     // To specify a User defined fitting function, specify option "U" and
343 :     // call the following functions:
344 :     // TVirtualFitter::Fitter(mygraph)->SetFCN(MyFittingFunction)
345 :     // where MyFittingFunction is of type:
346 :     // extern void MyFittingFunction(Int_t &npar, Double_t *gin, Double_t &f, Double_t *u, Int_t flag);
347 :     //
348 :     // How errors are used in the chisquare function (see TFitter GraphFitChisquare)// Access to the fit results
349 :     // ============================================
350 :     // In case of a TGraphErrors object, ex, the error along x, is projected
351 :     // along the y-direction by calculating the function at the points x-exlow and
352 :     // x+exhigh.
353 :     //
354 :     // The chisquare is computed as the sum of the quantity below at each point:
355 :     //
356 :     // (y - f(x))**2
357 :     // -----------------------------------
358 :     // ey**2 + ((f(x+exhigh) - f(x-exlow))/2)**2
359 :     //
360 :     // where x and y are the point coordinates.
361 :     //
362 :     // In case the function lies below (above) the data point, ey is ey_low (ey_high).
363 :     //
364 :     // thanks to Andy Haas (haas@yahoo.com) for adding the case with TGraphasymmerrors
365 :     // University of Washington
366 :     //
367 :     // a little different approach to approximating the uncertainty in y because of the
368 :     // errors in x, is to make it equal the error in x times the slope of the line.
369 :     // The improvement, compared to the first method (f(x+ exhigh) - f(x-exlow))/2
370 :     // is of (error of x)**2 order. This approach is called "effective variance method".
371 :     // This improvement has been made in version 4.00/08 by Anna Kreshuk.
372 :     //
373 :     // Associated functions
374 :     // ====================
375 :     // One or more object (typically a TF1*) can be added to the list
376 :     // of functions (fFunctions) associated to each graph.
377 :     // When TGraph::Fit is invoked, the fitted function is added to this list.
378 :     // Given a graph gr, one can retrieve an associated function
379 :     // with: TF1 *myfunc = gr->GetFunction("myfunc");
380 :     //
381 :     // If the graph is made persistent, the list of
382 :     // associated functions is also persistent. Given a pointer (see above)
383 :     // to an associated function myfunc, one can retrieve the function/fit
384 :     // parameters with calls such as:
385 :     // Double_t chi2 = myfunc->GetChisquare();
386 :     // Double_t par0 = myfunc->GetParameter(0); //value of 1st parameter
387 :     // Double_t err0 = myfunc->GetParError(0); //error on first parameter
388 :     //
389 :     // Fit Statistics
390 :     // ==============
391 :     // You can change the statistics box to display the fit parameters with
392 :     // the TStyle::SetOptFit(mode) method. This mode has four digits.
393 :     // mode = pcev (default = 0111)
394 :     // v = 1; print name/values of parameters
395 :     // e = 1; print errors (if e=1, v must be 1)
396 :     // c = 1; print Chisquare/Number of degress of freedom
397 :     // p = 1; print Probability
398 :     //
399 :     // For example: gStyle->SetOptFit(1011);
400 :     // prints the fit probability, parameter names/values, and errors.
401 :     // You can change the position of the statistics box with these lines
402 :     // (where g is a pointer to the TGraph):
403 :     //
404 :     // Root > TPaveStats *st = (TPaveStats*)g->GetListOfFunctions()->FindObject("stats")
405 :     // Root > st->SetX1NDC(newx1); //new x start position
406 :     // Root > st->SetX2NDC(newx2); //new x end position
407 : brun 11226
408 : moneta 31207 // internal multigraph fitting methods
409 :     Foption_t fitOption;
410 :     ROOT::Fit::FitOptionsMake(option,fitOption);
411 : rdm 11750
412 : moneta 31207 // create range and minimizer options with default values
413 :     ROOT::Fit::DataRange range(rxmin,rxmax);
414 :     ROOT::Math::MinimizerOptions minOption;
415 :     return ROOT::Fit::FitObject(this, f1 , fitOption , minOption, goption, range);
416 :    
417 : moneta 25487 }
418 : rdm 11750
419 : moneta 25487 //______________________________________________________________________________
420 :     void TMultiGraph::FitPanel()
421 :     {
422 :     // -*-*-*-*-*Display a panel with all histogram fit options*-*-*-*-*-*
423 :     // ==============================================
424 :     //
425 :     // See class TFitPanel for example
426 : rdm 11750
427 : moneta 25487 if (!gPad)
428 :     gROOT->MakeDefCanvas();
429 : brun 11226
430 : moneta 25487 if (!gPad) {
431 :     Error("FitPanel", "Unable to create a default canvas");
432 :     return;
433 : brun 12136 }
434 : brun 11226
435 : moneta 25487 // use plugin manager to create instance of TFitEditor
436 :     TPluginHandler *handler = gROOT->GetPluginManager()->FindHandler("TFitEditor");
437 :     if (handler && handler->LoadPlugin() != -1) {
438 :     if (handler->ExecPlugin(2, gPad, this) == 0)
439 :     Error("FitPanel", "Unable to crate the FitPanel");
440 : brun 11226 }
441 : couet 26029 else
442 : moneta 25487 Error("FitPanel", "Unable to find the FitPanel plug-in");
443 : brun 11226 }
444 :    
445 :     //______________________________________________________________________________
446 : brun 11584 Option_t *TMultiGraph::GetGraphDrawOption(const TGraph *gr) const
447 :     {
448 : couet 13304 // Return the draw option for the TGraph gr in this TMultiGraph
449 :     // The return option is the one specified when calling TMultiGraph::Add(gr,option).
450 : rdm 11750
451 : brun 11584 if (!fGraphs || !gr) return "";
452 :     TListIter next(fGraphs);
453 :     TObject *obj;
454 :     while ((obj = next())) {
455 : brun 11600 if (obj == (TObject*)gr) return next.GetOption();
456 : brun 11584 }
457 :     return "";
458 :     }
459 :    
460 : couet 13304
461 : brun 11584 //______________________________________________________________________________
462 : brun 11226 void TMultiGraph::InitGaus(Double_t xmin, Double_t xmax)
463 :     {
464 : couet 13304 // Compute Initial values of parameters for a gaussian.
465 : brun 11226
466 :     Double_t allcha, sumx, sumx2, x, val, rms, mean;
467 :     Int_t bin;
468 :     const Double_t sqrtpi = 2.506628;
469 :    
470 : couet 13304 // Compute mean value and RMS of the graph in the given range
471 : brun 11226 Int_t np = 0;
472 :     allcha = sumx = sumx2 = 0;
473 :     TGraph *g;
474 :     TIter next(fGraphs);
475 :     Double_t *px, *py;
476 :     Int_t npp; //number of points in each graph
477 :     while ((g = (TGraph*) next())) {
478 :     px=g->GetX();
479 :     py=g->GetY();
480 :     npp=g->GetN();
481 :     for (bin=0; bin<npp; bin++){
482 : brun 12643 x=px[bin];
483 :     if (x<xmin || x>xmax) continue;
484 :     np++;
485 :     val=py[bin];
486 :     sumx+=val*x;
487 :     sumx2+=val*x*x;
488 :     allcha+=val;
489 : brun 11226 }
490 :     }
491 :     if (np == 0 || allcha == 0) return;
492 :     mean = sumx/allcha;
493 :     rms = TMath::Sqrt(sumx2/allcha - mean*mean);
494 :    
495 :     Double_t binwidx = TMath::Abs((xmax-xmin)/np);
496 :     if (rms == 0) rms = 1;
497 :     TVirtualFitter *grFitter = TVirtualFitter::GetFitter();
498 :     TF1 *f1 = (TF1*)grFitter->GetUserFunc();
499 :     f1->SetParameter(0,binwidx*allcha/(sqrtpi*rms));
500 :     f1->SetParameter(1,mean);
501 :     f1->SetParameter(2,rms);
502 :     f1->SetParLimits(2,0,10*rms);
503 :     }
504 :    
505 : couet 13304
506 : brun 11226 //______________________________________________________________________________
507 :     void TMultiGraph::InitExpo(Double_t xmin, Double_t xmax)
508 :     {
509 : couet 13304 // Compute Initial values of parameters for an exponential.
510 : brun 11226
511 :     Double_t constant, slope;
512 :     Int_t ifail;
513 :    
514 :     LeastSquareLinearFit(-1, constant, slope, ifail, xmin, xmax);
515 :    
516 :     TVirtualFitter *grFitter = TVirtualFitter::GetFitter();
517 :     TF1 *f1 = (TF1*)grFitter->GetUserFunc();
518 :     f1->SetParameter(0,constant);
519 :     f1->SetParameter(1,slope);
520 :     }
521 :    
522 : couet 13304
523 : brun 11226 //______________________________________________________________________________
524 :     void TMultiGraph::InitPolynom(Double_t xmin, Double_t xmax)
525 :     {
526 : couet 13304 // Compute Initial values of parameters for a polynom.
527 : brun 11226
528 :     Double_t fitpar[25];
529 :    
530 :     TVirtualFitter *grFitter = TVirtualFitter::GetFitter();
531 :     TF1 *f1 = (TF1*)grFitter->GetUserFunc();
532 :     Int_t npar = f1->GetNpar();
533 :    
534 :     LeastSquareFit(npar, fitpar, xmin, xmax);
535 :    
536 :     for (Int_t i=0;i<npar;i++) f1->SetParameter(i, fitpar[i]);
537 :     }
538 :    
539 : couet 13304
540 : brun 11226 //______________________________________________________________________________
541 :     void TMultiGraph::LeastSquareFit(Int_t m, Double_t *a, Double_t xmin, Double_t xmax)
542 :     {
543 : couet 13304 // Least squares lpolynomial fitting without weights.
544 :     //
545 :     // m number of parameters
546 :     // a array of parameters
547 :     // first 1st point number to fit (default =0)
548 :     // last last point number to fit (default=fNpoints-1)
549 :     //
550 :     // based on CERNLIB routine LSQ: Translated to C++ by Rene Brun
551 : brun 11226
552 : couet 13304 const Double_t zero = 0.;
553 :     const Double_t one = 1.;
554 :     const Int_t idim = 20;
555 : brun 11226
556 : couet 13304 Double_t b[400] /* was [20][20] */;
557 :     Int_t i, k, l, ifail, bin;
558 :     Double_t power;
559 :     Double_t da[20], xk, yk;
560 : brun 11226
561 :    
562 : couet 13304 //count the total number of points to fit
563 :     TGraph *g;
564 :     TIter next(fGraphs);
565 :     Double_t *px, *py;
566 :     Int_t n=0;
567 :     Int_t npp;
568 :     while ((g = (TGraph*) next())) {
569 :     px=g->GetX();
570 :     py=g->GetY();
571 :     npp=g->GetN();
572 :     for (bin=0; bin<npp; bin++){
573 :     xk=px[bin];
574 :     if (xk < xmin || xk > xmax) continue;
575 :     n++;
576 :     }
577 :     }
578 :     if (m <= 2) {
579 :     LeastSquareLinearFit(n, a[0], a[1], ifail, xmin, xmax);
580 :     return;
581 :     }
582 :     if (m > idim || m > n) return;
583 :     da[0] = zero;
584 :     for (l = 2; l <= m; ++l) {
585 :     b[l-1] = zero;
586 :     b[m + l*20 - 21] = zero;
587 :     da[l-1] = zero;
588 :     }
589 :     Int_t np = 0;
590 : brun 11226
591 : couet 13304 next.Reset();
592 :     while ((g = (TGraph*) next())) {
593 :     px=g->GetX();
594 :     py=g->GetY();
595 :     npp=g->GetN();
596 :    
597 :     for (k = 0; k <= npp; ++k) {
598 :     xk = px[k];
599 :     if (xk < xmin || xk > xmax) continue;
600 :     np++;
601 :     yk = py[k];
602 :     power = one;
603 :     da[0] += yk;
604 :     for (l = 2; l <= m; ++l) {
605 :     power *= xk;
606 : brun 12643 b[l-1] += power;
607 :     da[l-1] += power*yk;
608 : couet 13304 }
609 :     for (l = 2; l <= m; ++l) {
610 :     power *= xk;
611 :     b[m + l*20 - 21] += power;
612 :     }
613 :     }
614 :     }
615 :     b[0] = Double_t(np);
616 :     for (i = 3; i <= m; ++i) {
617 :     for (k = i; k <= m; ++k) {
618 :     b[k - 1 + (i-1)*20 - 21] = b[k + (i-2)*20 - 21];
619 :     }
620 :     }
621 :     H1LeastSquareSeqnd(m, b, idim, ifail, 1, da);
622 : brun 11226
623 : couet 13304 if (ifail < 0) {
624 :     //a[0] = fY[0];
625 :     py=((TGraph *)fGraphs->First())->GetY();
626 :     a[0]=py[0];
627 :     for (i=1; i<m; ++i) a[i] = 0;
628 :     return;
629 :     }
630 :     for (i=0; i<m; ++i) a[i] = da[i];
631 : brun 11226 }
632 :    
633 : couet 13304
634 : brun 11226 //______________________________________________________________________________
635 :     void TMultiGraph::LeastSquareLinearFit(Int_t ndata, Double_t &a0, Double_t &a1, Int_t &ifail, Double_t xmin, Double_t xmax)
636 :     {
637 : couet 13304 // Least square linear fit without weights.
638 :     //
639 :     // Fit a straight line (a0 + a1*x) to the data in this graph.
640 :     // ndata: number of points to fit
641 :     // first: first point number to fit
642 :     // last: last point to fit O(ndata should be last-first
643 :     // ifail: return parameter indicating the status of the fit (ifail=0, fit is OK)
644 :     //
645 :     // extracted from CERNLIB LLSQ: Translated to C++ by Rene Brun
646 : brun 11226
647 : couet 13304 Double_t xbar, ybar, x2bar;
648 :     Int_t i;
649 :     Double_t xybar;
650 :     Double_t fn, xk, yk;
651 :     Double_t det;
652 : brun 11226
653 : couet 13304 ifail = -2;
654 :     xbar = ybar = x2bar = xybar = 0;
655 :     Int_t np = 0;
656 :     TGraph *g;
657 :     TIter next(fGraphs);
658 :     Double_t *px, *py;
659 :     Int_t npp;
660 :     while ((g = (TGraph*) next())) {
661 :     px=g->GetX();
662 :     py=g->GetY();
663 :     npp=g->GetN();
664 :     for (i = 0; i < npp; ++i) {
665 :     xk = px[i];
666 :     if (xk < xmin || xk > xmax) continue;
667 :     np++;
668 :     yk = py[i];
669 :     if (ndata < 0) {
670 :     if (yk <= 0) yk = 1e-9;
671 :     yk = TMath::Log(yk);
672 :     }
673 :     xbar += xk;
674 :     ybar += yk;
675 :     x2bar += xk*xk;
676 :     xybar += xk*yk;
677 :     }
678 :     }
679 :     fn = Double_t(np);
680 :     det = fn*x2bar - xbar*xbar;
681 :     ifail = -1;
682 :     if (det <= 0) {
683 :     if (fn > 0) a0 = ybar/fn;
684 :     else a0 = 0;
685 :     a1 = 0;
686 :     return;
687 :     }
688 :     ifail = 0;
689 :     a0 = (x2bar*ybar - xbar*xybar) / det;
690 :     a1 = (fn*xybar - xbar*ybar) / det;
691 :     }
692 : brun 11226
693 :    
694 :     //______________________________________________________________________________
695 : brun 1205 TH1F *TMultiGraph::GetHistogram() const
696 : brun 754 {
697 : couet 13304 // Returns a pointer to the histogram used to draw the axis
698 :     // Takes into account the two following cases.
699 :     // 1- option 'A' was specified in TMultiGraph::Draw. Return fHistogram
700 :     // 2- user had called TPad::DrawFrame. return pointer to hframe histogram
701 : brun 754
702 :     if (fHistogram) return fHistogram;
703 :     if (!gPad) return 0;
704 :     gPad->Modified();
705 :     gPad->Update();
706 :     if (fHistogram) return fHistogram;
707 :     TH1F *h1 = (TH1F*)gPad->FindObject("hframe");
708 :     return h1;
709 :     }
710 :    
711 : couet 13304
712 : brun 754 //______________________________________________________________________________
713 : brun 11226 TF1 *TMultiGraph::GetFunction(const char *name) const
714 :     {
715 : couet 13304 // Return pointer to function with name.
716 :     //
717 :     // Functions such as TGraph::Fit store the fitted function in the list of
718 :     // functions of this graph.
719 : brun 11226
720 :     if (!fFunctions) return 0;
721 :     return (TF1*)fFunctions->FindObject(name);
722 :     }
723 :    
724 : moneta 25487 //______________________________________________________________________________
725 : couet 26029 TList *TMultiGraph::GetListOfFunctions()
726 : moneta 25487 {
727 :     // Return pointer to list of functions
728 :     // if pointer is null create the list
729 : couet 13304
730 : moneta 25487 if (!fFunctions) fFunctions = new TList();
731 : couet 26029 return fFunctions;
732 : moneta 25487 }
733 :    
734 :    
735 : brun 11226 //______________________________________________________________________________
736 : brun 1205 TAxis *TMultiGraph::GetXaxis() const
737 : brun 754 {
738 :     // Get x axis of the graph.
739 :    
740 :     if (!gPad) return 0;
741 : brun 4083 TH1 *h = GetHistogram();
742 :     if (!h) return 0;
743 :     return h->GetXaxis();
744 : brun 754 }
745 :    
746 : couet 13304
747 : brun 754 //______________________________________________________________________________
748 : brun 1205 TAxis *TMultiGraph::GetYaxis() const
749 : brun 754 {
750 :     // Get y axis of the graph.
751 :    
752 :     if (!gPad) return 0;
753 : brun 4083 TH1 *h = GetHistogram();
754 :     if (!h) return 0;
755 :     return h->GetYaxis();
756 : brun 754 }
757 :    
758 : couet 13304
759 : brun 754 //______________________________________________________________________________
760 :     void TMultiGraph::Paint(Option_t *option)
761 :     {
762 : couet 13304 // paint all the graphs of this multigraph
763 : brun 754
764 : couet 13304 if (fGraphs->GetSize() == 0) return;
765 : rdm 11750
766 : couet 13304 char *l;
767 :     static char chopt[33];
768 :     Int_t nch = strlen(option);
769 :     Int_t i;
770 :     for (i=0;i<nch;i++) chopt[i] = toupper(option[i]);
771 :     chopt[nch] = 0;
772 :     TGraph *g;
773 : rdm 3742
774 : couet 13304 l = strstr(chopt,"A");
775 :     if (l) {
776 :     *l = ' ';
777 :     TIter next(fGraphs);
778 :     Int_t npt = 100;
779 :     Double_t maximum, minimum, rwxmin, rwxmax, rwymin, rwymax, uxmin, uxmax, dx, dy;
780 :     rwxmin = gPad->GetUxmin();
781 :     rwxmax = gPad->GetUxmax();
782 :     rwymin = gPad->GetUymin();
783 :     rwymax = gPad->GetUymax();
784 :     char *xtitle = 0;
785 :     char *ytitle = 0;
786 :     Int_t firstx = 0;
787 :     Int_t lastx = 0;
788 : rdm 11750
789 : couet 13304 if (fHistogram) {
790 :     //cleanup in case of a previous unzoom
791 :     if (fHistogram->GetMinimum() >= fHistogram->GetMaximum()) {
792 : brun 23506 nch = strlen(fHistogram->GetXaxis()->GetTitle());
793 : couet 13304 firstx = fHistogram->GetXaxis()->GetFirst();
794 :     lastx = fHistogram->GetXaxis()->GetLast();
795 :     if (nch) {
796 :     xtitle = new char[nch+1];
797 :     strcpy(xtitle,fHistogram->GetXaxis()->GetTitle());
798 :     }
799 :     nch = strlen(fHistogram->GetYaxis()->GetTitle());
800 :     if (nch) {
801 :     ytitle = new char[nch+1];
802 :     strcpy(ytitle,fHistogram->GetYaxis()->GetTitle());
803 :     }
804 :     delete fHistogram;
805 :     fHistogram = 0;
806 :     }
807 :     }
808 :     if (fHistogram) {
809 :     minimum = fHistogram->GetYaxis()->GetXmin();
810 :     maximum = fHistogram->GetYaxis()->GetXmax();
811 :     uxmin = gPad->PadtoX(rwxmin);
812 :     uxmax = gPad->PadtoX(rwxmax);
813 :     } else {
814 : couet 21573 g = (TGraph*) next();
815 :     g->ComputeRange(rwxmin, rwymin, rwxmax, rwymax);
816 : couet 13304 while ((g = (TGraph*) next())) {
817 : couet 20433 Double_t rx1,ry1,rx2,ry2;
818 :     g->ComputeRange(rx1, ry1, rx2, ry2);
819 :     if (rx1 < rwxmin) rwxmin = rx1;
820 :     if (ry1 < rwymin) rwymin = ry1;
821 :     if (rx2 > rwxmax) rwxmax = rx2;
822 :     if (ry2 > rwymax) rwymax = ry2;
823 : couet 13304 if (g->GetN() > npt) npt = g->GetN();
824 :     }
825 :     if (rwxmin == rwxmax) rwxmax += 1.;
826 :     if (rwymin == rwymax) rwymax += 1.;
827 :     dx = 0.05*(rwxmax-rwxmin);
828 :     dy = 0.05*(rwymax-rwymin);
829 :     uxmin = rwxmin - dx;
830 :     uxmax = rwxmax + dx;
831 :     if (gPad->GetLogy()) {
832 :     if (rwymin <= 0) rwymin = 0.001*rwymax;
833 :     minimum = rwymin/(1+0.5*TMath::Log10(rwymax/rwymin));
834 :     maximum = rwymax*(1+0.2*TMath::Log10(rwymax/rwymin));
835 :     } else {
836 :     minimum = rwymin - dy;
837 :     maximum = rwymax + dy;
838 :     }
839 :     if (minimum < 0 && rwymin >= 0) minimum = 0;
840 :     if (maximum > 0 && rwymax <= 0) maximum = 0;
841 :     }
842 : brun 754
843 : couet 13304 if (fMinimum != -1111) rwymin = minimum = fMinimum;
844 :     if (fMaximum != -1111) rwymax = maximum = fMaximum;
845 :     if (uxmin < 0 && rwxmin >= 0) {
846 :     if (gPad->GetLogx()) uxmin = 0.9*rwxmin;
847 :     //else uxmin = 0;
848 :     }
849 :     if (uxmax > 0 && rwxmax <= 0) {
850 :     if (gPad->GetLogx()) uxmax = 1.1*rwxmax;
851 :     //else uxmax = 0;
852 :     }
853 :     if (minimum < 0 && rwymin >= 0) {
854 :     if(gPad->GetLogy()) minimum = 0.9*rwymin;
855 :     //else minimum = 0;
856 :     }
857 :     if (maximum > 0 && rwymax <= 0) {
858 :     if(gPad->GetLogy()) maximum = 1.1*rwymax;
859 :     //else maximum = 0;
860 :     }
861 :     if (minimum <= 0 && gPad->GetLogy()) minimum = 0.001*maximum;
862 :     if (uxmin <= 0 && gPad->GetLogx()) {
863 :     if (uxmax > 1000) uxmin = 1;
864 :     else uxmin = 0.001*uxmax;
865 :     }
866 :     rwymin = minimum;
867 :     rwymax = maximum;
868 :     if (fHistogram) {
869 :     fHistogram->GetYaxis()->SetLimits(rwymin,rwymax);
870 :     }
871 : brun 3575
872 : couet 13304 // Create a temporary histogram to draw the axis
873 :     if (!fHistogram) {
874 :     // the graph is created with at least as many channels as there are points
875 :     // to permit zooming on the full range
876 :     rwxmin = uxmin;
877 :     rwxmax = uxmax;
878 :     fHistogram = new TH1F(GetName(),GetTitle(),npt,rwxmin,rwxmax);
879 :     if (!fHistogram) return;
880 :     fHistogram->SetMinimum(rwymin);
881 :     fHistogram->SetBit(TH1::kNoStats);
882 :     fHistogram->SetMaximum(rwymax);
883 :     fHistogram->GetYaxis()->SetLimits(rwymin,rwymax);
884 :     fHistogram->SetDirectory(0);
885 :     if (xtitle) {fHistogram->GetXaxis()->SetTitle(xtitle); delete [] xtitle;}
886 :     if (ytitle) {fHistogram->GetYaxis()->SetTitle(ytitle); delete [] ytitle;}
887 :     if (firstx != lastx) fHistogram->GetXaxis()->SetRange(firstx,lastx);
888 :     }
889 :     fHistogram->Paint("0");
890 : brun 3575 }
891 : rdm 3742
892 : couet 21962 TGraph *gfit = 0;
893 : brun 754 if (fGraphs) {
894 : brun 4037 TObjOptLink *lnk = (TObjOptLink*)fGraphs->FirstLink();
895 : couet 21962 TObject *obj = 0;
896 : brun 4037
897 :     while (lnk) {
898 :     obj = lnk->GetObject();
899 :     if (strlen(lnk->GetOption())) obj->Paint(lnk->GetOption());
900 :     else obj->Paint(chopt);
901 :     lnk = (TObjOptLink*)lnk->Next();
902 :     }
903 : couet 21962 gfit = (TGraph*)obj; // pick one TGraph in the list to paint the fit parameters.
904 : brun 754 }
905 : brun 11226
906 :     TObject *f;
907 : couet 21962 TF1 *fit = 0;
908 : brun 11226 if (fFunctions) {
909 : couet 13304 TIter next(fFunctions);
910 :     while ((f = (TObject*) next())) {
911 :     if (f->InheritsFrom(TF1::Class())) {
912 :     if (f->TestBit(TF1::kNotDraw) == 0) f->Paint("lsame");
913 : couet 21962 fit = (TF1*)f;
914 : couet 13304 } else {
915 :     f->Paint();
916 :     }
917 : brun 11226 }
918 :     }
919 : couet 21962
920 : brun 24084 if (fit) gfit->PaintStats(fit);
921 : couet 13304 }
922 : brun 11226
923 :    
924 : brun 754 //______________________________________________________________________________
925 : brun 1205 void TMultiGraph::Print(Option_t *option) const
926 : brun 754 {
927 : couet 13304 // Print the list of graphs
928 : brun 754
929 :     TGraph *g;
930 :     if (fGraphs) {
931 : couet 13304 TIter next(fGraphs);
932 :     while ((g = (TGraph*) next())) {
933 :     g->Print(option);
934 :     }
935 : brun 754 }
936 :     }
937 :    
938 : couet 13304
939 : brun 754 //______________________________________________________________________________
940 : brun 5552 void TMultiGraph::RecursiveRemove(TObject *obj)
941 :     {
942 :     // Recursively remove this object from a list. Typically implemented
943 :     // by classes that can contain mulitple references to a same object.
944 :    
945 :     if (!fGraphs) return;
946 :     TObject *objr = fGraphs->Remove(obj);
947 :     if (!objr) return;
948 :     delete fHistogram; fHistogram = 0;
949 :     if (gPad) gPad->Modified();
950 :     }
951 :    
952 : couet 13304
953 : brun 5552 //______________________________________________________________________________
954 : brun 15672 void TMultiGraph::SavePrimitive(ostream &out, Option_t *option /*= ""*/)
955 : brun 754 {
956 : couet 13304 // Save primitive as a C++ statement(s) on output stream out
957 : brun 754
958 :     char quote = '"';
959 :     out<<" "<<endl;
960 :     if (gROOT->ClassSaved(TMultiGraph::Class())) {
961 : couet 13304 out<<" ";
962 : brun 754 } else {
963 : couet 13304 out<<" TMultiGraph *";
964 : brun 754 }
965 :     out<<"multigraph = new TMultiGraph();"<<endl;
966 :     out<<" multigraph->SetName("<<quote<<GetName()<<quote<<");"<<endl;
967 :     out<<" multigraph->SetTitle("<<quote<<GetTitle()<<quote<<");"<<endl;
968 :    
969 :     if (fGraphs) {
970 : brun 10847 TObjOptLink *lnk = (TObjOptLink*)fGraphs->FirstLink();
971 :     TObject *g;
972 :    
973 :     while (lnk) {
974 :     g = lnk->GetObject();
975 : couet 20297 g->SavePrimitive(out, Form("multigraph%s",lnk->GetOption()));
976 : brun 10847 lnk = (TObjOptLink*)lnk->Next();
977 : couet 13304 }
978 : brun 754 }
979 : couet 20297 out<<" multigraph->Draw(" <<quote<<option<<quote<<");"<<endl;
980 : couet 14017
981 :     TAxis *xaxis = GetXaxis();
982 :     TAxis *yaxis = GetYaxis();
983 : couet 20433
984 :     if (xaxis) xaxis->SaveAttributes(out, "multigraph","->GetXaxis()");
985 :     if (yaxis) yaxis->SaveAttributes(out, "multigraph","->GetYaxis()");
986 : brun 754 }
987 :    
988 : couet 13304
989 : brun 754 //______________________________________________________________________________
990 :     void TMultiGraph::SetMaximum(Double_t maximum)
991 :     {
992 : couet 13304 // Set multigraph maximum.
993 :    
994 : brun 754 fMaximum = maximum;
995 :     if (fHistogram) fHistogram->SetMaximum(maximum);
996 :     }
997 :    
998 : couet 13304
999 : brun 754 //______________________________________________________________________________
1000 :     void TMultiGraph::SetMinimum(Double_t minimum)
1001 :     {
1002 : couet 13304 // Set multigraph minimum.
1003 :    
1004 : brun 754 fMinimum = minimum;
1005 :     if (fHistogram) fHistogram->SetMinimum(minimum);
1006 :     }

Subversion Admin
ViewVC Help
Powered by ViewVC 1.0.9