GausFitCache.cxx
Go to the documentation of this file.
1 /**
2  * @file GausFitCache.cxx
3  * @brief Provide caches for TF1 functions to be used with ROOT fitters
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date March 25th, 2015
6  * @see GausFitCache.h
7  */
8 
9 // Library header
10 #include "GausFitCache.h"
11 
12 // C/C++ standard libraries
13 #include <cmath> // std::sqrt(), std::abs()
14 #include <sstream>
15 #include <memory> // std::default_delete<>
16 #include <algorithm> // std::sort(), std::for_each()
17 
18 // ROOT libraries
19 #include "TF1.h"
20 
21 // Framework libraries
22 #include "canvas/Utilities/Exception.h" // for Exception, LogicError
23 
24 namespace {
25 
26  template <typename T>
27  inline T sqr(T v) { return v*v; }
28 
29 } // local namespace
30 
31 
32 namespace hit {
33  //----------------------------------------------------------------------------
34  //--- GausFitCache
35  //---
37  std::for_each
38  (funcs.begin(), funcs.end(), std::default_delete<TF1>());
39  } // GausFitCache::~GausFitCache()
40 
41 
42  //----------------------------------------------------------------------------
43  TF1* GausFitCache::Get(size_t nFunc) {
44 
45  // expand the list if needed
46  if (nFunc >= funcs.size()) funcs.resize(nFunc + 1, nullptr);
47 
48  // get the pointer we need...
49  TF1*& pFunc = funcs[nFunc];
50 
51  if (!pFunc) pFunc = CreateFunction(nFunc);
52 
53  return pFunc;
54  } // GausFitCache::Get()
55 
56  //----------------------------------------------------------------------------
57  TF1* GausFitCache::GetClone(size_t nFunc)
58  { return static_cast<TF1*>(Get(nFunc)->Clone()); }
59 
60 
61  //----------------------------------------------------------------------------
62  TF1* GausFitCache::CreateFunction(size_t nFunc) const {
63 
64  std::string func_name = FunctionName(nFunc);
65 
66  // no gaussian, no nothing
67  if (nFunc == 0) return new TF1(func_name.c_str(), "0");
68 
69  std::ostringstream sstr;
70  sstr << "gaus(0)";
71  for (size_t iGaus = 1; iGaus < nFunc; ++iGaus)
72  sstr << " + gaus(" << (iGaus*3) << ")";
73 
74  // create and return the function
75  return new TF1(func_name.c_str(), sstr.str().c_str());
76  } // GausFitCache::CreateFunction()
77 
78 
79  //----------------------------------------------------------------------------
81  std::ostringstream sstr;
82  sstr << name << "_" << nFunc;
83  return sstr.str();
84  } // GausFitCache::FunctionName()
85 
86 
87  //----------------------------------------------------------------------------
88  //--- CompiledGausFitCacheBaseStruct
89  //---
91  (Double_t const* x, Double_t const* params)
92  {
93  return params[0] * std::exp(-0.5*sqr((x[0] - params[1])/params[2]));
94  } // details::CompiledGausFitCacheBaseStruct::gaus()
95 
96 
99  << "CompiledGausFitCacheBaseStruct: compiled functions can't be cloned";
100  } // CompiledGausFitCacheBaseStruct::GetClone()
101 
102 
104  (size_t nFunc) const
105  {
107  << name << " function cache can't create functions at run-time; " << nFunc
108  << "-addend function was requested, but we have only functions with up to "
109  << MaxGaussians() << " addends available\n";
110  } // CompiledGausFitCache<>::CannotCreateFunction()
111 
112  //----------------------------------------------------------------------------
113 
114 } // namespace hit
std::vector< TF1 * > funcs
Definition: GausFitCache.h:82
static Double_t gaus(Double_t const *x, Double_t const *params)
Single Gaussian function.
virtual TF1 * CreateFunction(size_t nFunc) const
Creates a new sum function.
std::string string
Definition: nybbler.cc:12
virtual TF1 * GetClone(size_t nGaus)
Throws an exception (ROOT does not support cloning compiled functions)
T sqr(T v)
virtual ~GausFitCache()
Destructor.
void CannotCreateFunction(size_t nGaus) const
Throws an error asserting compiled functions can&#39;t be cretead run-time.
virtual TF1 * Get(size_t nFunc)
Returns a function sum of nFunc base functions.
Detector simulation of raw signals on wires.
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
virtual std::string FunctionName(size_t nFunc) const
Returns a name for the function with nFunc base functions.
Provide caches for TF1 functions to be used with ROOT fitters.
list x
Definition: train.py:276
std::string name
name of the cache
Definition: GausFitCache.h:79
virtual TF1 * GetClone(size_t nGaus)