CMS 3D CMS Logo

ProcForeach.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: MVAComputer
4 // Class : ProcForeach
5 //
6 
7 // Implementation:
8 // Loops over a specified amount of VarProcessors and passes each
9 // instance of a set of variables individually per iteration.
10 //
11 // Author: Christophe Saout
12 // Created: Sat Apr 24 15:18 CEST 2007
13 //
14 
15 #include <algorithm>
16 
18 
22 
23 using namespace PhysicsTools;
24 
25 namespace { // anonymous
26 
27 class ProcForeach : public VarProcessor {
28  public:
29  typedef VarProcessor::Registry::Registry<ProcForeach,
30  Calibration::ProcForeach> Registry;
31 
32  ProcForeach(const char *name,
34  const MVAComputer *computer);
35  ~ProcForeach() override {}
36 
37  void configure(ConfIterator iter, unsigned int n) override;
38  ConfigCtx::Context *
39  configureLoop(ConfigCtx::Context *ctx, ConfigCtx::iterator begin,
40  ConfigCtx::iterator cur, ConfigCtx::iterator end) override;
41 
42  void eval(ValueIterator iter, unsigned int n) const override;
43  std::vector<double> deriv(
44  ValueIterator iter, unsigned int n) const override;
45  LoopStatus loop(double *output, int *conf,
46  unsigned int nOutput,
47  LoopCtx& ctx,
48  unsigned int &nOffset) const override;
49 
50  private:
51  struct ConfContext : public VarProcessor::ConfigCtx::Context {
52  ConfContext(unsigned int origin, unsigned int count) :
53  origin(origin), count(count) {}
54  ~ConfContext() override {}
55 
56  unsigned int origin;
57  unsigned int count;
58  };
59 
60  unsigned int count;
61 };
62 
63 ProcForeach::Registry registry("ProcForeach");
64 
65 ProcForeach::ProcForeach(const char *name,
67  const MVAComputer *computer) :
68  VarProcessor(name, calib, computer),
69  count(calib->nProcs)
70 {
71 }
72 
73 void ProcForeach::configure(ConfIterator iter, unsigned int n)
74 {
75  iter << Variable::FLAG_NONE;
76  while(iter)
77  iter << iter++(Variable::FLAG_MULTIPLE);
78 }
79 
81 ProcForeach::configureLoop(ConfigCtx::Context *ctx_, ConfigCtx::iterator begin,
82  ConfigCtx::iterator cur, ConfigCtx::iterator end)
83 {
84  ConfContext *ctx = dynamic_cast<ConfContext*>(ctx_);
85  if (!ctx)
86  return new ConfContext(cur - begin + 1, count);
87 
88  for(ConfigCtx::iterator iter = cur; iter != end; iter++) {
89  iter->mask = Variable::FLAG_ALL;
90  iter->origin = ctx->origin;
91  }
92 
93  if (--ctx->count)
94  return ctx;
95  else
96  return nullptr;
97 }
98 
99 void ProcForeach::eval(ValueIterator iter, unsigned int n) const
100 {
101  auto const offset = iter.loopCtx().offset();
102  iter(offset);
103 
104  auto& loopSize = iter.loopCtx().size();
105  while(iter) {
106  unsigned int size = iter.size();
107  if (!loopSize)
108  loopSize = size;
109 
110  double value = iter[offset];
111  iter(value);
112  iter++;
113  }
114 }
115 
116 std::vector<double> ProcForeach::deriv(
117  ValueIterator iter, unsigned int n) const
118 {
119  auto const offset = iter.loopCtx().offset();
120  std::vector<unsigned int> offsets;
121  unsigned int in = 0, out = 0;
122  while(iter) {
123  offsets.push_back(in + offset);
124  in += (iter++).size();
125  out++;
126  }
127 
128  std::vector<double> result((out + 1) * in, 0.0);
129  for(unsigned int i = 0; i < out; i++)
130  result[(i + 1) * in + offsets[i]] = 1.0;
131 
132  return result;
133 }
134 
136 ProcForeach::loop(double *output, int *conf,
137  unsigned int nOutput, LoopCtx& ctx, unsigned int &nOffset) const
138 {
139  auto& index = ctx.index();
140  bool endIteration = false;
141  if (index++ == count) {
142  index = 0;
143  endIteration = true;
144  }
145  auto& offset = ctx.offset();
146  auto& size = ctx.size();
147 
148  if (offset == 0 && !endIteration) {
149  for(int cur = *conf + size; nOutput--; cur += size)
150  *++conf = cur;
151  }
152 
153  if (endIteration) {
154  if (++offset >= size) {
155  return kStop;
156  } else
157  return kReset;
158  } else if (offset > size) {
159  return kSkip;
160  } else {
161  nOffset = offset;
162  return kNext;
163  }
164 }
165 
166 } // anonymous namespace
size
Write out results.
constexpr char const *const kStop
Definition: channel_names.h:34
template to generate a registry singleton for a type.
Main interface class to the generic discriminator computer framework.
Definition: MVAComputer.h:39
#define end
Definition: vmac.h:39
Definition: value.py:1
#define begin
Definition: vmac.h:32
static Interceptor::Registry registry("Interceptor")
Common base class for variable processors.
Definition: VarProcessor.h:36