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

Subversion Admin
ViewVC Help
Powered by ViewVC 1.0.9