Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:23

0001 #ifndef PhysicsTools_FWLite_ScannerHelpers_h
0002 #define PhysicsTools_FWLite_ScannerHelpers_h
0003 
0004 #include <string>
0005 #include <FWCore/Reflection/interface/TypeWithDict.h>
0006 #include <TH1.h>
0007 #include <TH2.h>
0008 #include <TProfile.h>
0009 #include <TGraph.h>
0010 
0011 // AFAIK These includes are needed to build the dictionary
0012 // but must be kept hidden if CINT sees this class
0013 #if !defined(__CINT__) && !defined(__MAKECINT__)
0014 #include "CommonTools/Utils/interface/parser/ExpressionPtr.h"
0015 #include "CommonTools/Utils/interface/parser/SelectorPtr.h"
0016 #include "CommonTools/Utils/interface/parser/ExpressionBase.h"
0017 #include "CommonTools/Utils/interface/parser/SelectorBase.h"
0018 #endif
0019 
0020 namespace helper {
0021   /** Class helper::Parser has collection of useful static methods related to StringParser that can be exported to CINT via dictionaries.
0022      *  It's mosly meant to be used through the helper::ScannerBase class. */
0023   class Parser {
0024   public:
0025     /// Empty constructor, necessary for Root, useless
0026     Parser() {}
0027     /// Parse an expression for a given object type (using lazy parsing when resolving methods)
0028     static reco::parser::ExpressionPtr makeExpression(const std::string &expr, const edm::TypeWithDict &type);
0029     /// Parse an expression for a given object type (using lazy parsing when resolving methods)
0030     static reco::parser::SelectorPtr makeSelector(const std::string &expr, const edm::TypeWithDict &type);
0031     /// Perform the type deduction form edm::Wrapper<C> to C::value_type and resolves typedefs
0032     static edm::TypeWithDict elementType(const edm::TypeWithDict &wrapperType);
0033 
0034     //--- we define also dictionaries for these two trivial functions that should be callable even by CINT
0035     //    because otherwise sometimes CINT crashes even on the creation and destruction of edm::ObjectWithDict
0036     /// Make a edm::ObjectWithDict(type, obj) and pass it to the selector
0037     static bool test(const reco::parser::SelectorPtr &sel, const edm::TypeWithDict type, const void *obj);
0038     /// Make a edm::ObjectWithDict(type, obj) and pass it to the expression
0039     static double eval(const reco::parser::ExpressionPtr &sel, const edm::TypeWithDict type, const void *obj);
0040   };
0041 
0042   /** Class helper::ScannerBase: tool to print or histogram proprieties of an object using the dictionary,
0043      *  The class is generic, but each instance is restricted to the type of the objects to inspect, fixed at construction time. */
0044   class ScannerBase {
0045   public:
0046     /// Empty constructor, necessary for Root, DO NOT USE
0047     ScannerBase() {}
0048     /// Constructor taking as argument the type of the individual object passed to the scanner
0049     ScannerBase(const edm::TypeWithDict &objType) : objType_(objType), cuts_(1), ignoreExceptions_(false) {}
0050 
0051     /// Add an expression to be evaluated on the objects
0052     /// Returns false if the parsing failed
0053     bool addExpression(const char *expr);
0054     /// Clear all the expressions
0055     void clearExpressions() { exprs_.clear(); }
0056     /// Number of valid expressions
0057     size_t numberOfExpressions() const { return exprs_.size(); }
0058 
0059     /// Set the default cut that is applied to the events
0060     bool setCut(const char *cut);
0061     /// Clear the default cut
0062     void clearCut();
0063     /// Add one extra cut that can be evaluated separately (as if it was an expression)
0064     bool addExtraCut(const char *cut);
0065     /// Clear all extra cuts ;
0066     void clearExtraCuts();
0067     /// Number of extra cuts
0068     size_t numberOfExtraCuts() const { return cuts_.size() - 1; }
0069 
0070     /// Check if the object passes the default cut (icut=0) or any extra cut (icut = 1 .. numberOfExtraCuts)
0071     /// Obj must point to an object of the type used to construct this ScannerBase
0072     bool test(const void *obj, size_t icut = 0) const;
0073 
0074     /// Evaluate one of the expressions set in this scanner
0075     /// Obj must point to an object of the type used to construct this ScannerBase
0076     double eval(const void *obj, size_t iexpr = 0) const;
0077 
0078     /// Print out in a single row all the expressions for this object
0079     /// Obj must point to an object of the type used to construct this ScannerBase
0080     void print(const void *obj) const;
0081 
0082     /// Fill the histogram with the first expression evaluated on the object, if it passes the default cut
0083     /// Obj must point to an object of the type used to construct this ScannerBase
0084     void fill1D(const void *obj, TH1 *hist) const;
0085 
0086     /// Fill the histogram with (x,y) equal to the first and second expressions evaluated on the object, if it passes the default cut
0087     /// Obj must point to an object of the type used to construct this ScannerBase
0088     void fill2D(const void *obj, TH2 *hist2d) const;
0089 
0090     /// Fill the graph with (x,y) equal to the first and second expressions evaluated on the object, if it passes the default cut
0091     /// Obj must point to an object of the type used to construct this ScannerBase
0092     void fillGraph(const void *obj, TGraph *graph) const;
0093 
0094     /// Fill the profile histogram with (x,y) equal to the first and second expressions evaluated on the object, if it passes the default cut
0095     /// Obj must point to an object of the type used to construct this ScannerBase
0096     void fillProf(const void *obj, TProfile *prof) const;
0097 
0098     /// If set to true, exceptions are silently ignored: test will return 'false', and 'eval' will return 0.
0099     /// If left to the default value, false, for each exception a printout is done.
0100     void setIgnoreExceptions(bool ignoreThem) { ignoreExceptions_ = ignoreThem; }
0101 
0102   private:
0103     edm::TypeWithDict objType_;
0104     std::vector<reco::parser::ExpressionPtr> exprs_;
0105 
0106     /// The first one is the default cut, the others are the extra ones
0107     std::vector<reco::parser::SelectorPtr> cuts_;
0108 
0109     /// See setIgnoreExceptions to find out what this means
0110     bool ignoreExceptions_;
0111   };
0112 }  // namespace helper
0113 
0114 #endif