CMS 3D CMS Logo

CalibratableTest.cc
Go to the documentation of this file.
19 
20 using namespace edm;
21 using namespace pftools;
22 using namespace reco;
23 
25  debug_(0), nParticleWrites_(0), nParticleFails_(0), nEventWrites_(0), nEventFails_(0),
26  deltaRCandToSim_(0.4) {
27 
28  std::cout << __PRETTY_FUNCTION__ << std::endl;
29 
30  /* This procedure is GENERIC to storing any dictionary enable class in a ROOT tree. */
31  tree_ = fileservice_->make<TTree>("CalibratableTest", "");
32  calib_ = new Calibratable();
33  tree_->Branch("Calibratable", "pftools::Calibratable", &calib_, 32000, 2);
34 
35  inputTagCandidates_= parameters.getParameter<InputTag>("PFCandidates");
36  inputTagSimParticles_= parameters.getParameter<InputTag>("PFSimParticles");
37  inputTagClustersEcal_= parameters.getParameter<InputTag>("PFClustersEcal");
38  inputTagClustersHcal_= parameters.getParameter<InputTag>("PFClustersHcal");
39  deltaRCandToSim_ = parameters.getParameter<double>("deltaRCandToSim");
40  debug_= parameters.getParameter<int>("debug");
41 }
42 
44 
45 }
46 
48  if (debug_ > 1)
49  std::cout << __PRETTY_FUNCTION__ << "\n";
50 
51 }
52 
54  const edm::EventSetup& iSetup) {
55  if (debug_ > 1)
56  std::cout << __PRETTY_FUNCTION__ << "\n";
57 
58  //Extract new collection references
63 
68 
69  //Reset calibratable branch
70  thisEventPasses_ = true;
71  thisParticlePasses_ = true;
72  calib_->reset();
73 
74  if (debug_ > 1)
75  std::cout << "\tStarting event..."<< std::endl;
76 
79  PFClusterCollection clustersEcal = **clustersEcal_;
80  PFClusterCollection clustersHcal = **clustersHcal_;
81 
82  if (sims.size() == 0) {
83  std::cout << "\tNo sim particles found!" << std::endl;
84  thisEventPasses_ = false;
85  }
86 
87  //Find primary pions in the event
88  std::vector<unsigned> primarySims = findPrimarySimParticles(sims);
89  if (debug_) {
90  std::cout << "\tFound "<< primarySims.size()
91  << " primary sim particles, "<< (**pfCandidates_).size() << " pfCandidates\n";
92  }
93  for (std::vector<unsigned>::const_iterator cit = primarySims.begin(); cit
94  != primarySims.end(); ++cit) {
95  //There will be one write to the tree for each pion found.
96  if (debug_ > 1)
97  std::cout << "\t**Starting particle...**\n";
98  const PFSimParticle& sim = sims[*cit];
99  //One sim per calib =>
100  calib_->sim_numEvent_ = 1;
101  calib_->sim_isMC_ = true;
102  calib_->sim_energyEvent_ = sim.trajectoryPoint(PFTrajectoryPoint::ClosestApproach).momentum().E();
103  calib_->sim_phi_ = sim.trajectoryPoint(PFTrajectoryPoint::ClosestApproach).momentum().Phi();
104  calib_->sim_eta_ = sim.trajectoryPoint(PFTrajectoryPoint::ClosestApproach).momentum().Eta();
105 
106  if (sim.nTrajectoryPoints() > PFTrajectoryPoint::ECALEntrance) {
107  calib_->sim_etaEcal_ = sim.trajectoryPoint(PFTrajectoryPoint::ECALEntrance).positionREP().Eta();
108  calib_->sim_phiEcal_ = sim.trajectoryPoint(PFTrajectoryPoint::ECALEntrance).positionREP().Phi();
109  }
110  if (sim.nTrajectoryPoints() > PFTrajectoryPoint::HCALEntrance) {
111  calib_->sim_etaHcal_ = sim.trajectoryPoint(PFTrajectoryPoint::HCALEntrance).positionREP().Eta();
112  calib_->sim_phiHcal_ = sim.trajectoryPoint(PFTrajectoryPoint::HCALEntrance).positionREP().Phi();
113  }
114 
115  // Find candidates near this sim particle
116  std::vector<unsigned> matchingCands = findCandidatesInDeltaR(sim,
117  candidates, deltaRCandToSim_);
118  if (debug_ > 3)
119  std::cout << "\t\tFound candidates near sim, found "
120  << matchingCands.size()<< " of them.\n";
121  if (matchingCands.size() == 0)
122  thisParticlePasses_ = false;
123  for (std::vector<unsigned>::const_iterator mcIt = matchingCands.begin(); mcIt
124  != matchingCands.end(); ++mcIt) {
125  const PFCandidate& theCand = candidates[*mcIt];
126  extractCandidate(theCand);
127  }
128  //Finally,
130 
131  }
132 
133  delete pfCandidates_;
134  delete simParticles_;
135  delete clustersEcal_;
136  delete clustersHcal_;
137 
138  if (thisEventPasses_)
139  ++nEventWrites_;
140  else
141  ++nEventFails_;
142 
143 }
144 
146  const std::vector<PFSimParticle>& sims) {
147  std::vector<unsigned> answers;
148  unsigned index(0);
149  for (std::vector<PFSimParticle>::const_iterator cit = sims.begin(); cit
150  != sims.end(); ++cit) {
151  PFSimParticle theSim = *cit;
152  //TODO: what about rejected events?
153  if (theSim.motherId() >= 0)
154  continue;
155  int particleId = abs(theSim.pdgCode());
156  if (particleId != 211)
157  continue;
158  //TODO: ...particularly interacting pions?
159  if (theSim.daughterIds().size() > 0)
160  continue;
161  answers.push_back(index);
162  ++index;
163  }
164  return answers;
165 }
166 
168  if (debug_ > 3)
169  std::cout << "\tCandidate: "<< cand << "\n";
170 
171  //There may be several PFCandiates per sim particle. So we create a mini-class
172  //to represent each one. CandidateWrapper is defined in Calibratable.
173  //It's very easy to use, as we shall see...
174  CandidateWrapper cw;
175  cw.energy_ = cand.energy();
176  cw.eta_ = cand.eta();
177  cw.phi_ = cand.phi();
178  cw.type_ = cand.particleId();
179  cw.energyEcal_ = cand.ecalEnergy();
180  cw.energyHcal_ = cand.hcalEnergy();
181  if (debug_ > 4)
182  std::cout << "\t\tECAL energy = " << cand.ecalEnergy()
183  << ", HCAL energy = " << cand.hcalEnergy() << "\n";
184 
185  //Now, extract block elements from the pfCandidate:
186  PFCandidate::ElementsInBlocks eleInBlocks = cand.elementsInBlocks();
187  if (debug_ > 3)
188  std::cout << "\tLooping over elements in blocks, "
189  << eleInBlocks.size() << " of them."<< std::endl;
190  for (PFCandidate::ElementsInBlocks::iterator bit = eleInBlocks.begin(); bit
191  != eleInBlocks.end(); ++bit) {
192 
193  /*
194  * Find PFClusters associated with this candidate.
195  */
196 
197  //Extract block reference
198  PFBlockRef blockRef((*bit).first);
199  //Extract index
200  unsigned indexInBlock((*bit).second);
201  //Dereference the block (what a palava!)
202  const PFBlock& block = *blockRef;
203  //And finally get a handle on the elements
205  //get references to the candidate's track, ecal clusters and hcal clusters
206  switch (elements[indexInBlock].type()) {
207  case PFBlockElement::ECAL: {
208  reco::PFClusterRef clusterRef = elements[indexInBlock].clusterRef();
209  const PFCluster theRealCluster = *clusterRef;
210  CalibratableElement d(theRealCluster.energy(),
211  theRealCluster.positionREP().eta(), theRealCluster.positionREP().phi(), theRealCluster.layer() );
212  calib_->cluster_ecal_.push_back(d);
213  if (debug_ > 4)
214  std::cout << "\t\tECAL cluster: "<< theRealCluster << "\n";
215 
216  break;
217  }
218 
219  case PFBlockElement::HCAL: {
220  reco::PFClusterRef clusterRef = elements[indexInBlock].clusterRef();
221  const PFCluster theRealCluster = *clusterRef;
222  CalibratableElement d(theRealCluster.energy(),
223  theRealCluster.positionREP().eta(), theRealCluster.positionREP().phi(), theRealCluster.layer() );
224  calib_->cluster_hcal_.push_back(d);
225  if (debug_ > 4)
226  std::cout << "\t\tHCAL cluster: "<< theRealCluster << "\n";
227 
228  break;
229  }
230 
231  default:
232  if (debug_ > 3)
233  std::cout << "\t\tOther block type: "
234  << elements[indexInBlock].type() << "\n";
235  break;
236  }
237 
238  }
239  //For each candidate found,
240  calib_->cands_.push_back(cw);
241 }
242 
244  const PFSimParticle& pft, const std::vector<PFCandidate>& cands,
245  const double& deltaRCut) {
246 
247  unsigned index(0);
248  std::vector<unsigned> answers;
249 
250  double trEta = pft.trajectoryPoint(PFTrajectoryPoint::ECALEntrance).positionREP().Eta();
251  double trPhi = pft.trajectoryPoint(PFTrajectoryPoint::ECALEntrance).positionREP().Phi();
252 
253  for (std::vector<PFCandidate>::const_iterator cit = cands.begin(); cit
254  != cands.end(); ++cit) {
255 
256  PFCandidate cand = *cit;
257  double cEta = cand.eta();
258  double cPhi = cand.phi();
259 
260  if (deltaR(cEta, trEta, cPhi, trPhi) < deltaRCut) {
261  //accept
262  answers.push_back(index);
263  }
264 
265  ++index;
266  }
267  return answers;
268 }
269 
273  calib_->recompute();
274  if (debug_> 4) {
275  //print a summary
276  std::cout << *calib_;
277  }
278  tree_->Fill();
279  } else {
280  ++nParticleFails_;
281  }
282  if (debug_ > 1)
283  std::cout << "\t**Finished particle.**\n";
284  calib_->reset();
285 }
286 
288 
289  if (debug_> 0) {
290  std::cout << __PRETTY_FUNCTION__ << std::endl;
291 
292  std::cout << "\tnParticleWrites: "<< nParticleWrites_
293  << ", nParticleFails: "<< nParticleFails_ << "\n";
294  std::cout << "\tnEventWrites: "<< nEventWrites_ << ", nEventFails: "
295  << nEventFails_ << "\n";
296  std::cout << "Leaving "<< __PRETTY_FUNCTION__ << "\n";
297  }
298 
299 }
300 
301 double CalibratableTest::deltaR(const double& eta1, const double& eta2,
302  const double& phi1, const double& phi2) {
303  double deltaEta = fabs(eta1 - eta2);
304  double deltaPhi = fabs(phi1 - phi2);
305  if (deltaPhi > M_PI) {
306  deltaPhi = 2 * M_PI- deltaPhi;
307  }
308  return sqrt(pow(deltaEta, 2) + pow(deltaPhi, 2));
309 }
310 
311 
PFLayer::Layer layer() const
cluster layer, see PFLayer.h in this directory
Definition: PFCluster.cc:128
const REPPoint & positionREP() const
trajectory position in (rho, eta, phi) base
const std::vector< int > & daughterIds() const
Definition: PFSimParticle.h:44
type
Definition: HCALResponse.h:21
T getParameter(std::string const &) const
edm::InputTag inputTagClustersHcal_
edm::Service< TFileService > fileservice_
Wraps essential single particle calibration data ready for export to a Root file. ...
Definition: Calibratable.h:122
Particle flow cluster, see clustering algorithm in PFClusterAlgo.
Definition: PFCluster.h:47
virtual double eta() const final
momentum pseudorapidity
Small wrapper class for storing individual rechit and cluster information.
Definition: Calibratable.h:24
std::vector< unsigned > findPrimarySimParticles(const std::vector< reco::PFSimParticle > &sims)
virtual void fillTreeAndReset()
CalibratableTest(const edm::ParameterSet &)
edm::Handle< reco::PFCandidateCollection > * pfCandidates_
double deltaR(const double &eta1, const double &eta2, const double &phi1, const double &phi2)
std::vector< PFSimParticle > PFSimParticleCollection
collection of PFSimParticle objects
const edm::OwnVector< reco::PFBlockElement > & elements() const
Definition: PFBlock.h:107
T * make(const Args &...args) const
make new ROOT object
Definition: TFileService.h:64
int pdgCode() const
Definition: PFSimParticle.h:35
void extractCandidate(const reco::PFCandidate &cand)
pftools::Calibratable * calib_
virtual void beginJob() override
std::vector< ElementInBlock > ElementsInBlocks
Definition: PFCandidate.h:386
static const double deltaEta
Definition: CaloConstants.h:8
virtual double phi() const final
momentum azimuthal angle
edm::Handle< reco::PFSimParticleCollection > * simParticles_
edm::Handle< reco::PFClusterCollection > * clustersEcal_
T sqrt(T t)
Definition: SSEVec.h:18
const REPPoint & positionREP() const
cluster position: rho, eta, phi
Definition: PFCluster.h:97
void getCollection(edm::Handle< T > &c, const edm::InputTag &tag, const edm::Event &event) const
edm::InputTag inputTagCandidates_
std::vector< CandidateWrapper > cands_
Definition: Calibratable.h:196
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
std::vector< unsigned > findCandidatesInDeltaR(const reco::PFSimParticle &pft, const std::vector< reco::PFCandidate > &cands, const double &deltaR)
virtual void reset()
true particle for particle flow
Definition: PFSimParticle.h:19
double energy() const
cluster energy
Definition: PFCluster.h:82
virtual void endJob() override
std::vector< CalibratableElement > cluster_ecal_
Definition: Calibratable.h:184
#define M_PI
Definition: RunManager.h:28
edm::InputTag inputTagSimParticles_
General option file parser.
Definition: Calibratable.h:15
std::vector< reco::PFCandidate > PFCandidateCollection
collection of PFCandidates
const math::XYZTLorentzVector & momentum() const
4-momenta quadrivector
const reco::PFTrajectoryPoint & trajectoryPoint(unsigned index) const
Definition: PFTrack.h:102
virtual void recompute()
Definition: Calibratable.cc:45
std::vector< CalibratableElement > cluster_hcal_
Definition: Calibratable.h:184
edm::Handle< reco::PFClusterCollection > * clustersHcal_
int motherId() const
Definition: PFSimParticle.h:41
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:39
fixed size matrix
HLT enums.
std::vector< PFCluster > PFClusterCollection
collection of PFCluster objects
Definition: PFClusterFwd.h:9
unsigned int nTrajectoryPoints() const
Definition: PFTrack.h:90
virtual void analyze(const edm::Event &, const edm::EventSetup &) override
edm::InputTag inputTagClustersEcal_
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
Definition: event.py:1
Small wrapper class to store information associated with PFCandidates.
Definition: Calibratable.h:66
Block of elements.
Definition: PFBlock.h:30