CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PFClusterProducer.cc
Go to the documentation of this file.
3 
4 #include <memory>
5 
8 
11 
15 
16 // Geometry
23 #include "TVector2.h"
24 
25 
26 using namespace std;
27 using namespace edm;
28 
29 namespace {
30  const std::string PositionCalcType__EGPositionCalc("EGPositionCalc");
31  const std::string PositionCalcType__EGPositionFormula("EGPositionFormula");
32  const std::string PositionCalcType__PFPositionCalc("PFPositionCalc");
34  bool sortByKey(const EEPSPair& a, const EEPSPair& b) {
35  return a.first < b.first;
36  }
37  double testPreshowerDistance(const edm::Ptr<reco::PFCluster>& eeclus,
38  const edm::Ptr<reco::PFCluster>& psclus) {
39  if( psclus.isNull() ) return -1.0;
40  /*
41  // commented out since PFCluster::layer() uses a lot of CPU
42  // and since
43  if( PFLayer::ECAL_ENDCAP != eeclus->layer() ) return -1.0;
44  if( PFLayer::PS1 != psclus->layer() &&
45  PFLayer::PS2 != psclus->layer() ) {
46  throw cms::Exception("testPreshowerDistance")
47  << "The second argument passed to this function was "
48  << "not a preshower cluster!" << std::endl;
49  }
50  */
51  const reco::PFCluster::REPPoint& pspos = psclus->positionREP();
52  const reco::PFCluster::REPPoint& eepos = eeclus->positionREP();
53  // lazy continue based on geometry
54  if( eeclus->z()*psclus->z() < 0 ) return -1.0;
55  const double dphi= std::abs(TVector2::Phi_mpi_pi(eepos.phi() -
56  pspos.phi()));
57  if( dphi > 0.6 ) return -1.0;
58  const double deta= std::abs(eepos.eta() - pspos.eta());
59  if( deta > 0.3 ) return -1.0;
60  return LinkByRecHit::testECALAndPSByRecHit(*eeclus,*psclus,false);
61  }
62 }
63 
65 {
66 
67  verbose_ =
68  iConfig.getUntrackedParameter<bool>("verbose",false);
69 
70  pfEnergyCalibration_ =
71  std::shared_ptr<PFEnergyCalibration>(new PFEnergyCalibration());
72 
73  // parameters for clustering
74 
75  double threshBarrel =
76  iConfig.getParameter<double>("thresh_Barrel");
77  double threshSeedBarrel =
78  iConfig.getParameter<double>("thresh_Seed_Barrel");
79 
80  double threshPtBarrel =
81  iConfig.getParameter<double>("thresh_Pt_Barrel");
82  double threshPtSeedBarrel =
83  iConfig.getParameter<double>("thresh_Pt_Seed_Barrel");
84 
85  double threshCleanBarrel =
86  iConfig.getParameter<double>("thresh_Clean_Barrel");
87  std::vector<double> minS4S1CleanBarrel =
88  iConfig.getParameter< std::vector<double> >("minS4S1_Clean_Barrel");
89 
90  double threshEndcap =
91  iConfig.getParameter<double>("thresh_Endcap");
92  double threshSeedEndcap =
93  iConfig.getParameter<double>("thresh_Seed_Endcap");
94 
95  double threshPtEndcap =
96  iConfig.getParameter<double>("thresh_Pt_Endcap");
97  double threshPtSeedEndcap =
98  iConfig.getParameter<double>("thresh_Pt_Seed_Endcap");
99 
100  double threshCleanEndcap =
101  iConfig.getParameter<double>("thresh_Clean_Endcap");
102  std::vector<double> minS4S1CleanEndcap =
103  iConfig.getParameter< std::vector<double> >("minS4S1_Clean_Endcap");
104 
105  double threshDoubleSpikeBarrel =
106  iConfig.getParameter<double>("thresh_DoubleSpike_Barrel");
107  double minS6S2DoubleSpikeBarrel =
108  iConfig.getParameter<double>("minS6S2_DoubleSpike_Barrel");
109  double threshDoubleSpikeEndcap =
110  iConfig.getParameter<double>("thresh_DoubleSpike_Endcap");
111  double minS6S2DoubleSpikeEndcap =
112  iConfig.getParameter<double>("minS6S2_DoubleSpike_Endcap");
113 
114  int nNeighbours =
115  iConfig.getParameter<int>("nNeighbours");
116 
117 // double posCalcP1 =
118 // iConfig.getParameter<double>("posCalcP1");
119 
120  int posCalcNCrystal =
121  iConfig.getParameter<int>("posCalcNCrystal");
122 
123  double showerSigma =
124  iConfig.getParameter<double>("showerSigma");
125 
126  bool useCornerCells =
127  iConfig.getParameter<bool>("useCornerCells");
128 
129  bool cleanRBXandHPDs =
130  iConfig.getParameter<bool>("cleanRBXandHPDs");
131 
132 
133  clusterAlgo_.setThreshBarrel( threshBarrel );
134  clusterAlgo_.setThreshSeedBarrel( threshSeedBarrel );
135 
136  clusterAlgo_.setThreshPtBarrel( threshPtBarrel );
137  clusterAlgo_.setThreshPtSeedBarrel( threshPtSeedBarrel );
138 
139  clusterAlgo_.setThreshCleanBarrel(threshCleanBarrel);
140  clusterAlgo_.setS4S1CleanBarrel(minS4S1CleanBarrel);
141 
142  clusterAlgo_.setThreshDoubleSpikeBarrel( threshDoubleSpikeBarrel );
143  clusterAlgo_.setS6S2DoubleSpikeBarrel( minS6S2DoubleSpikeBarrel );
144 
145  clusterAlgo_.setThreshEndcap( threshEndcap );
146  clusterAlgo_.setThreshSeedEndcap( threshSeedEndcap );
147 
148  clusterAlgo_.setThreshPtEndcap( threshPtEndcap );
149  clusterAlgo_.setThreshPtSeedEndcap( threshPtSeedEndcap );
150 
151  clusterAlgo_.setThreshCleanEndcap(threshCleanEndcap);
152  clusterAlgo_.setS4S1CleanEndcap(minS4S1CleanEndcap);
153 
154  clusterAlgo_.setThreshDoubleSpikeEndcap( threshDoubleSpikeEndcap );
155  clusterAlgo_.setS6S2DoubleSpikeEndcap( minS6S2DoubleSpikeEndcap );
156 
157  clusterAlgo_.setNNeighbours( nNeighbours );
158 
159  // setup the position calculation (only affects ECAL position correction)
160  std::string poscalctype = PositionCalcType__PFPositionCalc;
161  if( iConfig.existsAs<std::string>("PositionCalcType") ) {
162  poscalctype = iConfig.getParameter<std::string>("PositionCalcType");
163  }
164  if( poscalctype == PositionCalcType__EGPositionCalc ) {
165  clusterAlgo_.setPositionCalcType(PFClusterAlgo::EGPositionCalc);
166  edm::ParameterSet pc_config =
167  iConfig.getParameterSet("PositionCalcConfig");
168  clusterAlgo_.setEGammaPosCalc(pc_config);
169  } else if( poscalctype == PositionCalcType__EGPositionFormula) {
170  clusterAlgo_.setPositionCalcType(PFClusterAlgo::EGPositionFormula);
171  edm::ParameterSet pc_config =
172  iConfig.getParameterSet("PositionCalcConfig");
173  double w0 = pc_config.getParameter<double>("W0");
174  clusterAlgo_.setPosCalcW0(w0);
175  } else if( poscalctype == PositionCalcType__PFPositionCalc) {
176  clusterAlgo_.setPositionCalcType(PFClusterAlgo::PFPositionCalc);
177  } else {
178  throw cms::Exception("InvalidClusteringType")
179  << "You have not chosen a valid position calculation type,"
180  << " please choose from \""
181  << PositionCalcType__EGPositionCalc << "\", \""
182  << PositionCalcType__EGPositionFormula << "\", or \""
183  << PositionCalcType__PFPositionCalc << "\"!";
184  }
185 
186  // p1 set to the minimum rechit threshold:
187  double posCalcP1 = threshBarrel<threshEndcap ? threshBarrel:threshEndcap;
188  clusterAlgo_.setPosCalcP1( posCalcP1 );
189  clusterAlgo_.setPosCalcNCrystal( posCalcNCrystal );
190  clusterAlgo_.setShowerSigma( showerSigma );
191 
192  clusterAlgo_.setUseCornerCells( useCornerCells );
193  clusterAlgo_.setCleanRBXandHPDs( cleanRBXandHPDs);
194 
195  int dcormode =
196  iConfig.getParameter<int>("depthCor_Mode");
197 
198  double dcora =
199  iConfig.getParameter<double>("depthCor_A");
200  double dcorb =
201  iConfig.getParameter<double>("depthCor_B");
202  double dcorap =
203  iConfig.getParameter<double>("depthCor_A_preshower");
204  double dcorbp =
205  iConfig.getParameter<double>("depthCor_B_preshower");
206 
207  if( dcormode !=0 )
209  dcora, dcorb,
210  dcorap, dcorbp );
211 
212 
213  geom = NULL;
214  // access to the collections of rechits from the various detectors:
215 
216 
217  inputTagPFRecHits_ =
218  iConfig.getParameter<InputTag>("PFRecHits");
219  //---ab
220  produces_eeps = iConfig.existsAs<InputTag>("PFClustersPS");
221  if( produces_eeps ) {
222  inputTagPFClustersPS_ =
223  iConfig.getParameter<InputTag>("PFClustersPS");
224  threshPFClusterES_ = iConfig.getParameter<double>("thresh_Preshower");
225  applyCrackCorrections_ =
226  iConfig.getParameter<bool>("applyCrackCorrections");
227  if (inputTagPFClustersPS_.label().empty()) {
228  produces_eeps = false;
229  } else {
230  produces<reco::PFCluster::EEtoPSAssociation>();
231  }
232  }
233 
234  //inputTagClusterCollectionName_ = iConfig.getParameter<string>("PFClusterCollectionName");
235 
236  // produces<reco::PFClusterCollection>(inputTagClusterCollectionName_);
237  produces<reco::PFClusterCollection>();
238  produces<reco::PFRecHitCollection>("Cleaned");
239 
240  //---ab
241 }
242 
243 
244 
246 
247 
249  edm::EventSetup const& iE) {
251  if( geom == NULL || (geom->cacheIdentifier() != temp.cacheIdentifier()) ) {
252  geom = &temp;
254  geom->get(cgeom);
255  clusterAlgo_.setEBGeom(cgeom->getSubdetectorGeometry(DetId::Ecal,
256  EcalBarrel));
257  clusterAlgo_.setEEGeom(cgeom->getSubdetectorGeometry(DetId::Ecal,
258  EcalEndcap));
259  clusterAlgo_.setPreshowerGeom(cgeom->getSubdetectorGeometry(DetId::Ecal,
260  EcalPreshower));
261  }
262 }
263 
265  const edm::EventSetup& iSetup) {
266 
267 
269  edm::Handle< edm::View<reco::PFCluster> > psclustersHandle;
270 
271  // access the rechits in the event
272  bool found = iEvent.getByLabel( inputTagPFRecHits_, rechitsHandle );
273 
274  if(!found ) {
275 
276  ostringstream err;
277  err<<"cannot find rechits: "<<inputTagPFRecHits_;
278  LogError("PFClusterProducer")<<err.str()<<endl;
279 
280  throw cms::Exception( "MissingProduct", err.str());
281  }
282 
283  // do clustering
284  clusterAlgo_.doClustering( rechitsHandle );
285 
286  if( verbose_ ) {
287  LogInfo("PFClusterProducer")
288  <<" clusters --------------------------------- "<<endl
289  <<clusterAlgo_<<endl;
290  }
291 
292  std::auto_ptr< std::vector<reco::PFCluster> > clusters =
293  clusterAlgo_.clusters();
294 
295  if( produces_eeps ) {
296  iEvent.getByLabel( inputTagPFClustersPS_, psclustersHandle );
297  // associate psclusters to ecal rechits
298  std::auto_ptr<reco::PFCluster::EEtoPSAssociation>
299  outEEPS( new reco::PFCluster::EEtoPSAssociation );
300  // make the association map of ECAL clusters to preshower clusters
301  edm::PtrVector<reco::PFCluster> clusterPtrsPS =
302  psclustersHandle->ptrVector();
303  double dist = -1.0, min_dist = -1.0;
304  // match PS clusters to EE clusters, minimum distance to EE is ensured
305  // since the inner loop is over the EE clusters
306  for( const auto& psclus : clusterPtrsPS ) {
307  if( psclus->energy() < threshPFClusterES_ ) continue;
308  switch( psclus->layer() ) { // just in case this isn't the ES...
309  case PFLayer::PS1:
310  case PFLayer::PS2:
311  break;
312  default:
313  continue;
314  }
315  edm::Ptr<reco::PFCluster> eematch,eeclus;
316  dist = min_dist = -1.0; // reset
317  for( size_t ic = 0; ic < clusters->size(); ++ic ) {
318  eeclus = edm::Ptr<reco::PFCluster>(clusters.get(),ic);
319  if( eeclus->layer() != PFLayer::ECAL_ENDCAP ) continue;
320  dist = testPreshowerDistance(eeclus,psclus);
321  if( dist == -1.0 || (min_dist != -1.0 && dist > min_dist) ) continue;
322  if( dist < min_dist || min_dist == -1.0 ) {
323  eematch = eeclus;
324  min_dist = dist;
325  }
326  } // loop on EE clusters
327  if( eematch.isNonnull() ) {
328  outEEPS->push_back(std::make_pair(eematch.key(),psclus));
329  }
330  } // loop on PS clusters
331  //sort the ps association
332  std::sort(outEEPS->begin(),outEEPS->end(),sortByKey);
333 
334  std::vector<double> ps1_energies,ps2_energies;
335  double ePS1, ePS2;
336  for( size_t ic = 0 ; ic < clusters->size(); ++ic ) {
337  ps1_energies.clear();
338  ps2_energies.clear();
339  ePS1 = ePS2 = 0;
340  auto ee_key_val = std::make_pair(ic,edm::Ptr<reco::PFCluster>());
341  const auto clustops = std::equal_range(outEEPS->begin(),
342  outEEPS->end(),
343  ee_key_val,
344  sortByKey);
345  for( auto i_ps = clustops.first; i_ps != clustops.second; ++i_ps) {
346  edm::Ptr<reco::PFCluster> psclus(i_ps->second);
347  switch( psclus->layer() ) {
348  case PFLayer::PS1:
349  ps1_energies.push_back(psclus->energy());
350  break;
351  case PFLayer::PS2:
352  ps2_energies.push_back(psclus->energy());
353  break;
354  default:
355  break;
356  }
357  }
358  const double eCorr=
359  pfEnergyCalibration_->energyEm(clusters->at(ic),
360  ps1_energies,ps2_energies,
361  ePS1,ePS2,
362  applyCrackCorrections_);
363  clusters->at(ic).setCorrectedEnergy(eCorr);
364  }
365 
366  iEvent.put(outEEPS);
367  }
368 
369  // get clusters out of the clustering algorithm
370  // and put them in the event. There is no copy.
371  //auto_ptr< vector<reco::PFCluster> > outClusters( clusters );
372  auto_ptr< vector<reco::PFRecHit> > recHitsCleaned ( clusterAlgo_.rechitsCleaned() );
374  iEvent.put( clusters );
375  iEvent.put( recHitsCleaned, "Cleaned" );
376 }
377 
378 
379 
T getParameter(std::string const &) const
unsigned long long cacheIdentifier() const
T getUntrackedParameter(std::string const &, T const &) const
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:184
static void setDepthCorParameters(int mode, double a, double b, double ap, double bp)
Definition: PFCluster.h:101
static double testECALAndPSByRecHit(const reco::PFCluster &clusterECAL, const reco::PFCluster &clusterPS, bool debug=false)
ROOT::Math::PositionVector3D< ROOT::Math::CylindricalEta3D< Double32_t > > REPPoint
Definition: PFCluster.h:47
#define NULL
Definition: scimark2.h:8
double Phi_mpi_pi(double x)
Definition: JetUtil.h:24
virtual void produce(edm::Event &, const edm::EventSetup &)
bool isNonnull() const
Checks for non-null.
Definition: Ptr.h:152
int iEvent
Definition: GenABIO.cc:243
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:116
std::vector< std::pair< CaloClusterPtr::key_type, edm::Ptr< PFCluster > > > EEtoPSAssociation
Definition: PFCluster.h:46
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
Container::value_type value_type
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:390
key_type key() const
Definition: Ptr.h:169
virtual void beginLuminosityBlock(edm::LuminosityBlock const &iL, edm::EventSetup const &iE)
ParameterSet const & getParameterSet(std::string const &) const
const T & get() const
Definition: EventSetup.h:55
double b
Definition: hdecay.h:120
bool isNull() const
Checks for null.
Definition: Ptr.h:148
double a
Definition: hdecay.h:121
PFClusterProducer(const edm::ParameterSet &)
static float eCorr(int ieta, int iphi, double ampl, int runnum)
Ugly hack to apply energy corrections to some HB- cells.