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:
30 
31  ProcForeach(const char *name, const Calibration::ProcForeach *calib, const MVAComputer *computer);
32  ~ProcForeach() override {}
33 
34  void configure(ConfIterator iter, unsigned int n) override;
35  ConfigCtx::Context *configureLoop(ConfigCtx::Context *ctx,
36  ConfigCtx::iterator begin,
37  ConfigCtx::iterator cur,
38  ConfigCtx::iterator end) override;
39 
40  void eval(ValueIterator iter, unsigned int n) const override;
41  std::vector<double> deriv(ValueIterator iter, unsigned int n) const override;
42  LoopStatus loop(double *output, int *conf, unsigned int nOutput, LoopCtx &ctx, unsigned int &nOffset) const override;
43 
44  private:
45  struct ConfContext : public VarProcessor::ConfigCtx::Context {
46  ConfContext(unsigned int origin, unsigned int count) : origin(origin), count(count) {}
47  ~ConfContext() override {}
48 
49  unsigned int origin;
50  unsigned int count;
51  };
52 
53  unsigned int count;
54  };
55 
56  ProcForeach::Registry registry("ProcForeach");
57 
58  ProcForeach::ProcForeach(const char *name, const Calibration::ProcForeach *calib, const MVAComputer *computer)
59  : VarProcessor(name, calib, computer), count(calib->nProcs) {}
60 
61  void ProcForeach::configure(ConfIterator iter, unsigned int n) {
62  iter << Variable::FLAG_NONE;
63  while (iter)
64  iter << iter++(Variable::FLAG_MULTIPLE);
65  }
66 
67  VarProcessor::ConfigCtx::Context *ProcForeach::configureLoop(ConfigCtx::Context *ctx_,
68  ConfigCtx::iterator begin,
69  ConfigCtx::iterator cur,
70  ConfigCtx::iterator end) {
71  ConfContext *ctx = dynamic_cast<ConfContext *>(ctx_);
72  if (!ctx)
73  return new ConfContext(cur - begin + 1, count);
74 
75  for (ConfigCtx::iterator iter = cur; iter != end; iter++) {
76  iter->mask = Variable::FLAG_ALL;
77  iter->origin = ctx->origin;
78  }
79 
80  if (--ctx->count)
81  return ctx;
82  else
83  return nullptr;
84  }
85 
86  void ProcForeach::eval(ValueIterator iter, unsigned int n) const {
87  auto const offset = iter.loopCtx().offset();
88  iter(offset);
89 
90  auto &loopSize = iter.loopCtx().size();
91  while (iter) {
92  unsigned int size = iter.size();
93  if (!loopSize)
94  loopSize = size;
95 
96  double value = iter[offset];
97  iter(value);
98  iter++;
99  }
100  }
101 
102  std::vector<double> ProcForeach::deriv(ValueIterator iter, unsigned int n) const {
103  auto const offset = iter.loopCtx().offset();
104  std::vector<unsigned int> offsets;
105  unsigned int in = 0, out = 0;
106  while (iter) {
107  offsets.push_back(in + offset);
108  in += (iter++).size();
109  out++;
110  }
111 
112  std::vector<double> result((out + 1) * in, 0.0);
113  for (unsigned int i = 0; i < out; i++)
114  result[(i + 1) * in + offsets[i]] = 1.0;
115 
116  return result;
117  }
118 
120  double *output, int *conf, unsigned int nOutput, LoopCtx &ctx, unsigned int &nOffset) const {
121  auto &index = ctx.index();
122  bool endIteration = false;
123  if (index++ == count) {
124  index = 0;
125  endIteration = true;
126  }
127  auto &offset = ctx.offset();
128  auto &size = ctx.size();
129 
130  if (offset == 0 && !endIteration) {
131  for (int cur = *conf + size; nOutput--; cur += size)
132  *++conf = cur;
133  }
134 
135  if (endIteration) {
136  if (++offset >= size) {
137  return kStop;
138  } else
139  return kReset;
140  } else if (offset > size) {
141  return kSkip;
142  } else {
143  nOffset = offset;
144  return kNext;
145  }
146  }
147 
148 } // 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
Definition: value.py:1
Common base class for variable processors.
Definition: VarProcessor.h:36