File: ProjectingSampler.cc

package info (click to toggle)
herwig%2B%2B 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 27,128 kB
  • ctags: 24,739
  • sloc: cpp: 188,949; fortran: 23,193; sh: 11,365; python: 5,069; ansic: 3,539; makefile: 1,865; perl: 2
file content (233 lines) | stat: -rw-r--r-- 7,090 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// -*- C++ -*-
//
// ProjectingSampler.cc is a part of Herwig++ - A multi-purpose Monte Carlo event generator
// Copyright (C) 2002-2012 The Herwig Collaboration
//
// Herwig++ is licenced under version 2 of the GPL, see COPYING for details.
// Please respect the MCnet academic guidelines, see GUIDELINES for details.
//
//
// This is the implementation of the non-inlined, non-templated member
// functions of the ProjectingSampler class.
//

#include "ProjectingSampler.h"
#include "ThePEG/Interface/ClassDocumentation.h"
#include "ThePEG/Interface/Parameter.h"
#include "ThePEG/EventRecord/Particle.h"
#include "ThePEG/Repository/UseRandom.h"
#include "ThePEG/Repository/EventGenerator.h"
#include "ThePEG/Utilities/DescribeClass.h"


#include "ThePEG/Persistency/PersistentOStream.h"
#include "ThePEG/Persistency/PersistentIStream.h"

using namespace Herwig;

ProjectingSampler::ProjectingSampler() 
  : theFirstIteration(true), theNIterations(4),
    theEnhancementFactor(2.0), theNBins(8),
    theEpsilon(0.5), theLastNPoints(0),
    theWeightThreshold(0.001) {}

ProjectingSampler::~ProjectingSampler() {}

IBPtr ProjectingSampler::clone() const {
  return new_ptr(*this);
}

IBPtr ProjectingSampler::fullclone() const {
  return new_ptr(*this);
}

void ProjectingSampler::select(double weight) {
  for ( size_t k = 0; k < lastPoint().size(); ++k ) {
    if ( theFirstIteration ) {
      theProjections[k].bin(lastPoint()[k]);
    }
    theProjections[k].select(theLastValue);
  }
  GeneralStatistics::select(weight);
}

void ProjectingSampler::accept() {
  for ( size_t k = 0; k < lastPoint().size(); ++k ) {
    theProjections[k].accept();
  }
  GeneralStatistics::accept();
}

void ProjectingSampler::reject() {
  for ( size_t k = 0; k < lastPoint().size(); ++k ) {
    theProjections[k].reject();
  }
  GeneralStatistics::reject();
}

void ProjectingSampler::generate(bool noMaxInfo) {
  double w = 1.;
  if ( !theFirstIteration ) {
    for ( size_t k = 0; k < lastPoint().size(); ++k ) {
      w *= theProjections[k].sample(lastPoint()[k]);
    }
  } else {
    for ( size_t k = 0; k < lastPoint().size(); ++k )
      lastPoint()[k] = UseRandom::rnd();
  }
  try {
    theLastValue = theEventHandler->dSigDR(lastPoint()) / nanobarn;
    w *= theLastValue;
  } catch (Veto&) {
    theLastValue = 0.0;
    w = 0.0;
  } catch (...) {
    throw;
  }
  if ( abs(w) > maxWeight() && !noMaxInfo ) {
    double old = maxWeight();
    select(w);
    throw NewMaximum(old,abs(w));
  }
  select(w);
  if ( !noMaxInfo &&
       selectedPoints() == theLastNPoints ) {
    theLastNPoints = (unsigned long)(theLastNPoints*theEnhancementFactor);
    adapt();
    throw UpdateCrossSections();
  }
}

void ProjectingSampler::initialize(bool progress) {
  if ( initialized() )
    return;
  lastPoint().resize(dimension());
  theProjections.resize(dimension(),BinnedStatistics(theNBins,theWeightThreshold));
  theLastNPoints = initialPoints();
  theFirstIteration = true;
  runIteration(theLastNPoints,progress);
  theLastNPoints = (unsigned long)(theLastNPoints*theEnhancementFactor);
  adapt();
  if ( theNIterations == 1 ) {
    isInitialized();
    return;
  }
  nextIteration();
  theFirstIteration = false;
  for ( unsigned long k = 1; k < theNIterations-1; ++k ) {
    runIteration(theLastNPoints,progress);
    theLastNPoints = (unsigned long)(theLastNPoints*theEnhancementFactor);
    adapt();
    nextIteration();
  }
  runIteration(theLastNPoints,progress);
  theLastNPoints = (unsigned long)(theLastNPoints*theEnhancementFactor);
  adapt();
  isInitialized();
}

struct ProjectingAdaptor {

  double variance;
  double epsilon;

  double importanceMeasure(const GeneralStatistics& s) const {
    return s.averageAbsWeight();
  }

  bool adapt(const GeneralStatistics& s) const {
    return s.averageAbsWeightVariance()/variance > epsilon;
  }

};

void ProjectingSampler::adapt() {

  ProjectingAdaptor adaptor;
  adaptor.variance = 0.;
  for ( vector<BinnedStatistics>::iterator s =
	  theProjections.begin(); s != theProjections.end(); ++s ) {
    double variance = 0.;
    for ( map<double,GeneralStatistics>::const_iterator k = 
	    s->statistics().begin(); k != s->statistics().end(); ++k ) {
      variance += k->second.averageAbsWeightVariance();
    }
    adaptor.variance += variance/s->statistics().size();
  }
  adaptor.variance /= theProjections.size();
  adaptor.epsilon = theEpsilon;

  size_t count = 0;
  for ( vector<BinnedStatistics>::iterator s =
	  theProjections.begin(); s != theProjections.end(); ++s, ++count ) {
    s->adapt(adaptor);
  }

}

// If needed, insert default implementations of virtual function defined
// in the InterfacedBase class here (using ThePEG-interfaced-impl in Emacs).


void ProjectingSampler::persistentOutput(PersistentOStream & os) const {
  os << theFirstIteration << theNIterations << theEnhancementFactor 
     << theNBins << theEpsilon << theLastNPoints
     << theProjections << theWeightThreshold;
}

void ProjectingSampler::persistentInput(PersistentIStream & is, int) {
  is >> theFirstIteration >> theNIterations >> theEnhancementFactor 
     >> theNBins >> theEpsilon >> theLastNPoints
     >> theProjections >> theWeightThreshold;
}


// *** Attention *** The following static variable is needed for the type
// description system in ThePEG. Please check that the template arguments
// are correct (the class and its base class), and that the constructor
// arguments are correct (the class name and the name of the dynamically
// loadable library where the class implementation can be found).
DescribeClass<ProjectingSampler,Herwig::BinSampler>
  describeHerwigProjectingSampler("Herwig::ProjectingSampler", "HwExsample2.so");

void ProjectingSampler::Init() {

  static ClassDocumentation<ProjectingSampler> documentation
    ("ProjectingSampler does adaption from projections of the integrand.");


  static Parameter<ProjectingSampler,unsigned long> interfaceNIterations
    ("NIterations",
     "The number of iterations to perform initially.",
     &ProjectingSampler::theNIterations, 4, 1, 0,
     false, false, Interface::lowerlim);


  static Parameter<ProjectingSampler,double> interfaceEnhancementFactor
    ("EnhancementFactor",
     "The enhancement factor for the number of points in the next iteration.",
     &ProjectingSampler::theEnhancementFactor, 2.0, 1.0, 0,
     false, false, Interface::lowerlim);


  static Parameter<ProjectingSampler,unsigned int> interfaceNBins
    ("NBins",
     "The number of projection bins to consider initially.",
     &ProjectingSampler::theNBins, 8, 1, 0,
     false, false, Interface::lowerlim);


  static Parameter<ProjectingSampler,double> interfaceEpsilon
    ("Epsilon",
     "The adaption threshold.",
     &ProjectingSampler::theEpsilon, 0.5, 0.0, 0,
     false, false, Interface::lowerlim);

  static Parameter<ProjectingSampler,double> interfaceWeightThreshold
    ("WeightThreshold",
     "The minimum weight per bin in units of the average weight.",
     &ProjectingSampler::theWeightThreshold, 0.001, 0.0, 0,
     false, false, Interface::lowerlim);

}