CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CalibratableTest.cc
Go to the documentation of this file.
19 
23 
24 using namespace edm;
25 using namespace pftools;
26 using namespace reco;
27 
29  debug_(0), nParticleWrites_(0), nParticleFails_(0), nEventWrites_(0), nEventFails_(0),
30  deltaRCandToSim_(0.4) {
31 
32  std::cout << __PRETTY_FUNCTION__ << std::endl;
33 
34  /* This procedure is GENERIC to storing any dictionary enable class in a ROOT tree. */
35  tree_ = fileservice_->make<TTree>("CalibratableTest", "");
36  calib_ = new Calibratable();
37  tree_->Branch("Calibratable", "pftools::Calibratable", &calib_, 32000, 2);
38 
39  inputTagCandidates_= parameters.getParameter<InputTag>("PFCandidates");
40  inputTagSimParticles_= parameters.getParameter<InputTag>("PFSimParticles");
41  inputTagClustersEcal_= parameters.getParameter<InputTag>("PFClustersEcal");
42  inputTagClustersHcal_= parameters.getParameter<InputTag>("PFClustersHcal");
43  deltaRCandToSim_ = parameters.getParameter<double>("deltaRCandToSim");
44  debug_= parameters.getParameter<int>("debug");
45 }
46 
48 
49 }
50 
52  if (debug_ > 1)
53  std::cout << __PRETTY_FUNCTION__ << "\n";
54 
55 }
56 
58  const edm::EventSetup& iSetup) {
59  if (debug_ > 1)
60  std::cout << __PRETTY_FUNCTION__ << "\n";
61 
62  //Extract new collection references
67 
72 
73  //Reset calibratable branch
74  thisEventPasses_ = true;
75  thisParticlePasses_ = true;
76  calib_->reset();
77 
78  if (debug_ > 1)
79  std::cout << "\tStarting event..."<< std::endl;
80 
82  PFCandidateCollection candidates = **pfCandidates_;
83  PFClusterCollection clustersEcal = **clustersEcal_;
84  PFClusterCollection clustersHcal = **clustersHcal_;
85 
86  if (sims.size() == 0) {
87  std::cout << "\tNo sim particles found!" << std::endl;
88  thisEventPasses_ = false;
89  }
90 
91  //Find primary pions in the event
92  std::vector<unsigned> primarySims = findPrimarySimParticles(sims);
93  if (debug_) {
94  std::cout << "\tFound "<< primarySims.size()
95  << " primary sim particles, "<< (**pfCandidates_).size() << " pfCandidates\n";
96  }
97  for (std::vector<unsigned>::const_iterator cit = primarySims.begin(); cit
98  != primarySims.end(); ++cit) {
99  //There will be one write to the tree for each pion found.
100  if (debug_ > 1)
101  std::cout << "\t**Starting particle...**\n";
102  const PFSimParticle& sim = sims[*cit];
103  //One sim per calib =>
104  calib_->sim_numEvent_ = 1;
105  calib_->sim_isMC_ = true;
106  calib_->sim_energyEvent_ = sim.trajectoryPoint(PFTrajectoryPoint::ClosestApproach).momentum().E();
107  calib_->sim_phi_ = sim.trajectoryPoint(PFTrajectoryPoint::ClosestApproach).momentum().Phi();
108  calib_->sim_eta_ = sim.trajectoryPoint(PFTrajectoryPoint::ClosestApproach).momentum().Eta();
109 
110  if (sim.nTrajectoryPoints() > PFTrajectoryPoint::ECALEntrance) {
111  calib_->sim_etaEcal_ = sim.trajectoryPoint(PFTrajectoryPoint::ECALEntrance).positionREP().Eta();
112  calib_->sim_phiEcal_ = sim.trajectoryPoint(PFTrajectoryPoint::ECALEntrance).positionREP().Phi();
113  }
114  if (sim.nTrajectoryPoints() > PFTrajectoryPoint::HCALEntrance) {
115  calib_->sim_etaHcal_ = sim.trajectoryPoint(PFTrajectoryPoint::HCALEntrance).positionREP().Eta();
116  calib_->sim_phiHcal_ = sim.trajectoryPoint(PFTrajectoryPoint::HCALEntrance).positionREP().Phi();
117  }
118 
119  // Find candidates near this sim particle
120  std::vector<unsigned> matchingCands = findCandidatesInDeltaR(sim,
121  candidates, deltaRCandToSim_);
122  if (debug_ > 3)
123  std::cout << "\t\tFound candidates near sim, found "
124  << matchingCands.size()<< " of them.\n";
125  if (matchingCands.size() == 0)
126  thisParticlePasses_ = false;
127  for (std::vector<unsigned>::const_iterator mcIt = matchingCands.begin(); mcIt
128  != matchingCands.end(); ++mcIt) {
129  const PFCandidate& theCand = candidates[*mcIt];
130  extractCandidate(theCand);
131  }
132  //Finally,
134 
135  }
136 
137  delete pfCandidates_;
138  delete simParticles_;
139  delete clustersEcal_;
140  delete clustersHcal_;
141 
142  if (thisEventPasses_)
143  ++nEventWrites_;
144  else
145  ++nEventFails_;
146 
147 }
148 
150  const std::vector<PFSimParticle>& sims) {
151  std::vector<unsigned> answers;
152  unsigned index(0);
153  for (std::vector<PFSimParticle>::const_iterator cit = sims.begin(); cit
154  != sims.end(); ++cit) {
155  PFSimParticle theSim = *cit;
156  //TODO: what about rejected events?
157  if (theSim.motherId() >= 0)
158  continue;
159  int particleId = abs(theSim.pdgCode());
160  if (particleId != 211)
161  continue;
162  //TODO: ...particularly interacting pions?
163  if (theSim.daughterIds().size() > 0)
164  continue;
165  answers.push_back(index);
166  ++index;
167  }
168  return answers;
169 }
170 
172  if (debug_ > 3)
173  std::cout << "\tCandidate: "<< cand << "\n";
174 
175  //There may be several PFCandiates per sim particle. So we create a mini-class
176  //to represent each one. CandidateWrapper is defined in Calibratable.
177  //It's very easy to use, as we shall see...
178  CandidateWrapper cw;
179  cw.energy_ = cand.energy();
180  cw.eta_ = cand.eta();
181  cw.phi_ = cand.phi();
182  cw.type_ = cand.particleId();
183  cw.energyEcal_ = cand.ecalEnergy();
184  cw.energyHcal_ = cand.hcalEnergy();
185  if (debug_ > 4)
186  std::cout << "\t\tECAL energy = " << cand.ecalEnergy()
187  << ", HCAL energy = " << cand.hcalEnergy() << "\n";
188 
189  //Now, extract block elements from the pfCandidate:
190  PFCandidate::ElementsInBlocks eleInBlocks = cand.elementsInBlocks();
191  if (debug_ > 3)
192  std::cout << "\tLooping over elements in blocks, "
193  << eleInBlocks.size() << " of them."<< std::endl;
194  for (PFCandidate::ElementsInBlocks::iterator bit = eleInBlocks.begin(); bit
195  != eleInBlocks.end(); ++bit) {
196 
197  /*
198  * Find PFClusters associated with this candidate.
199  */
200 
201  //Extract block reference
202  PFBlockRef blockRef((*bit).first);
203  //Extract index
204  unsigned indexInBlock((*bit).second);
205  //Dereference the block (what a palava!)
206  const PFBlock& block = *blockRef;
207  //And finally get a handle on the elements
209  //get references to the candidate's track, ecal clusters and hcal clusters
210  switch (elements[indexInBlock].type()) {
211  case PFBlockElement::ECAL: {
212  reco::PFClusterRef clusterRef = elements[indexInBlock].clusterRef();
213  const PFCluster theRealCluster = *clusterRef;
214  CalibratableElement d(theRealCluster.energy(),
215  theRealCluster.positionREP().eta(), theRealCluster.positionREP().phi(), theRealCluster.layer() );
216  calib_->cluster_ecal_.push_back(d);
217  if (debug_ > 4)
218  std::cout << "\t\tECAL cluster: "<< theRealCluster << "\n";
219 
220  break;
221  }
222 
223  case PFBlockElement::HCAL: {
224  reco::PFClusterRef clusterRef = elements[indexInBlock].clusterRef();
225  const PFCluster theRealCluster = *clusterRef;
226  CalibratableElement d(theRealCluster.energy(),
227  theRealCluster.positionREP().eta(), theRealCluster.positionREP().phi(), theRealCluster.layer() );
228  calib_->cluster_hcal_.push_back(d);
229  if (debug_ > 4)
230  std::cout << "\t\tHCAL cluster: "<< theRealCluster << "\n";
231 
232  break;
233  }
234 
235  default:
236  if (debug_ > 3)
237  std::cout << "\t\tOther block type: "
238  << elements[indexInBlock].type() << "\n";
239  break;
240  }
241 
242  }
243  //For each candidate found,
244  calib_->cands_.push_back(cw);
245 }
246 
248  const PFSimParticle& pft, const std::vector<PFCandidate>& cands,
249  const double& deltaRCut) {
250 
251  unsigned index(0);
252  std::vector<unsigned> answers;
253 
254  double trEta = pft.trajectoryPoint(PFTrajectoryPoint::ECALEntrance).positionREP().Eta();
255  double trPhi = pft.trajectoryPoint(PFTrajectoryPoint::ECALEntrance).positionREP().Phi();
256 
257  for (std::vector<PFCandidate>::const_iterator cit = cands.begin(); cit
258  != cands.end(); ++cit) {
259 
260  PFCandidate cand = *cit;
261  double cEta = cand.eta();
262  double cPhi = cand.phi();
263 
264  if (deltaR(cEta, trEta, cPhi, trPhi) < deltaRCut) {
265  //accept
266  answers.push_back(index);
267  }
268 
269  ++index;
270  }
271  return answers;
272 }
273 
277  calib_->recompute();
278  if (debug_> 4) {
279  //print a summary
280  std::cout << *calib_;
281  }
282  tree_->Fill();
283  } else {
284  ++nParticleFails_;
285  }
286  if (debug_ > 1)
287  std::cout << "\t**Finished particle.**\n";
288  calib_->reset();
289 }
290 
292 
293  if (debug_> 0) {
294  std::cout << __PRETTY_FUNCTION__ << std::endl;
295 
296  std::cout << "\tnParticleWrites: "<< nParticleWrites_
297  << ", nParticleFails: "<< nParticleFails_ << "\n";
298  std::cout << "\tnEventWrites: "<< nEventWrites_ << ", nEventFails: "
299  << nEventFails_ << "\n";
300  std::cout << "Leaving "<< __PRETTY_FUNCTION__ << "\n";
301  }
302 
303 }
304 
305 double CalibratableTest::deltaR(const double& eta1, const double& eta2,
306  const double& phi1, const double& phi2) {
307  double deltaEta = fabs(eta1 - eta2);
308  double deltaPhi = fabs(phi1 - phi2);
309  if (deltaPhi > M_PI) {
310  deltaPhi = 2 * M_PI- deltaPhi;
311  }
312  return sqrt(pow(deltaEta, 2) + pow(deltaPhi, 2));
313 }
314 
315 
PFLayer::Layer layer() const
cluster layer, see PFLayer.h in this directory
Definition: PFCluster.cc:81
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:22
T getParameter(std::string const &) const
dictionary parameters
Definition: Parameters.py:2
edm::InputTag inputTagClustersHcal_
edm::Service< TFileService > fileservice_
Wraps essential single particle calibration data ready for export to a Root file. ...
Definition: Calibratable.h:122
virtual void endJob()
Particle flow cluster, see clustering algorithm in PFClusterAlgo.
Definition: PFCluster.h:42
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)
#define abs(x)
Definition: mlp_lapack.h:159
list elements
Definition: asciidump.py:414
std::vector< PFSimParticle > PFSimParticleCollection
collection of PFSimParticle objects
const edm::OwnVector< reco::PFBlockElement > & elements() const
Definition: PFBlock.h:107
int pdgCode() const
Definition: PFSimParticle.h:35
void extractCandidate(const reco::PFCandidate &cand)
pftools::Calibratable * calib_
Definition: sim.h:19
virtual double eta() const
momentum pseudorapidity
std::vector< ElementInBlock > ElementsInBlocks
Definition: PFCandidate.h:337
edm::Handle< reco::PFSimParticleCollection > * simParticles_
edm::Handle< reco::PFClusterCollection > * clustersEcal_
T sqrt(T t)
Definition: SSEVec.h:46
const REPPoint & positionREP() const
cluster position: rho, eta, phi
Definition: PFCluster.h:76
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
std::vector< unsigned > findCandidatesInDeltaR(const reco::PFSimParticle &pft, const std::vector< reco::PFCandidate > &cands, const double &deltaR)
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
virtual void reset()
true particle for particle flow
Definition: PFSimParticle.h:19
double energy() const
cluster energy
Definition: PFCluster.h:73
std::vector< CalibratableElement > cluster_ecal_
Definition: Calibratable.h:184
edm::InputTag inputTagSimParticles_
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
#define M_PI
Definition: BFit3D.cc:3
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
virtual void beginJob()
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:33
virtual void analyze(const edm::Event &, const edm::EventSetup &)
T * make() const
make new ROOT object
std::vector< PFCluster > PFClusterCollection
collection of PFCluster objects
Definition: PFClusterFwd.h:9
tuple cout
Definition: gather_cfg.py:121
unsigned int nTrajectoryPoints() const
Definition: PFTrack.h:90
edm::InputTag inputTagClustersEcal_
virtual double phi() const
momentum azimuthal angle
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
Small wrapper class to store information associated with PFCandidates.
Definition: Calibratable.h:66
Block of elements.
Definition: PFBlock.h:30