Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

Cut.cxx

Go to the documentation of this file.
00001 // Cut.cxx: implementation of the Cut class.
00002 //
00003 // Original author: T. Burnett tburnett@u.washington.edu
00004 // $Header: /nfs/slac/g/glast/ground/cvs/merit/src/Cut.cxx,v 1.1.1.1 1999/12/20 22:29:12 burnett Exp $
00006 
00007 #include "Cut.h"
00008 
00009 #include <cstdio>
00010 
00012 // Construction/Destruction
00014 
00015 Cut::Cut(const Tuple&t,std::string item_name, Comparison op, double cut, std::string label) 
00016 :Analyze(t,item_name,label),m_cut(cut),m_op(op)
00017 {
00018 
00019     if( label.empty()) {
00020         std::string u;
00021         switch(op) {
00022         case LT: u = "<" ;break;
00023         case GT: u = ">" ;break;
00024         case EQ: u = "=" ;break; 
00025         case NE: u = "!=" ;break;
00026         case LE: u = "<=" ;break;
00027         case GE: u = ">=" ;break;
00028         }
00029         static char buf[20]; 
00030         sprintf(buf,"%g",m_cut);
00031         
00032         set_name(item_name+u+buf);
00033     }
00034 }
00035 Cut::Cut(const Tuple&t, const std::string& expression)
00036 {
00037     std::string::const_iterator b(expression.begin());
00038     parse(t, b, expression.end() );
00039 }
00040 
00041 Cut::Cut(const Tuple&t, std::string::const_iterator& it, std::string::iterator end)
00042 {
00043     parse(t, it, end);
00044     if( it != end && *it==')' ) it++;  // needed for caller
00045 }
00046 
00047 void Cut::parse(const Tuple&t, std::string::const_iterator& it, std::string::const_iterator end)
00048 {
00049     std::string::const_iterator begin = it; // save first
00050     // parse the expression: expect name OP value
00051     std::string name;
00052     std::string value;
00053     bool first=true;
00054     for(;   it != end && *it!=')'; ++it) {
00055         if( first ) {
00056             if( isalnum(*it) || *it=='_' )name += *it;
00057             else { 
00058                 switch( *it ) {
00059                 case '<': if( *(it+1)=='=' ) {m_op=LE; ++it;} else m_op=LT; break;
00060                 case '>': if( *(it+1)=='=' ) {m_op=GE; ++it;} else m_op=GT; break;
00061                 case '=': m_op=EQ; if(*(it+1)=='=')++it; break;
00062                 case '!': m_op=NE; ++it; break; // assume followed by =
00063                 default: std::cerr << "Unexpected character '" << *it << "' found" << std::endl;
00064                     exit(-1);
00065                 }
00066                 first = false;
00067             }
00068         } else {
00069             value+= *it;
00070         }
00071     }
00072     m_cut = atof(value.c_str());
00073     set_tuple_item(t,name);
00074     set_name(std::string(begin,it-begin));
00075 
00076 }
00077 
00078 bool Cut::apply()
00079 {
00080     switch(m_op) {
00081     case LT: return item()< m_cut ;break;
00082     case GT: return item()> m_cut ;break;
00083     case EQ: return item()==m_cut ;break; 
00084     case NE: return item()!=m_cut ;break;
00085     case LE: return item()<=m_cut ;break;
00086     case GE: return item()>=m_cut ;break;
00087     default: return false;
00088     }
00089 }
00090 

Generated at Wed Nov 21 12:20:39 2001 by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000