Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-08-05 22:48:10

0001 // -*- C++ -*-
0002 //
0003 // Package:     Core
0004 // Class  :     FWItemValueGetter
0005 //
0006 // Implementation:
0007 //     <Notes on implementation>
0008 //
0009 // Original Author:  Chris Jones
0010 //         Created:  Sun Nov 30 16:15:43 EST 2008
0011 //
0012 
0013 // system include files
0014 #include <sstream>
0015 #include <cstdio>
0016 #include "TMath.h"
0017 #include "FWCore/Reflection/interface/BaseWithDict.h"
0018 #include "FWCore/Reflection/interface/ObjectWithDict.h"
0019 
0020 // user include files
0021 #include "Fireworks/Core/interface/FWItemValueGetter.h"
0022 
0023 #include "Fireworks/Core/interface/FWExpressionEvaluator.h"
0024 #include "Fireworks/Core/interface/FWExpressionException.h"
0025 #include "CommonTools/Utils/interface/Grammar.h"
0026 #include "CommonTools/Utils/interface/Exception.h"
0027 
0028 #include "Fireworks/Core/src/expressionFormatHelpers.h"
0029 
0030 //==============================================================================
0031 //==============================================================================
0032 //==============================================================================
0033 
0034 FWItemValueGetter::FWItemValueGetter(const edm::TypeWithDict& iType, const std::string& iPurpose)
0035     : m_type(iType), m_titleWidth(0) {
0036   if (!strcmp(iType.name().c_str(), "CaloTower")) {
0037     if (iPurpose == "ECal")
0038       addEntry("emEt", 1, "et", "GeV");
0039     else if (iPurpose == "HCal")
0040       addEntry("hadEt", 1, "et", "GeV");
0041     else if (iPurpose == "HCal Outer")
0042       addEntry("outerEt", 1, "et", "GeV");
0043   } else if (strstr(iPurpose.c_str(), "Beam Spot")) {
0044     addEntry("x0", 2, "x", "cm");
0045     addEntry("y0", 2, "y", "cm");
0046     addEntry("z0", 2, "z", "cm");
0047   } else if (strstr(iPurpose.c_str(), "Vertices")) {
0048     addEntry("x", 2, "x", "cm");
0049     addEntry("y", 2, "y", "cm");
0050     addEntry("z", 2, "z", "cm");
0051   } else if (strstr(iPurpose.c_str(), "Conversion")) {
0052     addEntry("pairMomentum().rho()", 1, "pt", "GeV");
0053     addEntry("pairMomentum().eta()", 2, "eta");
0054     addEntry("pairMomentum().phi()", 2, "phi");
0055   } else if (strstr(iPurpose.c_str(), "Candidate") || strstr(iPurpose.c_str(), "GenParticle")) {
0056     addEntry("pdgId()", 0, "pdg");
0057     bool x = addEntry("pt", 1);
0058     if (!x)
0059       x = addEntry("et", 1);
0060     if (!x)
0061       addEntry("energy", 1);
0062   } else if (iPurpose == "Jets") {
0063     addEntry("et", 1);
0064   } else if (iPurpose == "DT-segments") {
0065     addEntry("chamberId().wheel()", 0, "wh");
0066     addEntry("chamberId().station()", 0, "st");
0067     addEntry("chamberId().sector()", 0, "sc");
0068 
0069   } else if (iPurpose == "CSC-segments") {
0070     addEntry("cscDetId().endcap()", 0, "ec");
0071     addEntry("cscDetId().station()", 0, "st");
0072     addEntry("cscDetId().ring()", 0, "rn");
0073   } else if (iPurpose == "HGCal Trigger Cell" || iPurpose == "HGCal Trigger Cluster") {
0074     addEntry("detId", 0);
0075   } else if (iPurpose == "CaloParticle") {
0076     addEntry("energy", 3);
0077     addEntry("pdgId()", 3, "pdgId");
0078     addEntry("simClusters().size()", 3, "SimClSize");
0079   } else if (iPurpose == "Trackster" || iPurpose == "Trackster hits" || iPurpose == "Trackster layers") {
0080     addEntry("raw_energy", 3, "E", "GeV");
0081     addEntry("barycenter().Eta()", 3, "eta");
0082     addEntry("barycenter().Phi()", 3, "phi");
0083   } else if (iPurpose == "HGCal MultiCluster") {
0084     addEntry("energy", 3);
0085   } else {
0086     // by the default  add pt, et, or energy
0087     bool x = addEntry("pt", 1);
0088     if (!x)
0089       x = addEntry("et", 1);
0090     if (!x)
0091       addEntry("energy", 1);
0092   }
0093 
0094   if (addEntry("eta", 2))
0095     addEntry("phi", 2);
0096 }
0097 
0098 bool FWItemValueGetter::addEntry(std::string iExpression, int iPrec, std::string iTitle, std::string iUnit) {
0099   using namespace boost::spirit::classic;
0100 
0101   reco::parser::ExpressionPtr tmpPtr;
0102   reco::parser::Grammar grammar(tmpPtr, m_type);
0103 
0104   if (m_type != edm::TypeWithDict() && !iExpression.empty()) {
0105     using namespace fireworks::expression;
0106 
0107     //Backwards compatibility with old format
0108     std::string temp = oldToNewFormat(iExpression);
0109 
0110     //now setup the parser
0111     try {
0112       if (parse(temp.c_str(), grammar.use_parser<1>() >> end_p, space_p).full) {
0113         m_entries.push_back(Entry(tmpPtr, iExpression, iUnit, iTitle.empty() ? iExpression : iTitle, iPrec));
0114         m_titleWidth = TMath::Max(m_titleWidth, (int)m_entries.back().m_title.size());
0115         return true;
0116       }
0117     } catch (const reco::parser::BaseException& e) {
0118       // std::cout <<"failed to parse "<<iExpression<<" because "<<reco::parser::baseExceptionWhat(e)<<std::endl;
0119     }
0120   }
0121   return false;
0122 }
0123 
0124 //______________________________________________________________________________
0125 
0126 double FWItemValueGetter::valueFor(const void* iObject, int idx) const {
0127   //  std::cout << " value for " << idx << "size " <<  m_entries.size() <<std::endl;
0128   edm::ObjectWithDict o(m_type, const_cast<void*>(iObject));
0129   return m_entries[idx].m_expr->value(o);
0130 }
0131 
0132 UInt_t FWItemValueGetter::precision(int idx) const { return m_entries[idx].m_precision; }
0133 
0134 std::vector<std::string> FWItemValueGetter::getTitles() const {
0135   std::vector<std::string> titles;
0136   titles.reserve(m_entries.size());
0137 
0138   for (std::vector<Entry>::const_iterator i = m_entries.begin(); i != m_entries.end(); ++i)
0139     titles.push_back((*i).m_title.empty() ? (*i).m_expression : (*i).m_title);
0140 
0141   return titles;
0142 }
0143 
0144 int FWItemValueGetter::numValues() const { return static_cast<int>(m_entries.size()); }
0145 //______________________________________________________________________________
0146 
0147 const std::string& FWItemValueGetter::getToolTip(const void* iObject) const {
0148   static std::string buff(128, 0);
0149   static std::string fs = "\n %*s = %.*f";
0150 
0151   edm::ObjectWithDict o(m_type, const_cast<void*>(iObject));
0152 
0153   int off = 0;
0154   for (std::vector<Entry>::const_iterator i = m_entries.begin(); i != m_entries.end(); ++i) {
0155     const Entry& e = *i;
0156     off += snprintf(&buff[off],
0157                     127,
0158                     fs.c_str(),
0159                     m_titleWidth,
0160                     e.m_title.c_str(),
0161                     e.m_precision ? (e.m_precision + 1) : 0,
0162                     e.m_expr->value(o));
0163   }
0164 
0165   // std::cout << buff;
0166   return buff;
0167 }