Cuts.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 #include "Rivet/Rivet.hh"
00004 #include "Rivet/Cuts.hh"
00005 
00006 using std::ostringstream;
00007 
00008 namespace Rivet {
00009 
00010 
00011   Cuts& Cuts::addCut(const string& quantity, const Comparison& comparison, const double value) {
00012     // If this quantity doesn't yet have any associated cuts, make a
00013     // default cut with effectively infinitely loose cuts.
00014     if (_cuts.find(quantity) == _cuts.end()) {
00015       _cuts[quantity] = BinaryCut();
00016     }
00017     // Combine cuts in the most restrictive way.
00018     switch (comparison) {
00019     case LESS_EQ:
00020       if (value < _cuts[quantity].getLowerThan()) {
00021         _cuts[quantity].setLowerThan(value);
00022       }
00023       break;
00024     case MORE_EQ:
00025       if (value > _cuts[quantity].getHigherThan()) {
00026         _cuts[quantity].setHigherThan(value);
00027       }
00028       break;
00029     case EQUAL:
00030       _cuts[quantity].setLowerThan(value);
00031       _cuts[quantity].setHigherThan(value);
00032       break;
00033     }
00034 
00035     // Allow method chaining.
00036     return *this;
00037   }
00038 
00039 
00040 
00041   bool Cuts::checkConsistency() const {
00042     for (Cuts::const_iterator c = begin(); c != end(); ++c) {
00043       if (c->second.getLowerThan() < c->second.getLowerThan()) {
00044         ostringstream msg;
00045         msg << "Constraints on " << c->first << " are incompatible: "
00046             << ">=" << c->second.getHigherThan() << " AND "
00047             << "<=" << c->second.getLowerThan();
00048         throw Error(msg.str());
00049       }
00050     }
00051     return true;
00052   }
00053 
00054 
00055 
00056   ostream& Cuts::print(ostream & os) const {
00057     for (Cuts::const_iterator cut = begin(); cut != end(); ++cut) {
00058       os << endl;
00059       os << setw(12) << std::left << cut->first;
00060       if (cut->second.getHigherThan() > -numeric_limits<double>::max()) {
00061         os << setw(3) << ">=";
00062         os << setw(10) << std::right << cut->second.getHigherThan();
00063       } else {
00064         os << setw(13) << "";
00065       }
00066       if (cut->second.getLowerThan() < numeric_limits<double>::max()) {
00067         os << setw(3) << "<=";
00068         os << setw(10) << std::right << cut->second.getLowerThan();
00069       } else {
00070         os << setw(13) << "";
00071       }
00072     }
00073     return os;
00074   }
00075 
00076 
00077 }