LArSoft  v07_13_02
Liquid Argon Software toolkit - http://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 <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 
22 namespace {
23 
24  template <typename T>
25  inline T sqr(T v) { return v*v; }
26 
27 } // local namespace
28 
29 
30 namespace hit {
31  //----------------------------------------------------------------------------
32  //--- GausFitCache
33  //---
34  GausFitCache::~GausFitCache() {
35  std::for_each
36  (funcs.begin(), funcs.end(), std::default_delete<TF1>());
37  } // GausFitCache::~GausFitCache()
38 
39 
40  //----------------------------------------------------------------------------
41  TF1* GausFitCache::Get(size_t nFunc) {
42 
43  // expand the list if needed
44  if (nFunc >= funcs.size()) funcs.resize(nFunc + 1, nullptr);
45 
46  // get the pointer we need...
47  TF1*& pFunc = funcs[nFunc];
48 
49  if (!pFunc) pFunc = CreateFunction(nFunc);
50 
51  return pFunc;
52  } // GausFitCache::Get()
53 
54  //----------------------------------------------------------------------------
55  TF1* GausFitCache::GetClone(size_t nFunc)
56  { return static_cast<TF1*>(Get(nFunc)->Clone()); }
57 
58 
59  //----------------------------------------------------------------------------
60  TF1* GausFitCache::CreateFunction(size_t nFunc) const {
61 
62  std::string func_name = FunctionName(nFunc);
63 
64  // no gaussian, no nothing
65  if (nFunc == 0) return new TF1(func_name.c_str(), "0");
66 
67  std::ostringstream sstr;
68  sstr << "gaus(0)";
69  for (size_t iGaus = 1; iGaus < nFunc; ++iGaus)
70  sstr << " + gaus(" << (iGaus*3) << ")";
71 
72  // create and return the function
73  return new TF1(func_name.c_str(), sstr.str().c_str());
74  } // GausFitCache::CreateFunction()
75 
76 
77  //----------------------------------------------------------------------------
78  std::string GausFitCache::FunctionName(size_t nFunc) const {
79  std::ostringstream sstr;
80  sstr << name << "_" << nFunc;
81  return sstr.str();
82  } // GausFitCache::FunctionName()
83 
84 
85  //----------------------------------------------------------------------------
86  //--- CompiledGausFitCacheBaseStruct
87  //---
88  Double_t details::CompiledGausFitCacheBaseStruct::gaus
89  (Double_t const* x, Double_t const* params)
90  {
91  return params[0] * std::exp(-0.5*sqr((x[0] - params[1])/params[2]));
92  } // details::CompiledGausFitCacheBaseStruct::gaus()
93 
94 
95  TF1* details::CompiledGausFitCacheBaseStruct::GetClone(size_t nFunc) {
97  << "CompiledGausFitCacheBaseStruct: compiled functions can't be cloned";
98  } // CompiledGausFitCacheBaseStruct::GetClone()
99 
100 
101  void details::CompiledGausFitCacheBaseStruct::CannotCreateFunction
102  (size_t nFunc) const
103  {
105  << name << " function cache can't create functions at run-time; " << nFunc
106  << "-addend function was requested, but we have only functions with up to "
107  << MaxGaussians() << " addends available\n";
108  } // CompiledGausFitCache<>::CannotCreateFunction()
109 
110  //----------------------------------------------------------------------------
111 
112 } // namespace hit
Float_t x
Definition: compare.C:6
cout<< "Opened file "<< fin<< " ixs= "<< ixs<< endl;if(ixs==0) hhh=(TH1F *) fff-> Get("h1")
Definition: AddMC.C:8
T sqr(T v)
Detector simulation of raw signals on wires.
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
Provide caches for TF1 functions to be used with ROOT fitters.