CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
JetDeltaRValueMapProducer.cc
Go to the documentation of this file.
1 /* \class JetDeltaRValueMapProducer
2  *
3  * Associates jets using delta-R matching, and writes out
4  * a valuemap of single float variables based on a StringObjectFunction.
5  * This is used for miniAOD.
6  *
7  * \author: Sal Rappoccio
8  *
9  *
10  * for more details about the cut syntax, see the documentation
11  * page below:
12  *
13  * https://twiki.cern.ch/twiki/bin/view/CMS/SWGuidePhysicsCutParser
14  *
15  *
16  */
17 
18 
20 
24 
30 
31 template < class T >
33 
34 public:
35 
37 
39  srcToken_( consumes< typename edm::View<T> >( params.getParameter<edm::InputTag>("src") ) ),
40  matchedToken_( consumes< typename edm::View<T> >( params.getParameter<edm::InputTag>( "matched" ) ) ),
41  distMax_( params.getParameter<double>( "distMax" ) ),
42  value_( params.existsAs<std::string>("value") ? params.getParameter<std::string>("value") : "" ),
43  values_( params.existsAs<std::vector<std::string> >("values") ? params.getParameter<std::vector<std::string> >("values") : std::vector<std::string>() ),
44  valueLabels_( params.existsAs<std::vector<std::string> >("valueLabels") ? params.getParameter<std::vector<std::string> >("valueLabels") : std::vector<std::string>() ),
45  lazyParser_( params.existsAs<bool>("lazyParser") ? params.getParameter<bool>("lazyParser") : false )
46  {
47  if( value_!="" )
48  {
49  evaluationMap_.insert( std::make_pair( value_, StringObjectFunction<T>( value_, lazyParser_ ) ) );
50  produces< JetValueMap >();
51  }
52 
53  if( valueLabels_.size()>0 || values_.size()>0 )
54  {
55  if( valueLabels_.size()==values_.size() )
56  {
57  multiValue_ = true;
58  for( size_t i=0; i<valueLabels_.size(); ++i)
59  {
60  evaluationMap_.insert( std::make_pair( valueLabels_[i], StringObjectFunction<T>( values_[i], lazyParser_ ) ) );
61  produces< JetValueMap >(valueLabels_[i]);
62  }
63  }
64  else
65  edm::LogWarning("ValueLabelMismatch") << "The number of value labels does not match the number of values. Values will not be evaluated.";
66  }
67  }
68 
70 
71 private:
72 
73  virtual void beginJob() override {}
74  virtual void endJob() override {}
75 
76  virtual void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override {
77 
79  iEvent.getByToken( srcToken_, h_jets1 );
81  iEvent.getByToken( matchedToken_, h_jets2 );
82 
83  std::vector<float> values( h_jets1->size(), -99999 );
84  std::map<std::string, std::vector<float> > valuesMap;
85  if( multiValue_ )
86  {
87  for( size_t i=0; i<valueLabels_.size(); ++i)
88  valuesMap.insert( std::make_pair( valueLabels_[i], std::vector<float>( h_jets1->size(), -99999 ) ) );
89  }
90  std::vector<bool> jets1_locks( h_jets1->size(), false );
91 
92  for ( typename edm::View<T>::const_iterator ibegin = h_jets2->begin(),
93  iend = h_jets2->end(), ijet = ibegin;
94  ijet != iend; ++ijet )
95  {
96  float matched_dR2 = 1e9;
97  int matched_index = -1;
98 
99  for ( typename edm::View<T>::const_iterator jbegin = h_jets1->begin(),
100  jend = h_jets1->end(), jjet = jbegin;
101  jjet != jend; ++jjet )
102  {
103  int index=jjet - jbegin;
104 
105  if( jets1_locks.at(index) ) continue; // skip jets that have already been matched
106 
107  float temp_dR2 = reco::deltaR2(ijet->eta(),ijet->phi(),jjet->eta(),jjet->phi());
108  if ( temp_dR2 < matched_dR2 )
109  {
110  matched_dR2 = temp_dR2;
111  matched_index = index;
112  }
113  }// end loop over src jets
114 
115  if( matched_index>=0 )
116  {
117  if ( matched_dR2 > distMax_*distMax_ )
118  edm::LogWarning("MatchedJetsFarApart") << "Matched jets separated by dR greater than distMax=" << distMax_;
119  else
120  {
121  jets1_locks.at(matched_index) = true;
122  if( value_!="" )
123  values.at(matched_index) = (evaluationMap_.at(value_))(*ijet);
124  if( multiValue_ )
125  {
126  for( size_t i=0; i<valueLabels_.size(); ++i)
127  valuesMap.at(valueLabels_[i]).at(matched_index) = (evaluationMap_.at(valueLabels_[i]))(*ijet);
128  }
129  }
130  }
131  }// end loop over matched jets
132 
133  if( value_!="" )
134  {
135  std::auto_ptr< JetValueMap > jetValueMap ( new JetValueMap() );
136 
137  JetValueMap::Filler filler(*jetValueMap);
138  filler.insert(h_jets1, values.begin(), values.end());
139  filler.fill();
140 
141  // put in Event
142  iEvent.put(jetValueMap);
143  }
144  if( multiValue_ )
145  {
146  for( size_t i=0; i<valueLabels_.size(); ++i)
147  {
148  std::auto_ptr< JetValueMap > jetValueMap ( new JetValueMap() );
149 
150  JetValueMap::Filler filler(*jetValueMap);
151  filler.insert(h_jets1, valuesMap.at(valueLabels_[i]).begin(), valuesMap.at(valueLabels_[i]).end());
152  filler.fill();
153 
154  // put in Event
155  iEvent.put(jetValueMap, valueLabels_[i]);
156  }
157  }
158  }
159 
162  const double distMax_;
164  const std::vector<std::string> values_;
165  const std::vector<std::string> valueLabels_;
166  const bool lazyParser_;
168  std::map<std::string, StringObjectFunction<T> > evaluationMap_;
169 };
170 
172 
int i
Definition: DBlmapReader.cc:9
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:449
const std::vector< std::string > values_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
edm::ValueMap< float > JetValueMap
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:52
virtual void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
const edm::EDGetTokenT< typename edm::View< T > > srcToken_
const std::vector< std::string > valueLabels_
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
const edm::EDGetTokenT< typename edm::View< T > > matchedToken_
int iEvent
Definition: GenABIO.cc:230
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:113
double deltaR2(const T1 &t1, const T2 &t2)
Definition: deltaR.h:36
std::map< std::string, StringObjectFunction< T > > evaluationMap_
virtual void beginJob() override
JetDeltaRValueMapProducer< reco::Jet > RecoJetDeltaRValueMapProducer
volatile std::atomic< bool > shutdown_flag false
long double T
JetDeltaRValueMapProducer(edm::ParameterSet const &params)