LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
GausFitCache.cxx
Go to the documentation of this file.
1 
9 // Library header
10 #include "GausFitCache.h"
11 
12 // C/C++ standard libraries
13 #include <algorithm> // std::sort(), std::for_each()
14 #include <cmath> // std::sqrt(), std::abs()
15 #include <memory> // std::default_delete<>
16 #include <sstream>
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)
28  {
29  return v * v;
30  }
31 
32 } // local namespace
33 
34 namespace hit {
35  //----------------------------------------------------------------------------
36  //--- GausFitCache
37  //---
39  {
40  std::for_each(funcs.begin(), funcs.end(), std::default_delete<TF1>());
41  } // GausFitCache::~GausFitCache()
42 
43  //----------------------------------------------------------------------------
44  TF1* GausFitCache::Get(size_t nFunc)
45  {
46 
47  // expand the list if needed
48  if (nFunc >= funcs.size()) funcs.resize(nFunc + 1, nullptr);
49 
50  // get the pointer we need...
51  TF1*& pFunc = funcs[nFunc];
52 
53  if (!pFunc) pFunc = CreateFunction(nFunc);
54 
55  return pFunc;
56  } // GausFitCache::Get()
57 
58  //----------------------------------------------------------------------------
59  TF1* GausFitCache::GetClone(size_t nFunc)
60  {
61  return static_cast<TF1*>(Get(nFunc)->Clone());
62  }
63 
64  //----------------------------------------------------------------------------
65  TF1* GausFitCache::CreateFunction(size_t nFunc) const
66  {
67 
68  std::string func_name = FunctionName(nFunc);
69 
70  // no gaussian, no nothing
71  if (nFunc == 0) return new TF1(func_name.c_str(), "0");
72 
73  std::ostringstream sstr;
74  sstr << "gaus(0)";
75  for (size_t iGaus = 1; iGaus < nFunc; ++iGaus)
76  sstr << " + gaus(" << (iGaus * 3) << ")";
77 
78  // create and return the function
79  return new TF1(func_name.c_str(), sstr.str().c_str());
80  } // GausFitCache::CreateFunction()
81 
82  //----------------------------------------------------------------------------
83  std::string GausFitCache::FunctionName(size_t nFunc) const
84  {
85  std::ostringstream sstr;
86  sstr << name << "_" << nFunc;
87  return sstr.str();
88  } // GausFitCache::FunctionName()
89 
90  //----------------------------------------------------------------------------
91  //--- CompiledGausFitCacheBaseStruct
92  //---
93  Double_t details::CompiledGausFitCacheBaseStruct::gaus(Double_t const* x, Double_t const* params)
94  {
95  return params[0] * std::exp(-0.5 * sqr((x[0] - params[1]) / params[2]));
96  } // details::CompiledGausFitCacheBaseStruct::gaus()
97 
99  {
101  << "CompiledGausFitCacheBaseStruct: compiled functions can't be cloned";
102  } // CompiledGausFitCacheBaseStruct::GetClone()
103 
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 " << MaxGaussians()
109  << " addends available\n";
110  } // CompiledGausFitCache<>::CannotCreateFunction()
111 
112  //----------------------------------------------------------------------------
113 
114 } // namespace hit
Float_t x
Definition: compare.C:6
std::vector< TF1 * > funcs
Definition: GausFitCache.h:80
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.
virtual TF1 * GetClone(size_t nGaus)
Throws an exception (ROOT does not support cloning compiled functions)
virtual ~GausFitCache()
Destructor.
constexpr T sqr(T v)
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.
std::string name
name of the cache
Definition: GausFitCache.h:77
virtual TF1 * GetClone(size_t nGaus)