1 #ifndef MinL3AlgoUnivErr_H 2 #define MinL3AlgoUnivErr_H 75 template <
class IDdet>
76 class MinL3AlgoUnivErr {
78 typedef std::map<IDdet, float> IDmapF;
80 typedef typename IDmapF::iterator iter_IDmapF;
81 typedef typename IDmapF::const_iterator citer_IDmapF;
82 typedef std::map<IDdet, int> IDmapI;
84 typedef typename IDmapI::iterator iter_IDmapI;
85 typedef typename IDmapI::const_iterator citer_IDmapI;
91 MinL3AlgoUnivErr(
float kweight_ = 0.);
112 IDmapF iterate(
const std::vector<std::vector<float> >& eventMatrix,
114 const std::vector<float>& energyVector,
116 const bool& normalizeFlag =
false,
117 const int& nSubsamples = 10);
127 float getError(IDdet
id)
const;
139 IDmapF getError()
const;
150 int numberOfWrongErrors()
const;
164 float getErrorQuality(IDdet
id)
const;
166 IDmapF getErrorQuality()
const;
175 float getMeanPartialSolution(IDdet
id)
const;
183 IDmapF getMeanPartialSolution()
const;
188 void addEvent(
const std::vector<float>& myCluster,
const std::vector<IDdet>& idCluster,
const float&
energy);
194 std::vector<float> recalibrateEvent(
const std::vector<float>& myCluster,
195 const std::vector<IDdet>& idCluster,
196 const IDmapF& newCalibration,
197 const int& isol = -1,
198 const int& iter = -1);
204 IDmapF getSolution(
const bool resetsolution =
true);
209 void resetSolution();
220 std::vector<int> sumPartSolu0;
221 std::vector<float> sumPartSolu1;
222 std::vector<float> sumPartSolu2;
228 void registerPartialSolution(
const IDmapF& partialSolution);
236 template <
class IDdet>
237 MinL3AlgoUnivErr<IDdet>::MinL3AlgoUnivErr(
float kweight_) : kweight(kweight_), countEvents(0) {
238 std::cout <<
"MinL3AlgoUnivErr : L3 algo with a calculation" 239 <<
" of stat.errors working..." << std::endl;
245 template <
class IDdet>
246 MinL3AlgoUnivErr<IDdet>::~MinL3AlgoUnivErr() {}
250 template <
class IDdet>
251 typename MinL3AlgoUnivErr<IDdet>::IDmapF MinL3AlgoUnivErr<IDdet>::iterate(
252 const std::vector<std::vector<float> >& eventMatrix,
254 const std::vector<float>& energyVector,
256 const bool& normalizeFlag,
257 const int& nSubsamples) {
261 nOfSubsamples = nSubsamples;
265 sumPartSolu0.clear();
266 sumPartSolu1.clear();
267 sumPartSolu2.clear();
269 IDmapF totalSolution;
281 for (
int isol = 0; isol <= nSubsamples; isol++) {
282 IDmapF sampleSolution;
284 std::vector<std::vector<float> > myEventMatrix;
285 std::vector<std::vector<IDdet> > myIdMatrix;
286 std::vector<float> myEnergyVector;
294 myEventMatrix = eventMatrix;
295 myIdMatrix = idMatrix;
296 myEnergyVector = energyVector;
300 sampleSolution.clear();
301 myEventMatrix.clear();
303 myEnergyVector.clear();
305 for (
int i = 0; i < static_cast<int>(eventMatrix.size());
i++) {
307 if (
i % nSubsamples + 1 == isol) {
308 myEventMatrix.push_back(eventMatrix[
i]);
309 myIdMatrix.push_back(idMatrix[
i]);
310 myEnergyVector.push_back(energyVector[
i]);
315 int Nevents = myEventMatrix.size();
321 for (
int iter = 1; iter <= nIter; iter++) {
327 for (
i = 0;
i < Nevents;
i++) {
329 for (
unsigned j = 0;
j < myEventMatrix[
i].size();
j++) {
330 sumOverEnergy += myEventMatrix[
i][
j];
332 sumOverEnergy /= myEnergyVector[
i];
333 scale += sumOverEnergy;
337 for (
i = 0;
i < Nevents;
i++) {
338 myEnergyVector[
i] *=
scale;
343 for (
int iEvt = 0; iEvt < Nevents; iEvt++) {
344 addEvent(myEventMatrix[iEvt], myIdMatrix[iEvt], myEnergyVector[iEvt]);
346 iterSolution = getSolution();
347 if (iterSolution.empty()) {
348 sampleSolution.clear();
353 for (
int ievent = 0; ievent < Nevents; ievent++) {
354 myEventMatrix[ievent] = recalibrateEvent(myEventMatrix[ievent], myIdMatrix[ievent], iterSolution, isol, iter);
358 for (iter_IDmapF
i = iterSolution.begin();
i != iterSolution.end();
i++) {
359 iter_IDmapF itotal = sampleSolution.find(
i->first);
360 if (itotal == sampleSolution.end()) {
361 sampleSolution.insert(IDmapFvalue(
i->first,
i->second));
363 itotal->second *=
i->second;
373 totalSolution = sampleSolution;
376 registerPartialSolution(sampleSolution);
381 return totalSolution;
386 template <
class IDdet>
387 void MinL3AlgoUnivErr<IDdet>::addEvent(
const std::vector<float>& myCluster,
388 const std::vector<IDdet>& idCluster,
392 float w, invsumXmatrix;
396 float sumXmatrix = 0.;
397 for (
unsigned i = 0;
i < myCluster.size();
i++) {
398 sumXmatrix += myCluster[
i];
402 eventw = 1 - fabs(1 - sumXmatrix /
energy);
403 eventw =
pow(eventw, kweight);
405 if (sumXmatrix != 0.) {
406 invsumXmatrix = 1 / sumXmatrix;
409 for (
unsigned i = 0;
i < myCluster.size();
i++) {
410 w = myCluster[
i] * invsumXmatrix;
413 iter_IDmapF iwsum = wsum.find(idCluster[
i]);
414 if (iwsum == wsum.end())
415 wsum.insert(IDmapFvalue(idCluster[
i],
w * eventw));
417 iwsum->second +=
w * eventw;
419 iter_IDmapF iEwsum = Ewsum.find(idCluster[
i]);
420 if (iEwsum == Ewsum.end())
421 Ewsum.insert(IDmapFvalue(idCluster[
i], (
w * eventw *
energy * invsumXmatrix)));
423 iEwsum->second += (
w * eventw *
energy * invsumXmatrix);
431 template <
class IDdet>
432 typename MinL3AlgoUnivErr<IDdet>::IDmapF MinL3AlgoUnivErr<IDdet>::getSolution(
const bool resetsolution) {
435 for (iter_IDmapF
i = wsum.begin();
i != wsum.end();
i++) {
436 iter_IDmapF iEwsum = Ewsum.find(
i->first);
439 myValue = iEwsum->second /
i->second;
441 solution.insert(IDmapFvalue(
i->first, myValue));
452 template <
class IDdet>
453 void MinL3AlgoUnivErr<IDdet>::resetSolution() {
460 template <
class IDdet>
461 std::vector<float> MinL3AlgoUnivErr<IDdet>::recalibrateEvent(
const std::vector<float>& myCluster,
462 const std::vector<IDdet>& idCluster,
463 const IDmapF& newCalibration,
467 std::vector<float> newCluster(myCluster);
469 for (
unsigned i = 0;
i < myCluster.size();
i++) {
470 citer_IDmapF icalib = newCalibration.find(idCluster[
i]);
471 if (icalib != newCalibration.end()) {
472 newCluster[
i] *= icalib->second;
474 std::cout <<
"No calibration available for this element." << std::endl;
475 std::cout <<
" isol = " << isol <<
" iter = " << iter <<
" idCluster[i] = " << idCluster[
i] <<
"\n";
484 template <
class IDdet>
485 void MinL3AlgoUnivErr<IDdet>::registerPartialSolution(
const IDmapF& partialSolution)
491 for (citer_IDmapF cell = partialSolution.begin(); cell != partialSolution.end(); ++cell) {
492 IDdet
id = cell->first;
493 float corr = cell->second;
496 iter_IDmapI
cell2 = idToIndex.find(
id);
498 if (
cell2 == idToIndex.end()) {
502 sumPartSolu0.push_back(1);
503 sumPartSolu1.push_back(
corr);
504 sumPartSolu2.push_back(
corr *
corr);
505 idToIndex.insert(IDmapIvalue(
id, ++
lastIndex));
510 sumPartSolu0[
index] += 1;
519 template <
class IDdet>
520 float MinL3AlgoUnivErr<IDdet>::getError(IDdet
id)
const 524 citer_IDmapI cell = idToIndex.find(
id);
525 if (cell == idToIndex.end())
528 int i = cell->second;
529 int n = sumPartSolu0[
i];
533 float meanX = sumPartSolu1[
i] /
n;
534 float meanX2 = sumPartSolu2[
i] /
n;
536 error =
sqrt(fabs(meanX2 - meanX * meanX) / (
n - 1.));
544 template <
class IDdet>
545 typename MinL3AlgoUnivErr<IDdet>::IDmapF MinL3AlgoUnivErr<IDdet>::getError()
const 551 for (citer_IDmapI cell = idToIndex.begin(); cell != idToIndex.end(); ++cell) {
552 int i = cell->second;
553 int n = sumPartSolu0[
i];
557 float meanX = sumPartSolu1[
i] /
n;
558 float meanX2 = sumPartSolu2[
i] /
n;
560 error =
sqrt(fabs(meanX2 - meanX * meanX) / (
n - 1));
569 template <
class IDdet>
570 float MinL3AlgoUnivErr<IDdet>::getErrorQuality(IDdet
id)
const 574 citer_IDmapI cell = idToIndex.find(
id);
575 if (cell == idToIndex.end())
579 int i = cell->second;
580 int n = sumPartSolu0[
i];
581 if (
n < nOfSubsamples)
591 template <
class IDdet>
592 typename MinL3AlgoUnivErr<IDdet>::IDmapF MinL3AlgoUnivErr<IDdet>::getErrorQuality()
const 598 for (citer_IDmapI cell = idToIndex.begin(); cell != idToIndex.end(); ++cell) {
599 int i = cell->second;
600 int n = sumPartSolu0[
i];
602 if (
n < nOfSubsamples)
607 fractions.insert(IDmapFvalue(cell->first,
fraction));
615 template <
class IDdet>
616 int MinL3AlgoUnivErr<IDdet>::numberOfWrongErrors()
const 621 for (citer_IDmapI cell = idToIndex.begin(); cell != idToIndex.end(); ++cell) {
622 int i = cell->second;
623 int n = sumPartSolu0[
i];
625 if (
n < nOfSubsamples)
634 template <
class IDdet>
635 float MinL3AlgoUnivErr<IDdet>::getMeanPartialSolution(IDdet
id)
const 639 citer_IDmapI cell = idToIndex.find(
id);
640 if (cell == idToIndex.end())
643 int i = cell->second;
644 int n = sumPartSolu0[
i];
645 meanX = sumPartSolu1[
i] /
n;
652 template <
class IDdet>
653 typename MinL3AlgoUnivErr<IDdet>::IDmapF MinL3AlgoUnivErr<IDdet>::getMeanPartialSolution()
const 658 for (citer_IDmapI cell = idToIndex.begin(); cell != idToIndex.end(); ++cell) {
659 int i = cell->second;
660 int n = sumPartSolu0[
i];
661 float meanX = sumPartSolu1[
i] /
n;
662 solution.insert(IDmapFvalue(cell->first, meanX));
669 #endif // MinL3AlgoUnivErr_H
Container::value_type value_type
static std::atomic< unsigned int > lastIndex
void resetSolution()
reset for new iteration
Power< A, B >::type pow(const A &a, const B &b)