CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MVAComputer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: PhysicsToolsObjects
4 // Class : MVAComputer
5 //
6 
7 // Implementation:
8 // getProcessor() and addProcessor() methods to add processors to
9 // the discriminator computer calibration object. POOL doesn't support
10 // polymorph pointers, so this is implemented using multiple containers
11 // for each possible sub-class and an index array from which the
12 // array of pointers can be reconstructed. In order to avoid having
13 // to handle each sub-class container by hand here, the generated
14 // reflex dictionary is used to find and read/write the std::vector<...>
15 // containers for the individual classes in the private data members.
16 // So changes can be solely done in the header files and does not leave
17 // a trail elsewhere.
18 //
19 // Author: Christophe Saout
20 // Created: Sat Apr 24 15:18 CEST 2007
21 // $Id: MVAComputer.cc,v 1.10 2010/01/26 19:40:03 saout Exp $
22 //
23 #include <functional>
24 #include <algorithm>
25 #include <typeinfo>
26 #include <iostream>
27 #include <cstring>
28 #include <cstddef>
29 
30 #include <boost/thread.hpp>
31 
32 #include <Reflex/Reflex.h>
33 
35 
37 
38 namespace PhysicsTools {
39 namespace Calibration {
40 
41 std::string VarProcessor::getInstanceName() const
42 {
43  static const char prefix[] = "PhysicsTools::Calibration::";
44  std::string type = ROOT::Reflex::Tools::Demangle(typeid(*this));
45  if (type.size() <= sizeof prefix - 1 ||
46  type.substr(0, sizeof prefix - 1) != prefix)
47  throw cms::Exception("MVAComputerCalibration")
48  << "getInstanceName failed for "
49  << typeid(*this).name() << "." << std::endl;
50 
51  return type.substr(sizeof prefix - 1);
52 }
53 
54 std::string ProcExternal::getInstanceName() const
55 {
56  return method;
57 }
58 
60 {
61  static boost::mutex mutex;
62  static MVAComputer::CacheId nextCacheId = 0;
63 
64  boost::mutex::scoped_lock scoped_lock(mutex);
65  return ++nextCacheId;
66 }
67 
69  cacheId(getNextMVAComputerCacheId())
70 {
71 }
72 
74  inputSet(orig.inputSet),
75  output(orig.output),
76  cacheId(orig.cacheId)
77 {
78  for(std::vector<VarProcessor*>::const_iterator iter =
79  orig.processors.begin();
80  iter != orig.processors.end(); ++iter)
81  addProcessor(*iter);
82 }
83 
85 {
86  for(std::vector<VarProcessor*>::iterator iter = processors.begin();
87  iter != processors.end(); ++iter)
88  delete *iter;
89  processors.clear();
90 }
91 
93 {
94  inputSet = orig.inputSet;
95  output = orig.output;
96  cacheId = orig.cacheId;
97 
98  for(std::vector<VarProcessor*>::iterator iter = processors.begin();
99  iter != processors.end(); ++iter)
100  delete *iter;
101  processors.clear();
102 
103  for(std::vector<VarProcessor*>::const_iterator iter =
104  orig.processors.begin();
105  iter != orig.processors.end(); ++iter)
106  addProcessor(*iter);
107 
108  return *this;
109 }
110 
111 std::vector<VarProcessor*> MVAComputer::getProcessors() const
112 {
113  return processors;
114 }
115 
117 {
119 
120  ROOT::Reflex::Type baseType = ROOT::Reflex::GetType<VarProcessor>();
122  ROOT::Reflex::Type::ByTypeInfo(typeid(*proc));
123  ROOT::Reflex::Type refType(type,
124  ROOT::Reflex::CONST & ROOT::Reflex::REFERENCE);
125  if (!type.Name().size())
126  throw cms::Exception("MVAComputerCalibration")
127  << "Calibration class " << typeid(*proc).name()
128  << " not registered with ROOT::Reflex."
129  << std::endl;
130 
131  ROOT::Reflex::Object obj =
132  ROOT::Reflex::Object(baseType, const_cast<void*>(
133  static_cast<const void*>(proc))).CastObject(type);
134 
135  // find and call copy constructor
136  for(ROOT::Reflex::Member_Iterator iter = type.FunctionMember_Begin();
137  iter != type.FunctionMember_End(); iter++) {
138  const ROOT::Reflex::Type &ctor = iter->TypeOf();
139  if (!iter->IsConstructor() ||
140  ctor.FunctionParameterSize() != 1 ||
141  ctor.FunctionParameterAt(0).Id() != refType.Id())
142  continue;
143 
144  ROOT::Reflex::Object copy = type.Construct(ctor,
145  ROOT::Reflex::Tools::MakeVector<void*>(obj.Address()));
146 
147  processors.push_back(static_cast<VarProcessor*>(copy.Address()));
148  return;
149  }
150 
151  throw cms::Exception("MVAComputerCalibration")
152  << "Calibration class " << typeid(*proc).name()
153  << " has no copy ctor registered with ROOT::Reflex."
154  << std::endl;
155 }
156 
158 {
159  static MVAComputerContainer::CacheId nextCacheId = 0;
160  return ++nextCacheId;
161 }
162 
165 {
166 }
167 
169 {
171 
172  entries.push_back(std::make_pair(label, MVAComputer()));
173  return entries.back().second;
174 }
175 
176 namespace {
177  struct Comparator :
178  public std::unary_function<const std::string&, bool> {
179 
180  inline Comparator(const std::string &label) : label(label) {}
181 
182  inline bool
183  operator () (const MVAComputerContainer::Entry &entry) const
184  { return entry.first == label; }
185 
186  const std::string &label;
187  };
188 }
189 
190 const MVAComputer &MVAComputerContainer::find(const std::string &label) const
191 {
192  std::vector<Entry>::const_iterator pos =
193  std::find_if(entries.begin(), entries.end(),
194  Comparator(label));
195 
196  if (pos == entries.end())
197  throw cms::Exception("MVAComputerCalibration")
198  << "Calibration record " << label
199  << " not found in MVAComputerContainer." << std::endl;
200 
201  return pos->second;
202 }
203 
204 } // namespace Calibration
205 } // namespace PhysicsTools
type
Definition: HCALResponse.h:22
virtual const MVAComputer & find(const std::string &label) const
Definition: MVAComputer.cc:190
virtual std::vector< VarProcessor * > getProcessors() const
Definition: MVAComputer.cc:111
virtual std::string getInstanceName() const
Definition: MVAComputer.cc:41
static boost::mutex mutex
Definition: LHEProxy.cc:11
TrainProcessor *const proc
Definition: MVATrainer.cc:101
MVAComputer & operator=(const MVAComputer &orig)
Definition: MVAComputer.cc:92
std::vector< VarProcessor * > processors
Definition: MVAComputer.h:181
std::pair< std::string, MonitorElement * > entry
Definition: ME_MAP.h:8
std::pair< std::string, MVAComputer > Entry
Definition: MVAComputer.h:190
virtual std::string getInstanceName() const
Definition: MVAComputer.cc:54
void addProcessor(const VarProcessor *proc)
Definition: MVAComputer.cc:116
static MVAComputer::CacheId getNextMVAComputerCacheId()
Definition: MVAComputer.cc:59
static MVAComputerContainer::CacheId getNextMVAComputerContainerCacheId()
Definition: MVAComputer.cc:157
std::vector< Variable > inputSet
Definition: MVAComputer.h:177
MVAComputer & add(const std::string &label)
Definition: MVAComputer.cc:168