Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TF1NormSum.cxx
Go to the documentation of this file.
1// @(#)root/hist:$Id$
2// Authors: Lorenzo Moneta, Aurélie Flandi 27/08/14
3//
4/**********************************************************************
5 * *
6 * Copyright (c) 2015 ROOT Team, CERN/PH-SFT *
7 * *
8 * *
9 **********************************************************************/
10
11#include "TROOT.h"
12#include "TClass.h"
13#include "TObjString.h"
14#include "TObjArray.h"
15#include "TF1NormSum.h"
17#include "Math/WrappedTF1.h"
18
20
21/** \class TF1NormSum
22 \ingroup Functions
23Class adding two functions: c1*f1+c2*f2
24*/
25
26////////////////////////////////////////////////////////////////////////////////
27/// Function to find and rename duplicate parameters with the same name
28
29template<class Iterator>
30void FixDuplicateNames(Iterator begin, Iterator end) {
31
32 // make a map of values
33
34 std::multimap<TString, int > parMap;
35 for (Iterator it = begin; it != end; ++it) {
36 parMap.insert( std::make_pair( *it, std::distance(begin,it) ) );
37 }
38 for ( auto & elem : parMap) {
39 TString name = elem.first;
40 int n = parMap.count( name);
41 if (n > 1 ) {
42 std::pair <std::multimap<TString,int>::iterator, std::multimap<TString,int>::iterator> ret;
43 ret = parMap.equal_range(name);
44 int i = 0;
45 for (std::multimap<TString,int>::iterator it=ret.first; it!=ret.second; ++it) {
46 *(begin+it->second) = TString::Format("%s%d",name.Data(),++i);
47 }
48 }
49 }
50
51}
52
53////////////////////////////////////////////////////////////////////////////////
54
55void TF1NormSum::InitializeDataMembers(const std::vector<TF1 *> &functions, const std::vector<Double_t> &coeffs,
56 Double_t scale)
57{
58
59 fScale = scale;
60 fCoeffs = coeffs;
61 fNOfFunctions = functions.size();
62 fCstIndexes = std::vector < Int_t > (fNOfFunctions);
63 fParNames = std::vector<TString> (fNOfFunctions);
64 fParNames.reserve(3*fNOfFunctions); // enlarge capacity for function parameters
65
66 // fill fFunctions with unique_ptr's
67 fFunctions = std::vector<std::unique_ptr<TF1>>(functions.size());
68 for (unsigned int n = 0; n < fNOfFunctions; n++) {
69 // use TF1::Copy and not clone to copy the TF1 pointers
70 // and use IsA()::New() in case we have base class pointers
71 TF1 * f = (TF1*) functions[n]->IsA()->New();
72 functions[n]->Copy(*f);
73 fFunctions[n] = std::unique_ptr<TF1>(f);
74
75
76 if (!fFunctions[n])
77 Fatal("InitializeDataMembers", "Invalid input function -- abort");
78
80 }
81
82 for (unsigned int n=0; n < fNOfFunctions; n++)
83 {
84 int npar = fFunctions[n] -> GetNpar();
85 fCstIndexes[n] = fFunctions[n] -> GetParNumber("Constant");//return -1 if there is no constant parameter
86 fParNames[n] = TString::Format("Coeff%d",n);
87 if (fCstIndexes[n]!= -1) //if there exists a constant parameter
88 {
89 fFunctions[n] -> FixParameter(fCstIndexes[n], 1.); // fixes the parameters called "Constant" to 1
90 for (int i=0; i<npar; i++) // go through all the parameter to
91 {
92 if (i==fCstIndexes[n]) continue; // go to next step if this is the constant parameter
93 fParNames.push_back( fFunctions[n] -> GetParName(i) );
94 }
95 }
96 else {
97 for (int i=0; i < npar; i++) //go through all the parameter to
98 {
99 fParNames.push_back( fFunctions[n] -> GetParName(i) );
100 }
101 }
102 //normalize the functions if it is not already done (do at the end so constant parameter is not zero)
103 if (!fFunctions[n] -> IsEvalNormalized()) fFunctions[n] -> SetNormalized(true);
104 }
105
106 // Set range
107 if (fNOfFunctions == 0) {
108 fXmin = 0.;
109 fXmax = 1.;
110 // Info("InitializeDataMembers", "Initializing empty TF1NormSum with default [0,1] range");
111 } else {
112 fFunctions[0]->GetRange(fXmin, fXmax);
113 if (fXmin >= fXmax) {
114 fXmin = 0.;
115 fXmax = 1.;
116 // Info("InitializeDataMembers", "Initializing empty TF1NormSum with default [0,1] range");
117 }
118 for (unsigned int n = 1; n < fNOfFunctions; n++) {
119 fFunctions[n]->SetRange(fXmin, fXmax);
120 fFunctions[n]->Update();
121 }
122 }
123
125}
126
127////////////////////////////////////////////////////////////////////////////////
128
130{
131 fNOfFunctions = 0;
132 fScale = 1.;
133 fFunctions = std::vector<std::unique_ptr<TF1>>(0); // Vector of size fNOfFunctions containing TF1 functions
134 fCoeffs = std::vector < Double_t >(0) ; // Vector of size fNOfFunctions containing coefficients in front of each function
135 fCstIndexes = std::vector < Int_t > (0);
136 fXmin = 0; // Dummy values of xmin and xmax
137 fXmax = 1;
138}
139
140////////////////////////////////////////////////////////////////////////////////
141
142TF1NormSum::TF1NormSum(const std::vector <TF1*> &functions, const std::vector <Double_t> &coeffs, Double_t scale)
143{
144 InitializeDataMembers(functions, coeffs, scale);
145}
146
147////////////////////////////////////////////////////////////////////////////////
148/// TF1NormSum constructor taking 2 functions, and 2 coefficients (if not equal to 1)
149
150TF1NormSum::TF1NormSum(TF1* function1, TF1* function2, Double_t coeff1, Double_t coeff2, Double_t scale)
151{
152 std::vector<TF1 *> functions(2);
153 std::vector < Double_t > coeffs(2);
154
155 functions = {function1, function2};
156 coeffs = {coeff1, coeff2};
157
158 InitializeDataMembers(functions, coeffs,scale);
159}
160
161////////////////////////////////////////////////////////////////////////////////
162/// TF1NormSum constructor taking 3 functions, and 3 coefficients (if not equal to 1)
163
164TF1NormSum::TF1NormSum(TF1* function1, TF1* function2, TF1* function3, Double_t coeff1, Double_t coeff2, Double_t coeff3, Double_t scale)
165{
166 std::vector<TF1 *> functions(3);
167 std::vector < Double_t > coeffs(3);
168
169 functions = {function1, function2, function3};
170 coeffs = {coeff1, coeff2, coeff3};
171
172 InitializeDataMembers(functions, coeffs,scale);
173}
174
175////////////////////////////////////////////////////////////////////////////////
176/// TF1NormSum constructor taking any addition of formulas with coefficient or not
177///
178/// - example 1 : 2.*expo + gauss + 0.5* gauss
179/// - example 2 : expo + 0.3*f1 if f1 is defined in the list of functions
180
182{
184
185 TObjArray *arrayall = formula.Tokenize("*+");
186 TObjArray *arraytimes = formula.Tokenize("*") ;
187 Int_t noffunctions = (formula.Tokenize("+")) -> GetEntries();
188 Int_t nofobj = arrayall -> GetEntries();
189 Int_t nofcoeffs = nofobj - noffunctions;
190
191 std::vector<TF1 *> functions(noffunctions);
192 std::vector < Double_t > coeffs(noffunctions);
193 std::vector < TString > funcstringall(nofobj);
194 std::vector < Int_t > indexsizetimes(nofcoeffs+1);
195 std::vector < Bool_t > isacoeff(nofobj);//1 is it is a coeff, 0 if it is a functions
196
197 for (int i=0; i<nofobj; i++)
198 {
199 funcstringall[i] = ((TObjString*)((*arrayall)[i])) -> GetString();
200 funcstringall[i].ReplaceAll(" ","");
201 }
202 //algorithm to determine which object is a coefficient and which is a function
203 //uses the fact that the last item of funcstringtimes[i].Tokenize("+") is always a coeff.
204 Int_t j = 0;
205 Int_t k = 1;
206 for (int i=0; i<nofcoeffs+1; i++)
207 {
208 indexsizetimes[i] = ( ( ( (TObjString*)(*arraytimes)[i] ) -> GetString() ).Tokenize("+") ) -> GetEntries();
209 while (k < indexsizetimes[i])
210 {
211 isacoeff[k+j-1] = false;
212 k++;
213 }
214 j = j+indexsizetimes[i];
215 if (j==nofobj) isacoeff[j-1] = false; //the last one is never a coeff
216 else isacoeff[j-1] = true;
217 k = 1;
218 }
219
220 Double_t old_xmin = 0.0, old_xmax = 0.0;
221 k = 0; // index of term in funcstringall
222 for (int i=0; i<noffunctions; i++)
223 {
224 // first, handle coefficient
225 if (isacoeff[k]) {
226 coeffs[i] = funcstringall[k].Atof();
227 k++;
228 } else {
229 coeffs[i] = 1.;
230 }
231
232 // then, handle function
233 functions[i] = (TF1 *)(gROOT->GetListOfFunctions()->FindObject(funcstringall[k]));
234 if (!functions[i])
235 Error("TF1NormSum", "Function %s does not exist", funcstringall[k].Data());
236 // (set range for first function, which determines range of whole TF1NormSum)
237 if (i == 0) {
238 functions[i]->GetRange(old_xmin, old_xmax);
239 functions[i]->SetRange(xmin, xmax);
240 }
241
242 k++;
243 }
244 InitializeDataMembers(functions, coeffs,1.);
245
246 // Set range of first function back to original state
247 if (noffunctions > 0 && functions[0])
248 functions[0]->SetRange(old_xmin, old_xmax);
249}
250
251////////////////////////////////////////////////////////////////////////////////
252/// Copy constructor (necessary to hold unique_ptr as member variable)
253
255{
256 nsum.TF1NormSum::Copy(*this);
257}
258
259////////////////////////////////////////////////////////////////////////////////
260/// Operator =
261
263{
264 if (this != &rhs)
265 rhs.TF1NormSum::Copy(*this);
266 return *this;
267}
268
269////////////////////////////////////////////////////////////////////////////////
270/// Overload the parenthesis to add the functions
271
273{
274 // first refresh the parameters
275 if (p != nullptr)
277
278 Double_t sum = 0.;
279 for (unsigned int n=0; n<fNOfFunctions; n++)
280 sum += fCoeffs[n]*(fFunctions[n] -> EvalPar(x,nullptr));
281
282 // normalize by a scale parameter (typically the bin width)
283 return fScale * sum;
284}
285
286////////////////////////////////////////////////////////////////////////////////
287/// Return array of parameters
288
289std::vector<double> TF1NormSum::GetParameters() const {
290 std::vector<double> params(GetNpar() );
291 int offset = 0;
292 int nOfNonCstParams = 0;
293 for (unsigned int n=0; n<fNOfFunctions; n++)
294 {
295 params[n] = fCoeffs[n]; // copy the coefficients
296 offset += nOfNonCstParams; // offset to go along the list of parameters
297 int k = 0;
298 for (int j = 0; j < fFunctions[n]->GetNpar(); ++j) {
299 if (j != fCstIndexes[n]) {
300 params[k+fNOfFunctions+offset] = fFunctions[n]->GetParameter(j);
301 k++;
302 }
303 }
304 nOfNonCstParams = k;
305 }
306 return params;
307}
308////////////////////////////////////////////////////////////////////////////////
309/// Initialize array of all parameters.
310///
311/// double *params must contains first an array of the coefficients, then an array of the parameters.
312
313void TF1NormSum::SetParameters(const Double_t *params) // params should have the size [fNOfFunctions][fNOfNonCstParams]
314{
315 for (unsigned int n=0; n<fNOfFunctions; n++) //initialization of the coefficients
316 {
317 fCoeffs[n] = params[n];
318 }
319 Int_t offset = 0;
320 int k = 0; // k indicates the number of non-constant parameter per function
321 for (unsigned int n=0; n<fNOfFunctions; n++)
322 {
323 bool equalParams = true;
324 Double_t * funcParams = fFunctions[n]->GetParameters();
325 int npar = fFunctions[n]->GetNpar();
326 offset += k; // offset to go along the list of parameters
327 k = 0; // reset k value for next function
328 for (int i = 0; i < npar; ++i) {
329 // constant parameters can be only one
330 if (i != fCstIndexes[n])
331 {
332 // check if they are equal
333 equalParams &= (funcParams[i] == params[k+fNOfFunctions+offset] );
334 funcParams[i] = params[k+fNOfFunctions+offset];
335 k++;
336 }
337 }
338 // update function integral if not equal
339 if (!equalParams) fFunctions[n]->Update();
340
341 }
342}
343
344////////////////////////////////////////////////////////////////////////////////
345/// Initialize array of all parameters.
346///
347/// Overload the TF1::SetParameters() method.
348/// A maximum of 10 parameters must be used, with first the coefficients, then the parameters
349
351 Double_t p5, Double_t p6, Double_t p7, Double_t p8, Double_t p9, Double_t p10)
352{
353 const double params[] = {p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10};
355
356}
357
358////////////////////////////////////////////////////////////////////////////////
359/// Return the number of (non constant) parameters including the coefficients: for 2 functions: c1,c2,p0,p1,p2,p3...
360
362{
363 Int_t nofparams = 0;
364 for (unsigned int n=0; n<fNOfFunctions; ++n)
365 {
366 nofparams += fFunctions[n]->GetNpar();
367 if (fCstIndexes[n] >= 0) nofparams -= 1;
368 }
369 return nofparams + fNOfFunctions; //fNOfFunctions for the coefficients
370}
371
372////////////////////////////////////////////////////////////////////////////////
373
375{
376 if (a >= b) {
377 Warning("SetRange", "Invalid range: %f >= %f", a, b);
378 return;
379 }
380
381 fXmin = a;
382 fXmax = b;
383
384 for (unsigned int n = 0; n < fNOfFunctions; n++) {
385 fFunctions[n]->SetRange(a, b);
386 fFunctions[n]->Update();
387 }
388}
389
390////////////////////////////////////////////////////////////////////////////////
391
393{
394 a = fXmin;
395 b = fXmax;
396}
397
398////////////////////////////////////////////////////////////////////////////////
399/// Update the component functions of the normalized sum
400
402{
403 for (unsigned int n = 0; n < fNOfFunctions; n++)
404 fFunctions[n]->Update();
405}
406
407////////////////////////////////////////////////////////////////////////////////
408
409void TF1NormSum::Copy(TObject &obj) const
410{
411 ((TF1NormSum &)obj).fNOfFunctions = fNOfFunctions;
412 ((TF1NormSum &)obj).fScale = fScale;
413 ((TF1NormSum &)obj).fXmin = fXmin;
414 ((TF1NormSum &)obj).fXmax = fXmax;
415 ((TF1NormSum &)obj).fCoeffs = fCoeffs;
416 ((TF1NormSum &)obj).fCstIndexes = fCstIndexes;
417 ((TF1NormSum &)obj).fParNames = fParNames;
418
419 // Clone objects in unique_ptr's
420 ((TF1NormSum &)obj).fFunctions = std::vector<std::unique_ptr<TF1>>(fNOfFunctions);
421 for (unsigned int n = 0; n < fNOfFunctions; n++) {
422 TF1 * f = (TF1*) fFunctions[n]->IsA()->New();
423 fFunctions[n]->Copy(*f);
424 ((TF1NormSum &)obj).fFunctions[n] = std::unique_ptr<TF1>(f);
425 }
426}
#define b(i)
Definition RSha256.hxx:100
#define f(i)
Definition RSha256.hxx:104
#define a(i)
Definition RSha256.hxx:99
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
#define ClassImp(name)
Definition Rtypes.h:377
void FixDuplicateNames(Iterator begin, Iterator end)
Function to find and rename duplicate parameters with the same name.
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
char name[80]
Definition TGX11.cxx:110
float xmin
float xmax
#define gROOT
Definition TROOT.h:407
void * New(ENewType defConstructor=kClassNew, Bool_t quiet=kFALSE) const
Return a pointer to a newly allocated object of this class.
Definition TClass.cxx:4978
Class adding two functions: c1*f1+c2*f2.
Definition TF1NormSum.h:19
void Copy(TObject &obj) const override
Copy this to obj.
Double_t fScale
Fixed Scale parameter to normalize function (e.g. bin width)
Definition TF1NormSum.h:23
const char * GetParName(Int_t ipar) const
Definition TF1NormSum.h:66
void GetRange(Double_t &a, Double_t &b) const
TF1NormSum & operator=(const TF1NormSum &rhs)
Operator =.
TClass * IsA() const override
Definition TF1NormSum.h:78
Double_t fXmax
Maximal bound of range of NormSum.
Definition TF1NormSum.h:25
std::vector< TString > fParNames
Parameter names.
Definition TF1NormSum.h:29
void SetRange(Double_t a, Double_t b) override
Double_t fXmin
Minimal bound of range of NormSum.
Definition TF1NormSum.h:24
std::vector< double > GetParameters() const
Return array of parameters.
double operator()(const Double_t *x, const Double_t *p) override
Overload the parenthesis to add the functions.
void Update() override
Update the component functions of the normalized sum.
std::vector< std::unique_ptr< TF1 > > fFunctions
Vector of size fNOfFunctions containing TF1 functions.
Definition TF1NormSum.h:26
void InitializeDataMembers(const std::vector< TF1 * > &functions, const std::vector< Double_t > &coeffs, Double_t scale)
void SetParameters(const Double_t *params) override
Initialize array of all parameters.
std::vector< Double_t > fCoeffs
Vector of size afNOfFunctions containing coefficients in front of each function.
Definition TF1NormSum.h:27
Int_t GetNpar() const
Return the number of (non constant) parameters including the coefficients: for 2 functions: c1,...
unsigned int fNOfFunctions
Number of functions to add.
Definition TF1NormSum.h:22
std::vector< Int_t > fCstIndexes
Vector with size of fNOfFunctions containing the index of the constant parameter/ function (the remov...
Definition TF1NormSum.h:28
1-Dim function class
Definition TF1.h:214
static void InitStandardFunctions()
Create the basic function objects.
Definition TF1.cxx:2497
@ kNotGlobal
Definition TF1.h:326
An array of TObjects.
Definition TObjArray.h:31
Collectable string class.
Definition TObjString.h:28
Mother of all ROOT objects.
Definition TObject.h:41
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:973
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:987
virtual void Fatal(const char *method, const char *msgfmt,...) const
Issue fatal error message.
Definition TObject.cxx:1015
Basic string class.
Definition TString.h:139
TObjArray * Tokenize(const TString &delim) const
This function is used to isolate sequential tokens in a TString.
Definition TString.cxx:2242
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2356
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
static uint64_t sum(uint64_t i)
Definition Factory.cxx:2345