Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:11:41

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/parser/Grammar.h"
0026 #include "CommonTools/Utils/interface/parser/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   } else if (iPurpose == "CSC-segments") {
0069     addEntry("cscDetId().endcap()", 0, "ec");
0070     addEntry("cscDetId().station()", 0, "st");
0071     addEntry("cscDetId().ring()", 0, "rn");
0072   } else if (iPurpose == "BTLclusters" || iPurpose == "BTLrechits") {
0073     addEntry("energy()", 2, "E", "MeV");
0074     addEntry("time()", 2, "T", "ns");
0075   } else if (iPurpose == "ETLclusters" || iPurpose == "ETLrechits") {
0076     addEntry("energy()", 3, "E", "MeV");
0077     addEntry("time()", 2, "T", "ns");
0078   } else if (iPurpose == "HGCal Trigger Cell" || iPurpose == "HGCal Trigger Cluster") {
0079     addEntry("detId", 0);
0080   } else if (iPurpose == "CaloParticle") {
0081     addEntry("energy", 3);
0082     addEntry("pdgId()", 3, "pdgId");
0083     addEntry("simClusters().size()", 3, "SimClSize");
0084   } else if (iPurpose == "Trackster" || iPurpose == "Trackster hits" || iPurpose == "Trackster layers") {
0085     addEntry("raw_energy", 3, "E", "GeV");
0086     addEntry("barycenter().Eta()", 3, "eta");
0087     addEntry("barycenter().Phi()", 3, "phi");
0088   } else if (iPurpose == "HGCal MultiCluster") {
0089     addEntry("energy", 3);
0090   } else {
0091     // by the default  add pt, et, or energy
0092     bool x = addEntry("pt", 1);
0093     if (!x)
0094       x = addEntry("et", 1);
0095     if (!x)
0096       addEntry("energy", 1);
0097   }
0098 
0099   if (addEntry("eta", 2))
0100     addEntry("phi", 2);
0101 }
0102 
0103 bool FWItemValueGetter::addEntry(std::string iExpression, int iPrec, std::string iTitle, std::string iUnit) {
0104   using namespace boost::spirit::classic;
0105 
0106   reco::parser::ExpressionPtr tmpPtr;
0107   reco::parser::Grammar grammar(tmpPtr, m_type);
0108 
0109   if (m_type != edm::TypeWithDict() && !iExpression.empty()) {
0110     using namespace fireworks::expression;
0111 
0112     //Backwards compatibility with old format
0113     std::string temp = oldToNewFormat(iExpression);
0114 
0115     //now setup the parser
0116     try {
0117       if (parse(temp.c_str(), grammar.use_parser<1>() >> end_p, space_p).full) {
0118         m_entries.push_back(Entry(tmpPtr, iExpression, iUnit, iTitle.empty() ? iExpression : iTitle, iPrec));
0119         m_titleWidth = TMath::Max(m_titleWidth, (int)m_entries.back().m_title.size());
0120         return true;
0121       }
0122     } catch (const reco::parser::BaseException& e) {
0123       // std::cout <<"failed to parse "<<iExpression<<" because "<<reco::parser::baseExceptionWhat(e)<<std::endl;
0124     }
0125   }
0126   return false;
0127 }
0128 
0129 //______________________________________________________________________________
0130 
0131 double FWItemValueGetter::valueFor(const void* iObject, int idx) const {
0132   //  std::cout << " value for " << idx << "size " <<  m_entries.size() <<std::endl;
0133   edm::ObjectWithDict o(m_type, const_cast<void*>(iObject));
0134   return m_entries[idx].m_expr->value(o);
0135 }
0136 
0137 UInt_t FWItemValueGetter::precision(int idx) const { return m_entries[idx].m_precision; }
0138 
0139 std::vector<std::string> FWItemValueGetter::getTitles() const {
0140   std::vector<std::string> titles;
0141   titles.reserve(m_entries.size());
0142 
0143   for (std::vector<Entry>::const_iterator i = m_entries.begin(); i != m_entries.end(); ++i)
0144     titles.push_back((*i).m_title.empty() ? (*i).m_expression : (*i).m_title);
0145 
0146   return titles;
0147 }
0148 
0149 int FWItemValueGetter::numValues() const { return static_cast<int>(m_entries.size()); }
0150 //______________________________________________________________________________
0151 
0152 const std::string& FWItemValueGetter::getToolTip(const void* iObject) const {
0153   static std::string buff(128, 0);
0154   static std::string fs = "\n %*s = %.*f";
0155 
0156   edm::ObjectWithDict o(m_type, const_cast<void*>(iObject));
0157 
0158   int off = 0;
0159   for (std::vector<Entry>::const_iterator i = m_entries.begin(); i != m_entries.end(); ++i) {
0160     const Entry& e = *i;
0161     off += snprintf(&buff[off],
0162                     127,
0163                     fs.c_str(),
0164                     m_titleWidth,
0165                     e.m_title.c_str(),
0166                     e.m_precision ? (e.m_precision + 1) : 0,
0167                     e.m_expr->value(o));
0168   }
0169 
0170   // std::cout << buff;
0171   return buff;
0172 }