[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 34699 - (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 13304 AppendPad(option);
235 : brun 754 }
236 :    
237 : couet 13304
238 : brun 754 //______________________________________________________________________________
239 : moneta 31491 TFitResultPtr TMultiGraph::Fit(const char *fname, Option_t *option, Option_t *, Axis_t xmin, Axis_t xmax)
240 : brun 11226 {
241 : couet 13304 // Fit this graph with function with name fname.
242 :     //
243 :     // interface to TF1::Fit(TF1 *f1...
244 : brun 11226
245 :     char *linear;
246 : brun 11231 linear= (char*)strstr(fname, "++");
247 : brun 11226 TF1 *f1=0;
248 :     if (linear)
249 : brun 12643 f1=new TF1(fname, fname, xmin, xmax);
250 : brun 11226 else {
251 :     f1 = (TF1*)gROOT->GetFunction(fname);
252 :     if (!f1) { Printf("Unknown function: %s",fname); return -1; }
253 :     }
254 :    
255 :     return Fit(f1,option,"",xmin,xmax);
256 :     }
257 :    
258 : couet 13304
259 : brun 11226 //______________________________________________________________________________
260 : moneta 31491 TFitResultPtr TMultiGraph::Fit(TF1 *f1, Option_t *option, Option_t *goption, Axis_t rxmin, Axis_t rxmax)
261 : brun 11226 {
262 : couet 13304 // Fit this multigraph with function f1.
263 :     //
264 :     // In this function all graphs of the multigraph are fitted simultaneously
265 :     //
266 :     // f1 is an already predefined function created by TF1.
267 :     // Predefined functions such as gaus, expo and poln are automatically
268 :     // created by ROOT.
269 :     //
270 :     // The list of fit options is given in parameter option.
271 :     // option = "W" Set all errors to 1
272 :     // = "U" Use a User specified fitting algorithm (via SetFCN)
273 :     // = "Q" Quiet mode (minimum printing)
274 :     // = "V" Verbose mode (default is between Q and V)
275 :     // = "B" Use this option when you want to fix one or more parameters
276 :     // and the fitting function is like "gaus","expo","poln","landau".
277 :     // = "R" Use the Range specified in the function range
278 :     // = "N" Do not store the graphics function, do not draw
279 :     // = "0" Do not plot the result of the fit. By default the fitted function
280 :     // is drawn unless the option"N" above is specified.
281 :     // = "+" Add this new fitted function to the list of fitted functions
282 :     // (by default, any previous function is deleted)
283 :     // = "C" In case of linear fitting, not calculate the chisquare
284 :     // (saves time)
285 :     // = "F" If fitting a polN, switch to minuit fitter
286 :     // = "ROB" In case of linear fitting, compute the LTS regression
287 : couet 20433 // coefficients (robust(resistant) regression), using
288 : couet 13304 // the default fraction of good points
289 :     // "ROB=0.x" - compute the LTS regression coefficients, using
290 :     // 0.x as a fraction of good points
291 :     //
292 :     // When the fit is drawn (by default), the parameter goption may be used
293 :     // to specify a list of graphics options. See TGraph::Paint for a complete
294 :     // list of these options.
295 :     //
296 :     // In order to use the Range option, one must first create a function
297 :     // with the expression to be fitted. For example, if your graph
298 :     // has a defined range between -4 and 4 and you want to fit a gaussian
299 :     // only in the interval 1 to 3, you can do:
300 :     // TF1 *f1 = new TF1("f1","gaus",1,3);
301 :     // graph->Fit("f1","R");
302 :     //
303 :     //
304 :     // who is calling this function
305 :     // ============================
306 :     // Note that this function is called when calling TGraphErrors::Fit
307 :     // or TGraphAsymmErrors::Fit ot TGraphBentErrors::Fit
308 :     // see the discussion below on the errors calulation.
309 :     //
310 :     // Setting initial conditions
311 :     // ==========================
312 :     // Parameters must be initialized before invoking the Fit function.
313 :     // The setting of the parameter initial values is automatic for the
314 :     // predefined functions : poln, expo, gaus, landau. One can however disable
315 :     // this automatic computation by specifying the option "B".
316 :     // You can specify boundary limits for some or all parameters via
317 :     // f1->SetParLimits(p_number, parmin, parmax);
318 :     // if parmin>=parmax, the parameter is fixed
319 :     // Note that you are not forced to fix the limits for all parameters.
320 :     // For example, if you fit a function with 6 parameters, you can do:
321 :     // func->SetParameters(0,3.1,1.e-6,0.1,-8,100);
322 :     // func->SetParLimits(4,-10,-4);
323 :     // func->SetParLimits(5, 1,1);
324 :     // With this setup, parameters 0->3 can vary freely
325 :     // Parameter 4 has boundaries [-10,-4] with initial value -8
326 :     // Parameter 5 is fixed to 100.
327 :     //
328 :     // Fit range
329 :     // =========
330 :     // The fit range can be specified in two ways:
331 :     // - specify rxmax > rxmin (default is rxmin=rxmax=0)
332 :     // - specify the option "R". In this case, the function will be taken
333 :     // instead of the full graph range.
334 :     //
335 : moneta 34699 // Changing the fitting function
336 :     // =============================
337 :     // By default a chi2 fitting function is used for fitting the TGraphs's.
338 :     // The function is implemented in FitUtil::EvaluateChi2.
339 :     // In case of TGraphErrors an effective chi2 is used
340 :     // (see TGraphErrors fit in TGraph::Fit) and is implemented in
341 :     // FitUtil::EvaluateChi2Effective
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 : couet 13304 //
348 : moneta 34699 // Access to the fit result
349 :     // ========================
350 :     // The function returns a TFitResultPtr which can hold a pointer to a TFitResult object.
351 :     // By default the TFitResultPtr contains only the status of the fit and it converts
352 :     // automatically to an integer. If the option "S" is instead used, TFitResultPtr contains
353 :     // the TFitResult and behaves as a smart pointer to it. For example one can do:
354 :     // TFitResultPtr r = graph->Fit("myFunc","S");
355 :     // TMatrixDSym cov = r->GetCovarianceMatrix(); // to access the covariance matrix
356 :     // Double_t par0 = r->Parameter(0); // retrieve the value for the parameter 0
357 :     // Double_t err0 = r->ParError(0); // retrieve the error for the parameter 0
358 :     // r->Print("V"); // print full information of fit including covariance matrix
359 :     // r->Write(); // store the result in a file
360 : couet 13304 //
361 : moneta 34699 // The fit parameters, error and chi2 (but not covariance matrix) can be retrieved also
362 :     // from the fitted function.
363 : couet 13304 //
364 :     //
365 :     // Associated functions
366 :     // ====================
367 :     // One or more object (typically a TF1*) can be added to the list
368 :     // of functions (fFunctions) associated to each graph.
369 :     // When TGraph::Fit is invoked, the fitted function is added to this list.
370 :     // Given a graph gr, one can retrieve an associated function
371 :     // with: TF1 *myfunc = gr->GetFunction("myfunc");
372 :     //
373 :     // If the graph is made persistent, the list of
374 :     // associated functions is also persistent. Given a pointer (see above)
375 :     // to an associated function myfunc, one can retrieve the function/fit
376 :     // parameters with calls such as:
377 :     // Double_t chi2 = myfunc->GetChisquare();
378 :     // Double_t par0 = myfunc->GetParameter(0); //value of 1st parameter
379 :     // Double_t err0 = myfunc->GetParError(0); //error on first parameter
380 :     //
381 :     // Fit Statistics
382 :     // ==============
383 :     // You can change the statistics box to display the fit parameters with
384 :     // the TStyle::SetOptFit(mode) method. This mode has four digits.
385 :     // mode = pcev (default = 0111)
386 :     // v = 1; print name/values of parameters
387 :     // e = 1; print errors (if e=1, v must be 1)
388 :     // c = 1; print Chisquare/Number of degress of freedom
389 :     // p = 1; print Probability
390 :     //
391 :     // For example: gStyle->SetOptFit(1011);
392 :     // prints the fit probability, parameter names/values, and errors.
393 :     // You can change the position of the statistics box with these lines
394 :     // (where g is a pointer to the TGraph):
395 :     //
396 :     // Root > TPaveStats *st = (TPaveStats*)g->GetListOfFunctions()->FindObject("stats")
397 :     // Root > st->SetX1NDC(newx1); //new x start position
398 :     // Root > st->SetX2NDC(newx2); //new x end position
399 : brun 11226
400 : moneta 31207 // internal multigraph fitting methods
401 :     Foption_t fitOption;
402 :     ROOT::Fit::FitOptionsMake(option,fitOption);
403 : rdm 11750
404 : moneta 31207 // create range and minimizer options with default values
405 :     ROOT::Fit::DataRange range(rxmin,rxmax);
406 :     ROOT::Math::MinimizerOptions minOption;
407 :     return ROOT::Fit::FitObject(this, f1 , fitOption , minOption, goption, range);
408 :    
409 : moneta 25487 }
410 : rdm 11750
411 : moneta 25487 //______________________________________________________________________________
412 :     void TMultiGraph::FitPanel()
413 :     {
414 :     // -*-*-*-*-*Display a panel with all histogram fit options*-*-*-*-*-*
415 :     // ==============================================
416 :     //
417 :     // See class TFitPanel for example
418 : rdm 11750
419 : moneta 25487 if (!gPad)
420 :     gROOT->MakeDefCanvas();
421 : brun 11226
422 : moneta 25487 if (!gPad) {
423 :     Error("FitPanel", "Unable to create a default canvas");
424 :     return;
425 : brun 12136 }
426 : brun 11226
427 : moneta 25487 // use plugin manager to create instance of TFitEditor
428 :     TPluginHandler *handler = gROOT->GetPluginManager()->FindHandler("TFitEditor");
429 :     if (handler && handler->LoadPlugin() != -1) {
430 :     if (handler->ExecPlugin(2, gPad, this) == 0)
431 :     Error("FitPanel", "Unable to crate the FitPanel");
432 : brun 11226 }
433 : couet 26029 else
434 : moneta 25487 Error("FitPanel", "Unable to find the FitPanel plug-in");
435 : brun 11226 }
436 :    
437 :     //______________________________________________________________________________
438 : brun 11584 Option_t *TMultiGraph::GetGraphDrawOption(const TGraph *gr) const
439 :     {
440 : couet 13304 // Return the draw option for the TGraph gr in this TMultiGraph
441 :     // The return option is the one specified when calling TMultiGraph::Add(gr,option).
442 : rdm 11750
443 : brun 11584 if (!fGraphs || !gr) return "";
444 :     TListIter next(fGraphs);
445 :     TObject *obj;
446 :     while ((obj = next())) {
447 : brun 11600 if (obj == (TObject*)gr) return next.GetOption();
448 : brun 11584 }
449 :     return "";
450 :     }
451 :    
452 : couet 13304
453 : brun 11584 //______________________________________________________________________________
454 : brun 11226 void TMultiGraph::InitGaus(Double_t xmin, Double_t xmax)
455 :     {
456 : couet 13304 // Compute Initial values of parameters for a gaussian.
457 : brun 11226
458 :     Double_t allcha, sumx, sumx2, x, val, rms, mean;
459 :     Int_t bin;
460 :     const Double_t sqrtpi = 2.506628;
461 :    
462 : couet 13304 // Compute mean value and RMS of the graph in the given range
463 : brun 11226 Int_t np = 0;
464 :     allcha = sumx = sumx2 = 0;
465 :     TGraph *g;
466 :     TIter next(fGraphs);
467 :     Double_t *px, *py;
468 :     Int_t npp; //number of points in each graph
469 :     while ((g = (TGraph*) next())) {
470 :     px=g->GetX();
471 :     py=g->GetY();
472 :     npp=g->GetN();
473 :     for (bin=0; bin<npp; bin++){
474 : brun 12643 x=px[bin];
475 :     if (x<xmin || x>xmax) continue;
476 :     np++;
477 :     val=py[bin];
478 :     sumx+=val*x;
479 :     sumx2+=val*x*x;
480 :     allcha+=val;
481 : brun 11226 }
482 :     }
483 :     if (np == 0 || allcha == 0) return;
484 :     mean = sumx/allcha;
485 :     rms = TMath::Sqrt(sumx2/allcha - mean*mean);
486 :    
487 :     Double_t binwidx = TMath::Abs((xmax-xmin)/np);
488 :     if (rms == 0) rms = 1;
489 :     TVirtualFitter *grFitter = TVirtualFitter::GetFitter();
490 :     TF1 *f1 = (TF1*)grFitter->GetUserFunc();
491 :     f1->SetParameter(0,binwidx*allcha/(sqrtpi*rms));
492 :     f1->SetParameter(1,mean);
493 :     f1->SetParameter(2,rms);
494 :     f1->SetParLimits(2,0,10*rms);
495 :     }
496 :    
497 : couet 13304
498 : brun 11226 //______________________________________________________________________________
499 :     void TMultiGraph::InitExpo(Double_t xmin, Double_t xmax)
500 :     {
501 : couet 13304 // Compute Initial values of parameters for an exponential.
502 : brun 11226
503 :     Double_t constant, slope;
504 :     Int_t ifail;
505 :    
506 :     LeastSquareLinearFit(-1, constant, slope, ifail, xmin, xmax);
507 :    
508 :     TVirtualFitter *grFitter = TVirtualFitter::GetFitter();
509 :     TF1 *f1 = (TF1*)grFitter->GetUserFunc();
510 :     f1->SetParameter(0,constant);
511 :     f1->SetParameter(1,slope);
512 :     }
513 :    
514 : couet 13304
515 : brun 11226 //______________________________________________________________________________
516 :     void TMultiGraph::InitPolynom(Double_t xmin, Double_t xmax)
517 :     {
518 : couet 13304 // Compute Initial values of parameters for a polynom.
519 : brun 11226
520 :     Double_t fitpar[25];
521 :    
522 :     TVirtualFitter *grFitter = TVirtualFitter::GetFitter();
523 :     TF1 *f1 = (TF1*)grFitter->GetUserFunc();
524 :     Int_t npar = f1->GetNpar();
525 :    
526 :     LeastSquareFit(npar, fitpar, xmin, xmax);
527 :    
528 :     for (Int_t i=0;i<npar;i++) f1->SetParameter(i, fitpar[i]);
529 :     }
530 :    
531 : couet 13304
532 : brun 11226 //______________________________________________________________________________
533 :     void TMultiGraph::LeastSquareFit(Int_t m, Double_t *a, Double_t xmin, Double_t xmax)
534 :     {
535 : couet 13304 // Least squares lpolynomial fitting without weights.
536 :     //
537 :     // m number of parameters
538 :     // a array of parameters
539 :     // first 1st point number to fit (default =0)
540 :     // last last point number to fit (default=fNpoints-1)
541 :     //
542 :     // based on CERNLIB routine LSQ: Translated to C++ by Rene Brun
543 : brun 11226
544 : couet 13304 const Double_t zero = 0.;
545 :     const Double_t one = 1.;
546 :     const Int_t idim = 20;
547 : brun 11226
548 : couet 13304 Double_t b[400] /* was [20][20] */;
549 :     Int_t i, k, l, ifail, bin;
550 :     Double_t power;
551 :     Double_t da[20], xk, yk;
552 : brun 11226
553 :    
554 : couet 13304 //count the total number of points to fit
555 :     TGraph *g;
556 :     TIter next(fGraphs);
557 :     Double_t *px, *py;
558 :     Int_t n=0;
559 :     Int_t npp;
560 :     while ((g = (TGraph*) next())) {
561 :     px=g->GetX();
562 :     py=g->GetY();
563 :     npp=g->GetN();
564 :     for (bin=0; bin<npp; bin++){
565 :     xk=px[bin];
566 :     if (xk < xmin || xk > xmax) continue;
567 :     n++;
568 :     }
569 :     }
570 :     if (m <= 2) {
571 :     LeastSquareLinearFit(n, a[0], a[1], ifail, xmin, xmax);
572 :     return;
573 :     }
574 :     if (m > idim || m > n) return;
575 :     da[0] = zero;
576 :     for (l = 2; l <= m; ++l) {
577 :     b[l-1] = zero;
578 :     b[m + l*20 - 21] = zero;
579 :     da[l-1] = zero;
580 :     }
581 :     Int_t np = 0;
582 : brun 11226
583 : couet 13304 next.Reset();
584 :     while ((g = (TGraph*) next())) {
585 :     px=g->GetX();
586 :     py=g->GetY();
587 :     npp=g->GetN();
588 :    
589 :     for (k = 0; k <= npp; ++k) {
590 :     xk = px[k];
591 :     if (xk < xmin || xk > xmax) continue;
592 :     np++;
593 :     yk = py[k];
594 :     power = one;
595 :     da[0] += yk;
596 :     for (l = 2; l <= m; ++l) {
597 :     power *= xk;
598 : brun 12643 b[l-1] += power;
599 :     da[l-1] += power*yk;
600 : couet 13304 }
601 :     for (l = 2; l <= m; ++l) {
602 :     power *= xk;
603 :     b[m + l*20 - 21] += power;
604 :     }
605 :     }
606 :     }
607 :     b[0] = Double_t(np);
608 :     for (i = 3; i <= m; ++i) {
609 :     for (k = i; k <= m; ++k) {
610 :     b[k - 1 + (i-1)*20 - 21] = b[k + (i-2)*20 - 21];
611 :     }
612 :     }
613 :     H1LeastSquareSeqnd(m, b, idim, ifail, 1, da);
614 : brun 11226
615 : couet 13304 if (ifail < 0) {
616 :     //a[0] = fY[0];
617 :     py=((TGraph *)fGraphs->First())->GetY();
618 :     a[0]=py[0];
619 :     for (i=1; i<m; ++i) a[i] = 0;
620 :     return;
621 :     }
622 :     for (i=0; i<m; ++i) a[i] = da[i];
623 : brun 11226 }
624 :    
625 : couet 13304
626 : brun 11226 //______________________________________________________________________________
627 :     void TMultiGraph::LeastSquareLinearFit(Int_t ndata, Double_t &a0, Double_t &a1, Int_t &ifail, Double_t xmin, Double_t xmax)
628 :     {
629 : couet 13304 // Least square linear fit without weights.
630 :     //
631 :     // Fit a straight line (a0 + a1*x) to the data in this graph.
632 :     // ndata: number of points to fit
633 :     // first: first point number to fit
634 :     // last: last point to fit O(ndata should be last-first
635 :     // ifail: return parameter indicating the status of the fit (ifail=0, fit is OK)
636 :     //
637 :     // extracted from CERNLIB LLSQ: Translated to C++ by Rene Brun
638 : brun 11226
639 : couet 13304 Double_t xbar, ybar, x2bar;
640 :     Int_t i;
641 :     Double_t xybar;
642 :     Double_t fn, xk, yk;
643 :     Double_t det;
644 : brun 11226
645 : couet 13304 ifail = -2;
646 :     xbar = ybar = x2bar = xybar = 0;
647 :     Int_t np = 0;
648 :     TGraph *g;
649 :     TIter next(fGraphs);
650 :     Double_t *px, *py;
651 :     Int_t npp;
652 :     while ((g = (TGraph*) next())) {
653 :     px=g->GetX();
654 :     py=g->GetY();
655 :     npp=g->GetN();
656 :     for (i = 0; i < npp; ++i) {
657 :     xk = px[i];
658 :     if (xk < xmin || xk > xmax) continue;
659 :     np++;
660 :     yk = py[i];
661 :     if (ndata < 0) {
662 :     if (yk <= 0) yk = 1e-9;
663 :     yk = TMath::Log(yk);
664 :     }
665 :     xbar += xk;
666 :     ybar += yk;
667 :     x2bar += xk*xk;
668 :     xybar += xk*yk;
669 :     }
670 :     }
671 :     fn = Double_t(np);
672 :     det = fn*x2bar - xbar*xbar;
673 :     ifail = -1;
674 :     if (det <= 0) {
675 :     if (fn > 0) a0 = ybar/fn;
676 :     else a0 = 0;
677 :     a1 = 0;
678 :     return;
679 :     }
680 :     ifail = 0;
681 :     a0 = (x2bar*ybar - xbar*xybar) / det;
682 :     a1 = (fn*xybar - xbar*ybar) / det;
683 :     }
684 : brun 11226
685 :    
686 :     //______________________________________________________________________________
687 : brun 1205 TH1F *TMultiGraph::GetHistogram() const
688 : brun 754 {
689 : couet 13304 // Returns a pointer to the histogram used to draw the axis
690 :     // Takes into account the two following cases.
691 :     // 1- option 'A' was specified in TMultiGraph::Draw. Return fHistogram
692 :     // 2- user had called TPad::DrawFrame. return pointer to hframe histogram
693 : brun 754
694 :     if (fHistogram) return fHistogram;
695 :     if (!gPad) return 0;
696 :     gPad->Modified();
697 :     gPad->Update();
698 :     if (fHistogram) return fHistogram;
699 :     TH1F *h1 = (TH1F*)gPad->FindObject("hframe");
700 :     return h1;
701 :     }
702 :    
703 : couet 13304
704 : brun 754 //______________________________________________________________________________
705 : brun 11226 TF1 *TMultiGraph::GetFunction(const char *name) const
706 :     {
707 : couet 13304 // Return pointer to function with name.
708 :     //
709 :     // Functions such as TGraph::Fit store the fitted function in the list of
710 :     // functions of this graph.
711 : brun 11226
712 :     if (!fFunctions) return 0;
713 :     return (TF1*)fFunctions->FindObject(name);
714 :     }
715 :    
716 : moneta 25487 //______________________________________________________________________________
717 : couet 26029 TList *TMultiGraph::GetListOfFunctions()
718 : moneta 25487 {
719 :     // Return pointer to list of functions
720 :     // if pointer is null create the list
721 : couet 13304
722 : moneta 25487 if (!fFunctions) fFunctions = new TList();
723 : couet 26029 return fFunctions;
724 : moneta 25487 }
725 :    
726 :    
727 : brun 11226 //______________________________________________________________________________
728 : brun 1205 TAxis *TMultiGraph::GetXaxis() const
729 : brun 754 {
730 :     // Get x axis of the graph.
731 :    
732 :     if (!gPad) return 0;
733 : brun 4083 TH1 *h = GetHistogram();
734 :     if (!h) return 0;
735 :     return h->GetXaxis();
736 : brun 754 }
737 :    
738 : couet 13304
739 : brun 754 //______________________________________________________________________________
740 : brun 1205 TAxis *TMultiGraph::GetYaxis() const
741 : brun 754 {
742 :     // Get y axis of the graph.
743 :    
744 :     if (!gPad) return 0;
745 : brun 4083 TH1 *h = GetHistogram();
746 :     if (!h) return 0;
747 :     return h->GetYaxis();
748 : brun 754 }
749 :    
750 : couet 13304
751 : brun 754 //______________________________________________________________________________
752 :     void TMultiGraph::Paint(Option_t *option)
753 :     {
754 : couet 13304 // paint all the graphs of this multigraph
755 : brun 754
756 : couet 33513 if (!fGraphs) return;
757 : couet 13304 if (fGraphs->GetSize() == 0) return;
758 : rdm 11750
759 : couet 13304 char *l;
760 :     static char chopt[33];
761 :     Int_t nch = strlen(option);
762 :     Int_t i;
763 :     for (i=0;i<nch;i++) chopt[i] = toupper(option[i]);
764 :     chopt[nch] = 0;
765 :     TGraph *g;
766 : rdm 3742
767 : couet 13304 l = strstr(chopt,"A");
768 :     if (l) {
769 :     *l = ' ';
770 :     TIter next(fGraphs);
771 :     Int_t npt = 100;
772 :     Double_t maximum, minimum, rwxmin, rwxmax, rwymin, rwymax, uxmin, uxmax, dx, dy;
773 :     rwxmin = gPad->GetUxmin();
774 :     rwxmax = gPad->GetUxmax();
775 :     rwymin = gPad->GetUymin();
776 :     rwymax = gPad->GetUymax();
777 :     char *xtitle = 0;
778 :     char *ytitle = 0;
779 :     Int_t firstx = 0;
780 :     Int_t lastx = 0;
781 : rdm 11750
782 : couet 13304 if (fHistogram) {
783 :     //cleanup in case of a previous unzoom
784 :     if (fHistogram->GetMinimum() >= fHistogram->GetMaximum()) {
785 : brun 23506 nch = strlen(fHistogram->GetXaxis()->GetTitle());
786 : couet 13304 firstx = fHistogram->GetXaxis()->GetFirst();
787 :     lastx = fHistogram->GetXaxis()->GetLast();
788 :     if (nch) {
789 :     xtitle = new char[nch+1];
790 :     strcpy(xtitle,fHistogram->GetXaxis()->GetTitle());
791 :     }
792 :     nch = strlen(fHistogram->GetYaxis()->GetTitle());
793 :     if (nch) {
794 :     ytitle = new char[nch+1];
795 :     strcpy(ytitle,fHistogram->GetYaxis()->GetTitle());
796 :     }
797 :     delete fHistogram;
798 :     fHistogram = 0;
799 :     }
800 :     }
801 :     if (fHistogram) {
802 :     minimum = fHistogram->GetYaxis()->GetXmin();
803 :     maximum = fHistogram->GetYaxis()->GetXmax();
804 :     uxmin = gPad->PadtoX(rwxmin);
805 :     uxmax = gPad->PadtoX(rwxmax);
806 :     } else {
807 : couet 21573 g = (TGraph*) next();
808 : brun 32499 if (g) g->ComputeRange(rwxmin, rwymin, rwxmax, rwymax);
809 : couet 13304 while ((g = (TGraph*) next())) {
810 : couet 20433 Double_t rx1,ry1,rx2,ry2;
811 :     g->ComputeRange(rx1, ry1, rx2, ry2);
812 :     if (rx1 < rwxmin) rwxmin = rx1;
813 :     if (ry1 < rwymin) rwymin = ry1;
814 :     if (rx2 > rwxmax) rwxmax = rx2;
815 :     if (ry2 > rwymax) rwymax = ry2;
816 : couet 13304 if (g->GetN() > npt) npt = g->GetN();
817 :     }
818 :     if (rwxmin == rwxmax) rwxmax += 1.;
819 :     if (rwymin == rwymax) rwymax += 1.;
820 :     dx = 0.05*(rwxmax-rwxmin);
821 :     dy = 0.05*(rwymax-rwymin);
822 :     uxmin = rwxmin - dx;
823 :     uxmax = rwxmax + dx;
824 :     if (gPad->GetLogy()) {
825 :     if (rwymin <= 0) rwymin = 0.001*rwymax;
826 :     minimum = rwymin/(1+0.5*TMath::Log10(rwymax/rwymin));
827 :     maximum = rwymax*(1+0.2*TMath::Log10(rwymax/rwymin));
828 :     } else {
829 :     minimum = rwymin - dy;
830 :     maximum = rwymax + dy;
831 :     }
832 :     if (minimum < 0 && rwymin >= 0) minimum = 0;
833 :     if (maximum > 0 && rwymax <= 0) maximum = 0;
834 :     }
835 : brun 754
836 : couet 13304 if (fMinimum != -1111) rwymin = minimum = fMinimum;
837 :     if (fMaximum != -1111) rwymax = maximum = fMaximum;
838 :     if (uxmin < 0 && rwxmin >= 0) {
839 :     if (gPad->GetLogx()) uxmin = 0.9*rwxmin;
840 :     //else uxmin = 0;
841 :     }
842 :     if (uxmax > 0 && rwxmax <= 0) {
843 :     if (gPad->GetLogx()) uxmax = 1.1*rwxmax;
844 :     //else uxmax = 0;
845 :     }
846 :     if (minimum < 0 && rwymin >= 0) {
847 :     if(gPad->GetLogy()) minimum = 0.9*rwymin;
848 :     //else minimum = 0;
849 :     }
850 :     if (maximum > 0 && rwymax <= 0) {
851 :     if(gPad->GetLogy()) maximum = 1.1*rwymax;
852 :     //else maximum = 0;
853 :     }
854 :     if (minimum <= 0 && gPad->GetLogy()) minimum = 0.001*maximum;
855 :     if (uxmin <= 0 && gPad->GetLogx()) {
856 :     if (uxmax > 1000) uxmin = 1;
857 :     else uxmin = 0.001*uxmax;
858 :     }
859 :     rwymin = minimum;
860 :     rwymax = maximum;
861 :     if (fHistogram) {
862 :     fHistogram->GetYaxis()->SetLimits(rwymin,rwymax);
863 :     }
864 : brun 3575
865 : couet 13304 // Create a temporary histogram to draw the axis
866 :     if (!fHistogram) {
867 :     // the graph is created with at least as many channels as there are points
868 :     // to permit zooming on the full range
869 :     rwxmin = uxmin;
870 :     rwxmax = uxmax;
871 :     fHistogram = new TH1F(GetName(),GetTitle(),npt,rwxmin,rwxmax);
872 :     if (!fHistogram) return;
873 :     fHistogram->SetMinimum(rwymin);
874 :     fHistogram->SetBit(TH1::kNoStats);
875 :     fHistogram->SetMaximum(rwymax);
876 :     fHistogram->GetYaxis()->SetLimits(rwymin,rwymax);
877 :     fHistogram->SetDirectory(0);
878 :     if (xtitle) {fHistogram->GetXaxis()->SetTitle(xtitle); delete [] xtitle;}
879 :     if (ytitle) {fHistogram->GetYaxis()->SetTitle(ytitle); delete [] ytitle;}
880 :     if (firstx != lastx) fHistogram->GetXaxis()->SetRange(firstx,lastx);
881 :     }
882 :     fHistogram->Paint("0");
883 : brun 3575 }
884 : rdm 3742
885 : couet 21962 TGraph *gfit = 0;
886 : brun 754 if (fGraphs) {
887 : brun 4037 TObjOptLink *lnk = (TObjOptLink*)fGraphs->FirstLink();
888 : couet 21962 TObject *obj = 0;
889 : brun 4037
890 :     while (lnk) {
891 :     obj = lnk->GetObject();
892 :     if (strlen(lnk->GetOption())) obj->Paint(lnk->GetOption());
893 :     else obj->Paint(chopt);
894 :     lnk = (TObjOptLink*)lnk->Next();
895 :     }
896 : couet 21962 gfit = (TGraph*)obj; // pick one TGraph in the list to paint the fit parameters.
897 : brun 754 }
898 : brun 11226
899 :     TObject *f;
900 : couet 21962 TF1 *fit = 0;
901 : brun 11226 if (fFunctions) {
902 : couet 13304 TIter next(fFunctions);
903 :     while ((f = (TObject*) next())) {
904 :     if (f->InheritsFrom(TF1::Class())) {
905 :     if (f->TestBit(TF1::kNotDraw) == 0) f->Paint("lsame");
906 : couet 21962 fit = (TF1*)f;
907 : couet 13304 } else {
908 :     f->Paint();
909 :     }
910 : brun 11226 }
911 :     }
912 : couet 21962
913 : brun 24084 if (fit) gfit->PaintStats(fit);
914 : couet 13304 }
915 : brun 11226
916 :    
917 : brun 754 //______________________________________________________________________________
918 : brun 1205 void TMultiGraph::Print(Option_t *option) const
919 : brun 754 {
920 : couet 13304 // Print the list of graphs
921 : brun 754
922 :     TGraph *g;
923 :     if (fGraphs) {
924 : couet 13304 TIter next(fGraphs);
925 :     while ((g = (TGraph*) next())) {
926 :     g->Print(option);
927 :     }
928 : brun 754 }
929 :     }
930 :    
931 : couet 13304
932 : brun 754 //______________________________________________________________________________
933 : brun 5552 void TMultiGraph::RecursiveRemove(TObject *obj)
934 :     {
935 :     // Recursively remove this object from a list. Typically implemented
936 :     // by classes that can contain mulitple references to a same object.
937 :    
938 :     if (!fGraphs) return;
939 :     TObject *objr = fGraphs->Remove(obj);
940 :     if (!objr) return;
941 :     delete fHistogram; fHistogram = 0;
942 :     if (gPad) gPad->Modified();
943 :     }
944 :    
945 : couet 13304
946 : brun 5552 //______________________________________________________________________________
947 : brun 15672 void TMultiGraph::SavePrimitive(ostream &out, Option_t *option /*= ""*/)
948 : brun 754 {
949 : couet 13304 // Save primitive as a C++ statement(s) on output stream out
950 : brun 754
951 :     char quote = '"';
952 :     out<<" "<<endl;
953 :     if (gROOT->ClassSaved(TMultiGraph::Class())) {
954 : couet 13304 out<<" ";
955 : brun 754 } else {
956 : couet 13304 out<<" TMultiGraph *";
957 : brun 754 }
958 :     out<<"multigraph = new TMultiGraph();"<<endl;
959 :     out<<" multigraph->SetName("<<quote<<GetName()<<quote<<");"<<endl;
960 :     out<<" multigraph->SetTitle("<<quote<<GetTitle()<<quote<<");"<<endl;
961 :    
962 :     if (fGraphs) {
963 : brun 10847 TObjOptLink *lnk = (TObjOptLink*)fGraphs->FirstLink();
964 :     TObject *g;
965 :    
966 :     while (lnk) {
967 :     g = lnk->GetObject();
968 : couet 20297 g->SavePrimitive(out, Form("multigraph%s",lnk->GetOption()));
969 : brun 10847 lnk = (TObjOptLink*)lnk->Next();
970 : couet 13304 }
971 : brun 754 }
972 : couet 20297 out<<" multigraph->Draw(" <<quote<<option<<quote<<");"<<endl;
973 : couet 14017
974 :     TAxis *xaxis = GetXaxis();
975 :     TAxis *yaxis = GetYaxis();
976 : couet 20433
977 :     if (xaxis) xaxis->SaveAttributes(out, "multigraph","->GetXaxis()");
978 :     if (yaxis) yaxis->SaveAttributes(out, "multigraph","->GetYaxis()");
979 : brun 754 }
980 :    
981 : couet 13304
982 : brun 754 //______________________________________________________________________________
983 :     void TMultiGraph::SetMaximum(Double_t maximum)
984 :     {
985 : couet 13304 // Set multigraph maximum.
986 :    
987 : brun 754 fMaximum = maximum;
988 :     if (fHistogram) fHistogram->SetMaximum(maximum);
989 :     }
990 :    
991 : couet 13304
992 : brun 754 //______________________________________________________________________________
993 :     void TMultiGraph::SetMinimum(Double_t minimum)
994 :     {
995 : couet 13304 // Set multigraph minimum.
996 :    
997 : brun 754 fMinimum = minimum;
998 :     if (fHistogram) fHistogram->SetMinimum(minimum);
999 :     }

Subversion Admin
ViewVC Help
Powered by ViewVC 1.0.9