This file is indexed.

/usr/include/ThePEG/ACDC/ACDCGen.icc is in libthepeg-dev 1.8.0-3build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
// -*- C++ -*-
//
// ACDCGen.icc is a part of ThePEG - Toolkit for HEP Event Generation
// Copyright (C) 1999-2011 Leif Lonnblad
//
// ThePEG is licenced under version 2 of the GPL, see COPYING for details.
// Please respect the MCnet academic guidelines, see GUIDELINES for details.
//

namespace ACDCGenerator {

template <typename Rnd, typename FncPtr>
inline ACDCGen<Rnd,FncPtr>::ACDCGen(Rnd * r)
  : theRnd(r), theNAcc(0), theN(0), theNI(1, 0),
    theSumW(1, 0.0), theSumW2(1, 0.0),
    theEps(100*std::numeric_limits<double>::epsilon()), theMargin(1.1),
    theNTry(100), theMaxTry(10000), useCheapRandom(false), theFunctions(1),
    theDimensions(1, 0), thePrimaryCells(1), theSumMaxInts(1, 0.0), theLast(0),
    theLastCell(0), theLastF(0.0) {
  maxsize = 0;
}

template <typename Rnd, typename FncPtr>
inline ACDCGen<Rnd,FncPtr>::ACDCGen()
  : theRnd(0), theNAcc(0), theN(0), theNI(1, 0),
    theSumW(1, 0.0), theSumW2(1, 0.0),
    theEps(100*std::numeric_limits<double>::epsilon()), theMargin(1.1),
    theNTry(100), theMaxTry(10000), useCheapRandom(false), theFunctions(1),
    theDimensions(1, 0), thePrimaryCells(1), theSumMaxInts(1, 0.0), theLast(0),
    theLastCell(0), theLastF(0.0) {
  maxsize = 0;
}

template <typename Rnd, typename FncPtr>
inline ACDCGen<Rnd,FncPtr>::~ACDCGen() {
  clear();
}

template <typename Rnd, typename FncPtr>
inline void ACDCGen<Rnd,FncPtr>::setRnd(Rnd * r) {
  theRnd = r;
}

template <typename Rnd, typename FncPtr>
inline void ACDCGen<Rnd,FncPtr>::clear() {
  theNAcc = 0;
  theN = 0;
  theNI = vector<long>(1, 0);
  theSumW = DVector(1, 0.0);
  theSumW2 = DVector(1, 0.0);
  theFunctions = FncVector(1);
  theDimensions = DimVector(1, 0);
  for ( int i = 0, N = thePrimaryCells.size(); i < N; ++i )
    if ( thePrimaryCells[i] ) delete thePrimaryCells[i];
  thePrimaryCells = CellVector(1);
  theSumMaxInts = DVector(1, 0.0);
  theLast = 0;
  theLastCell = 0;
  theLastPoint.clear();
  theLastF = 0.0;
  levels.clear();
}

template <typename Rnd, typename FncPtr>
inline bool ACDCGen<Rnd,FncPtr>::
addFunction(DimType dim, FncPtrType fnc, double maxrat) {
  if ( maxrat < 0.0 ) maxrat = 1.0/nTry();
  typedef map<double,DVector> PointMap;
  theLast = theFunctions.size();
  theFunctions.push_back(fnc);
  theNI.push_back(0);
  theSumW.push_back(0.0);
  theSumW2.push_back(0.0);
  theDimensions.push_back(dim);

  // Generate nTry() points with non-zero function value
  DVector x(dim);
  PointMap pmap;
  long itry = 0;
  while ( pmap.size() < nTry() ) {
    if ( ++itry > maxTry() ) {
      thePrimaryCells.push_back(new ACDCGenCell(0.0));
      theSumMaxInts.push_back(theSumMaxInts.back() + cells().back()->doMaxInt());
      return false;
    }
    rnd(dim, x);
    double val = FncTraits::value(fnc, x);
    if ( val > 0.0 ) {
      pmap[val] = x;
      itry = 0;
    }
  }

  // Create the root cell and set its overestimated function value to
  // the smallest non-zero value found
  double minf = pmap.begin()->first;
  double maxf = (--pmap.end())->first;
  minf = max(minf, maxrat*maxf);
  //  thePrimaryCells.push_back(new ACDCGenCell(pmap.begin()->first));
  thePrimaryCells.push_back(new ACDCGenCell(minf));
  theLastF = pmap.begin()->first;
  pmap.erase(pmap.begin());
  theSumMaxInts.push_back(theSumMaxInts.back() + cells().back()->doMaxInt());

  // Start the divide-and-conquer procedure using the point with the
  // highest function value found.
  theLastF = (--pmap.end())->first;
  theLastPoint =  (--pmap.end())->second;
  pmap.erase(--pmap.end());
  DVector up(dim, 1.0);
  DVector lo(dim, 0.0);
  theLastCell = cells().back()->getCell(lo, lastPoint(), up);
  if ( lastF() > lastCell()->g() ) {
    compensate(lo, up);
    levels.clear();
  }

  // For each other generated point check that it's below the
  // overestimated value of the corresponding cell. If not start the
  // divide-and-conquer using that point.
  while ( !pmap.empty() ) {
    theLastPoint = pmap.begin()->second;
    theLastF = pmap.begin()->first;
    pmap.erase(pmap.begin());
    DVector up(dim, 1.0);
    DVector lo(dim, 0.0);
    theLastCell = cells().back()->getCell(lo, lastPoint(), up);
    if ( lastF() > lastCell()->g() ) {
      compensate(lo, up);
      levels.clear();
    }
  }
  //  cells().back()->smooth(1.0/nTry());
  return true;
}

template <typename Rnd, typename FncPtr>
inline void ACDCGen<Rnd,FncPtr>::chooseCell(DVector & lo, DVector & up) {
  if ( compensating() ) {
    // If we are compensating, we must choose the cell to be compensated.
    up = levels.back().up;
    lo = levels.back().lo;
    theLastCell = levels.back().cell;
    theLast = levels.back().index;
  } else {
    // Otherwise, first choose the function to be used and choose the
    // corresponding root cell.
    theLast = upper_bound(sumMaxInts().begin(), sumMaxInts().end(),
			  rnd()*sumMaxInts().back())
      - sumMaxInts().begin();
    if(theLast>=sumMaxInts().size()) {
      throw ThePEG::Exception() << "Selected a function outside the allowed range"
				<< " in ACDCGen::chooseCell(). This is usually due"
				<< " to a floating point error (nan or inf) in the"
				<< " calculation of the weight"
				<< ThePEG::Exception::abortnow;
    }
    up = DVector(lastDimension(), 1.0);
    lo = DVector(lastDimension(), 0.0);
    theLastCell = lastPrimary();
  }

  // Now select randomly a sub-cell of the chosen cell
  if ( cheapRandom() ) {
    theLastCell = lastCell()->generate(lo, up, theRnd);
  } else {
    DVector rndv(lastDimension());
    rnd(lastDimension(), rndv);
    theLastCell = lastCell()->generate(lo, up, rndv);
  }
}

template <typename Rnd, typename FncPtr>
inline typename ACDCGen<Rnd,FncPtr>::FncPtrType
ACDCGen<Rnd,FncPtr>::generate() {
  long itry = 0;
  while ( true ) {
    if ( ++itry > maxTry() ) return FncPtrType();
    ++theN;

    // First choose a function and a cell to generate in.
    DVector up;
    DVector lo;
    chooseCell(lo, up);

    // Now choose a point in that cell according to a flat distribution.
    DimType D = lastDimension();
    theLastPoint.resize(D);
    for ( DimType d = 0; d < D; ++d ) theLastPoint[d] = rnd(lo[d], up[d]);

    // Calculate the function value in this point
    theLastF = FncTraits::value(lastFunction(), theLastPoint);
    if ( theLastF <= 0.0 ) continue;

    // If we are compensating we require the function value to be
    // above the previous overestimate of the function.
    if ( compensating() && lastF() < levels.back().g ) theLastF = 0.0;

    double w = lastF()/lastCell()->g();
    if ( w > 1.0 ) {
      // If the value was above the overestimate then we must start
      // the compensation procedure and the curren point is disregarded.
      --theN;
      compensate(lo, up);
      continue;
    }

    // Accept the point according to the ratio of the true and
    // overestimated function value.
    theSumW[last()] += w;
    theSumW2[last()] += w*w;
    ++theNI[last()];
    if ( w > rnd() ) {
      ++theNAcc;
      return lastFunction();
    }
  }
}

template <typename Rnd, typename FncPtr>
inline void ACDCGen<Rnd,FncPtr>::reject() {
  theSumW[last()] -= 1.0;
  theSumW2[last()] -= 1.0;
  --theNAcc;
}

template <typename Rnd, typename FncPtr>
inline bool ACDCGen<Rnd,FncPtr>::compensating() {
  while ( levels.size() && levels.back().lastN < N() ) levels.pop_back();
  // Leave all levels which has reached there 'expiry date'.

  return !levels.empty();
}

template <typename Rnd, typename FncPtr>
inline long ACDCGen<Rnd,FncPtr>::compleft() const {
  if ( levels.empty() ) return 0;
  long left = 1;
  for ( int i = 0, Ni = levels.size(); i < Ni; ++i )
    left = max(left, levels[i].lastN - N());

  return left;
}

template <typename Rnd, typename FncPtr>
inline void ACDCGen<Rnd,FncPtr>::
compensate(const DVector & lo, const DVector & up) {
  //Save the previous overestimated integral and create a new
  //compensation level.
  double i0 = maxInt();
  Level level;
  level.g = lastCell()->g();

  // Start the divide-and-conquer algorithm slicing up the selected
  // cell and specify it as the cell to compensate.
  Slicer slicer(lastDimension(), *this, lo, up);
  level.cell = slicer.first;
  level.index = last();
  level.up = slicer.firstup;
  level.lo = slicer.firstlo;

  // Now calculate the the new overestimated total integral and
  // calculate the number of attempted points needed to compensate.
  double rat = (doMaxInt())/i0;
  level.lastN = long(N()*rat);

  // If we are already compensating increase also the previous
  // compensation levels.
  for ( size_type i = 0; i < levels.size(); ++i )
    levels[i].lastN = long(levels[i].lastN*rat);
  levels.insert(levels.end(), level);
  maxsize = std::max(maxsize, levels.size());
}

template <typename Rnd, typename FncPtr>
inline void ACDCGen<Rnd,FncPtr>::eps(double newEps) {
  theEps = newEps;
}

template <typename Rnd, typename FncPtr>
inline void ACDCGen<Rnd,FncPtr>::margin(double newMargin) {
  theMargin = newMargin;
}

template <typename Rnd, typename FncPtr>
inline long ACDCGen<Rnd,FncPtr>::N() const {
  return theN;
}

template <typename Rnd, typename FncPtr>
inline long ACDCGen<Rnd,FncPtr>::n() const {
  return theNAcc;
}

template <typename Rnd, typename FncPtr>
inline typename ACDCGen<Rnd,FncPtr>::size_type
ACDCGen<Rnd,FncPtr>::nTry() const {
  return theNTry;
}

template <typename Rnd, typename FncPtr>
inline void ACDCGen<Rnd,FncPtr>::nTry(size_type newNTry) {
  theNTry = newNTry;
}

template <typename Rnd, typename FncPtr>
inline long ACDCGen<Rnd,FncPtr>::maxTry() const {
  return theMaxTry;
}

template <typename Rnd, typename FncPtr>
inline void ACDCGen<Rnd,FncPtr>::maxTry(long newMaxTry) {
  theMaxTry = newMaxTry;
}

template <typename Rnd, typename FncPtr>
inline bool ACDCGen<Rnd,FncPtr>::cheapRandom() const {
  return useCheapRandom;
}

template <typename Rnd, typename FncPtr>
inline void ACDCGen<Rnd,FncPtr>::cheapRandom(bool b) {
  useCheapRandom = b;
}

template <typename Rnd, typename FncPtr>
inline double ACDCGen<Rnd,FncPtr>::maxInt() const {
  return theSumMaxInts.back();
}

template <typename Rnd, typename FncPtr>
inline double ACDCGen<Rnd,FncPtr>::doMaxInt() {
  for ( size_type i = 1, imax = functions().size(); i < imax; ++i )
    theSumMaxInts[i] = sumMaxInts()[i - 1] + cells()[i]->doMaxInt();
  return maxInt();
}

template <typename Rnd, typename FncPtr>
inline int ACDCGen<Rnd,FncPtr>::nBins() const {
  int sum = 0;
  for ( size_type i = 1; i < functions().size(); ++i )
    sum += cell(i)->nBins();
  return sum;
}

template <typename Rnd, typename FncPtr>
inline int ACDCGen<Rnd,FncPtr>::depth() const {
  int mx = 0;
  for ( size_type i = 1; i < functions().size(); ++i )
    mx = max(mx, cell(i)->depth());
  return mx;
}

template <typename Rnd, typename FncPtr>
inline double ACDCGen<Rnd,FncPtr>::efficiency() const {
  return N() > 0? double(n())/double(N()): 0.0;
}

template <typename Rnd, typename FncPtr>
inline double ACDCGen<Rnd,FncPtr>::integral(FncPtrType f) const {
  if ( N() <= 0 ) return maxInt();
  double sumw = 0.0;
  for ( size_type i = 1; i < functions().size(); ++i )
    if ( functions()[i] == f || !f ) sumw += theSumW[i];
  return maxInt()*sumw/N();
}

template <typename Rnd, typename FncPtr>
inline double ACDCGen<Rnd,FncPtr>::integralErr(FncPtrType f) const {
  if ( N() <= 0 ) return maxInt();
  double sumw2 = 0.0;
  double sumw = 0.0;
  for ( size_type i = 1; i < functions().size(); ++i )
    if ( functions()[i] == f || !f ) {
      sumw2 += theSumW2[i];
      sumw += theSumW[i];
    }
  if ( f ) return maxInt()*sqrt(sumw2)/N();
  return maxInt()*sqrt(max(0.,sumw2 - sumw*sumw/N()))/N();
}

template <typename Rnd, typename FncPtr>
inline double ACDCGen<Rnd,FncPtr>::eps() const {
  return theEps;
}

template <typename Rnd, typename FncPtr>
inline double ACDCGen<Rnd,FncPtr>::margin() const {
  return theMargin;
}

template <typename Rnd, typename FncPtr>
inline double ACDCGen<Rnd,FncPtr>::rnd() const {
  return RndTraits::rnd(theRnd);
}

template <typename Rnd, typename FncPtr>
inline double ACDCGen<Rnd,FncPtr>::rnd(double lo, double up) const {
  return RndTraits::rnd(theRnd, lo, up);
}

template <typename Rnd, typename FncPtr>
inline void ACDCGen<Rnd,FncPtr>::
rnd(const DVector & lo, const DVector & up, DVector & r) const {
  RndTraits::rnd(theRnd, lo.begin(), lo.end(), up.begin(), r.begin());
}

template <typename Rnd, typename FncPtr>
inline void ACDCGen<Rnd,FncPtr>::
rnd(DimType D, DVector & r) const {
  RndTraits::rnd(theRnd, D, r.begin());
}

template <typename Rnd, typename FncPtr>
inline long ACDCGen<Rnd,FncPtr>::rndInt(long x) const {
  return RndTraits::rndInt(theRnd, x);
}

template <typename Rnd, typename FncPtr>
inline const typename ACDCGen<Rnd,FncPtr>::FncVector &
ACDCGen<Rnd,FncPtr>::functions() const {
  return theFunctions;
}

template <typename Rnd, typename FncPtr>
inline typename ACDCGen<Rnd,FncPtr>::FncPtrType
ACDCGen<Rnd,FncPtr>::function(size_type i) const {
  return functions()[i];
}

template <typename Rnd, typename FncPtr>
inline typename ACDCGen<Rnd,FncPtr>::FncPtrType
ACDCGen<Rnd,FncPtr>::lastFunction() const {
  return function(last());
}

template <typename Rnd, typename FncPtr>
inline const typename ACDCGen<Rnd,FncPtr>::DimVector &
ACDCGen<Rnd,FncPtr>::dimensions() const {
  return theDimensions;
}

template <typename Rnd, typename FncPtr>
inline DimType ACDCGen<Rnd,FncPtr>::dimension(size_type i) const {
  return dimensions()[i];
}

template <typename Rnd, typename FncPtr>
inline DimType ACDCGen<Rnd,FncPtr>::lastDimension() const {
  return dimension(last());
}

template <typename Rnd, typename FncPtr>
inline const typename ACDCGen<Rnd,FncPtr>::CellVector &
ACDCGen<Rnd,FncPtr>::cells() const {
  return thePrimaryCells;
}

template <typename Rnd, typename FncPtr>
inline ACDCGenCell * ACDCGen<Rnd,FncPtr>::cell(size_type i) const {
  return cells()[i];
}

template <typename Rnd, typename FncPtr>
inline ACDCGenCell * ACDCGen<Rnd,FncPtr>::lastPrimary() const {
  return cell(last());
}

template <typename Rnd, typename FncPtr>
inline const DVector & ACDCGen<Rnd,FncPtr>::sumMaxInts() const {
  return theSumMaxInts;
}

template <typename Rnd, typename FncPtr>
inline typename ACDCGen<Rnd,FncPtr>::size_type
ACDCGen<Rnd,FncPtr>::last() const {
  return theLast;
}

template <typename Rnd, typename FncPtr>
inline typename ACDCGen<Rnd,FncPtr>::size_type
ACDCGen<Rnd,FncPtr>::size() const {
  return cells().size() - 1;
}

template <typename Rnd, typename FncPtr>
inline ACDCGenCell * ACDCGen<Rnd,FncPtr>::lastCell() const {
  return theLastCell;
}

template <typename Rnd, typename FncPtr>
inline const DVector & ACDCGen<Rnd,FncPtr>::lastPoint() const {
  return theLastPoint;
}

template <typename Rnd, typename FncPtr>
inline double ACDCGen<Rnd,FncPtr>::lastF() const {
  return theLastF;
}

template <typename Rnd, typename FncPtr>
typename ACDCGen<Rnd,FncPtr>::size_type ACDCGen<Rnd,FncPtr>::maxsize = 0;

template <typename Rnd, typename FncPtr>
vector<ACDCGenCellInfo> ACDCGen<Rnd,FncPtr>::extractCellInfo() const {
  vector<ACDCGenCellInfo> ret;
  for ( size_type i = 1; i < cells().size(); ++i ) {
    DVector lo(dimension(i), 0.0);
    DVector up(dimension(i), 1.0);
    cell(i)->extract(lo, up, ret);
  }
  return ret;
}

template <typename Rnd, typename FncPtr>
ACDCGen<Rnd,FncPtr>::Slicer::
Slicer(DimType Din, ACDCGen & gen, const DVector & loin, const DVector & upin)
  : D(Din), lo(loin), up(upin), xcl(loin), xcu(upin), xhl(loin), xhu(upin),
    fhl(Din, 0.0), fhu(Din, 0.0), xsel(gen.lastPoint()), fsel(gen.lastF()),
    current(gen.lastCell()), first(gen.lastCell()),
    firstlo(loin), firstup(upin),f(gen.lastFunction()),
    epsilon(gen.eps()), margin(gen.margin()), minf(0.0), wholecomp(false) {
  divideandconquer();
}

template <typename Rnd, typename FncPtr>
ACDCGen<Rnd,FncPtr>::Slicer::~Slicer() {
  // Added for debugging purposes.
}

template <typename Rnd, typename FncPtr>
void ACDCGen<Rnd,FncPtr>::Slicer::divideandconquer() {
  // If the current function value was just a little above the
  // overestimate, just increase the overestimate of this cell and
  // we're done.
  if ( fsel < current->g()*margin ) {
    current->g(current->g()*margin);
    return;
  }

  // First initialize and slice up the current cell and save the
  // resulting for the compensation procedure.
  init();
  slice();
  if ( !wholecomp ) {
    first = current;
    firstlo = lo;
    firstup = up;
  }

  // Find the largest function value in the current cell and as long
  // as it is above the current overestimate, increase the
  // overestimate and repeat the slicing.
  while ( shiftmaxmin() > current->g() ) {
    current->g(minf*margin);
    if ( current->g() > fsel ) return;
    init();
    slice();
  }   
}

template <typename Rnd, typename FncPtr>
double ACDCGen<Rnd,FncPtr>::Slicer::shiftmaxmin() {
  // Find the largest diagonal
  DVector test = xsel;
  double scale = 0.0;
  for ( DimType d = 0; d < D; ++d )
    if ( fhl[d] > fsel || fhu[d] > fsel ) scale += 1.0;
  scale = sqrt(scale);
  for ( DimType d = 0; d < D; ++d ) {
    if ( fhl[d] > fsel && fhl[d] > fhu[d] )
      test[d] = test[d] + (xhl[d] - test[d])/scale;
    if ( fhu[d] > fsel && fhu[d] > fhl[d] )
      test[d] = test[d] + (xhu[d] - test[d])/scale;
  }

  // Find the largest value above overestimate
  DimType dsel = -1;
  double x = 0;
  minf = fsel;
  for ( DimType d = 0; d < D; ++d ) {

    // Find the point with the function minimum value above the
    // current overestimate.
    minf = std::min(minf, fhl[d]);
    minf = std::min(minf, fhu[d]);

    // Find points with the maximum function value and shift the
    // current point to it.
    if ( fhu[d] > fsel ) {
      fsel = fhu[d];
      dsel = d;
      x = xhu[d];
    }
    if ( fhl[d] > fsel ) {
      fsel = fhl[d];
      dsel = d;
      x = xhl[d];
    }
  }

  // Check also the largest diagonal
  //  double ftest = (*f)(test);
//   if ( ftest > fsel ) {
//     xsel = test;
//     fsel = ftest;
//     dsel = -1;
//   }

  if ( dsel >= 0 ) xsel[dsel] = x;
  minf = std::max(minf, current->g());
  return fsel;
}

template <typename Rnd, typename FncPtr>
void ACDCGen<Rnd,FncPtr>::Slicer::dohalf(DimType d) {
  xcl[d] = lo[d];

  // Find the point closest below the current point in the given
  // dimension with a function value below the current overestimate,
  // also find the one furthest away from the current point with a
  // function value above the current overestimate. Use a crude
  // iterative mid-point selection.
  while ( true ) {
    xhl[d] = (xsel[d] + xcl[d])*0.5;

    std::swap(xsel[d], xhl[d]);
    fhl[d] = FncTraits::value(f, xsel);
    std::swap(xsel[d], xhl[d]);

    if ( fhl[d] > current->g() ) break;
    if ( xsel[d] - xcl[d] < epsilon ) break;

    xcl[d] = xhl[d];
  }

  // Check if the current slicing is worth doing...
  double cut = ( up[d] - xcl[d] )/( up[d] - lo[d] );
  if ( cut < 1.0 - current->g()/fsel && cut > 0.0 )
    rateslice.insert(std::make_pair(cut, -1-d));

  // Find the point closest above the current point in the given
  // dimension with a function value below the current overestimate,
  // also find the one furthest away from the current point with a
  // function value above the current overestimate. Use a crude
  // iterative mid-point selection.
  xcu[d] = up[d];
  while ( true ) {
    xhu[d] = (xsel[d] + xcu[d])*0.5;

    std::swap(xsel[d], xhu[d]);
    fhu[d] = FncTraits::value(f, xsel);
    std::swap(xsel[d], xhu[d]);

    if ( fhu[d] > current->g() ) break;
    if ( xcu[d] - xsel[d] < epsilon ) break;

    xcu[d] = xhu[d];
  }

  // Check if the current slicing is worth doing...
  cut = ( xcu[d] - lo[d] )/( up[d] - lo[d] );
  if ( cut < 1.0 - current->g()/fsel && cut > 0.0 )
    rateslice.insert(std::make_pair(cut, 1+d));

}

template <typename Rnd, typename FncPtr>
void ACDCGen<Rnd,FncPtr>::Slicer::init() {
  for ( DimType d = 0; d < D; ++d ) dohalf(d);
}

template <typename Rnd, typename FncPtr>
void ACDCGen<Rnd,FncPtr>::Slicer::slice() {
  while ( !rateslice.empty() ) {
    // Perform the slicing which reduces the volume of the cell the
    // most first.
    DimType d = rateslice.begin()->second;
    rateslice.erase(rateslice.begin());
    if ( d > 0 ) {
      // Slice from above.
      d = d - 1;
      current->splitme(lo[d], xcu[d], up[d], d);
      checkdiag(current->upper(), d, xcu[d], up[d]);
      current = current->lower();
      up[d] = xcu[d];
    } else {
      // Slice from below..
      d = -d - 1;
      current->splitme(lo[d], xcl[d], up[d], d);
      checkdiag(current->lower(), d, lo[d], xcl[d]);
      current = current->upper();
      lo[d] = xcl[d];
    }
    dohalf(d);
  }
}

template <typename Rnd, typename FncPtr>
void ACDCGen<Rnd,FncPtr>::Slicer::
checkdiag(ACDCGenCell * cell, DimType dc, double lod, double upd) {
  return;
  // Look at the midpoint in the dc direction in which a cell has been
  // chopped off.
  if ( upd - lod <= epsilon ) return;
  DVector newlo = lo;
  DVector newup = up;
  newlo[dc] = lod;
  newup[dc] = upd;
  DVector newsel = xsel;
  newsel[dc] = 0.5*(lod + upd);
  double newfsel = FncTraits::value(f, newsel);
  double newfh = newfsel;
  DVector newxsel = newsel;
  vector<int> dir(D, 0);

  // For each other direction look at the mid point between the point
  // chosen above and the borders of the cell. Save the point which
  // gives the highest function value.
  for ( DimType d = 0; d < D; ++d ) {
    if ( d == dc ) continue;
    double xdum = 0.5*(newlo[d] + newsel[d]);
    swap(xdum, newsel[d]);
    double fh1 = FncTraits::value(f, newsel);
    if ( fh1 > newfsel ) {
      newfsel = fh1;
      newxsel = newsel;
    }
    if ( fh1 > newfh ) dir[d] = -1;
    swap(xdum, newsel[d]);
    xdum = 0.5*(newsel[d] + newup[d]);
    swap(xdum, newsel[d]);
    double fh2 = FncTraits::value(f, newsel);
    if ( fh2 > newfsel ) {
      newfsel = fh2;
      newxsel = newsel;
    }
    if ( fh2 > newfh && fh2 > fh1 ) dir[d] = 1;
    swap(xdum, newsel[d]);
  }

  // Now check along the diagonal where we found the highest values.
  for ( DimType d = 0; d < D; ++d ) {
    if ( dir[d] == 0 ) continue;
    if ( dir[d] > 0 ) newsel[d] = 0.5*(newsel[d] + newup[d]);
    else newsel[d] = 0.5*(newlo[d] + newsel[d]);
  }
  newfh = FncTraits::value(f, newsel);
  if ( newfh > newfsel ) {
    newfsel = newfh;
    newxsel = newsel;
  }

  if ( newfsel < cell->g() ) return;

  // If this the highest value is above the overestimate, also this
  // cell needs to be divided up and conquered.
  wholecomp = true;
  Slicer dummy(D, *this, cell, newlo, newxsel, newup, newfsel);
}

template <typename Rnd, typename FncPtr>
ACDCGen<Rnd,FncPtr>::Slicer::
Slicer(DimType Din, const Slicer & s, ACDCGenCell * cellin,
       const DVector & loin, const DVector & xselin, const DVector & upin,
       double fselin)
  : D(Din), lo(loin), up(upin), xcl(loin), xcu(upin), xhl(loin), xhu(upin),
    fhl(Din, 0.0), fhu(Din, 0.0), xsel(xselin), fsel(fselin),
    current(cellin), first(cellin),
    firstlo(loin), firstup(upin),f(s.f),
    epsilon(s.epsilon), margin(s.margin), minf(0.0), wholecomp(false) {
  divideandconquer();
}

template <typename Rnd, typename FncPtr>
template <typename POStream>
void ACDCGen<Rnd,FncPtr>::output(POStream & os) const {
  os << theNAcc << theN << theEps << theMargin << theNTry << theMaxTry
     << useCheapRandom << theLast << theLastPoint << theLastF
     << theFunctions.size() << levels.size();
  for ( int i = 1, N = theFunctions.size(); i < N; ++i )
    os << theFunctions[i] << theDimensions [i] << theSumMaxInts[i]
       << *thePrimaryCells[i] << theNI[i] << theSumW[i] << theSumW2[i];
  os << thePrimaryCells[theLast]->getIndex(theLastCell);
  for ( int i = 0, N = levels.size(); i < N; ++i )
    os << levels[i].lastN << levels[i].g << levels[i].index
       << levels[i].up << levels[i].lo
       << thePrimaryCells[levels[i].index]->getIndex(levels[i].cell);
}

template <typename Rnd, typename FncPtr>
template <typename PIStream>
void ACDCGen<Rnd,FncPtr>::input(PIStream & is) {
  clear();
  long fsize = 0;
  long lsize = 0;
  is >> theNAcc >> theN >> theEps >> theMargin >> theNTry >> theMaxTry
     >> useCheapRandom >> theLast >> theLastPoint >> theLastF >> fsize >> lsize;
  while ( --fsize ) {
    theFunctions.push_back(FncPtrType());
    theDimensions.push_back(DimType());
    theSumMaxInts.push_back(0.0);
    theNI.push_back(0);
    theSumW.push_back(0.0);
    theSumW2.push_back(0.0);
    thePrimaryCells.push_back(new ACDCGenCell(0.0));
    is >> theFunctions.back() >> theDimensions.back() >> theSumMaxInts.back()
       >> *thePrimaryCells.back() >> theNI.back()
       >> theSumW.back() >> theSumW2.back();
  }
  long index = -1;
  is >> index;
  theLastCell = thePrimaryCells[theLast]->getCell(index);
  while ( lsize-- ) {
    levels.push_back(Level());
    is >> levels.back().lastN >> levels.back().g >> levels.back().index
       >> levels.back().up >> levels.back().lo >> index;
    levels.back().cell = thePrimaryCells[levels.back().index]->getCell(index);
  }
}

}