forked from cms-sw/cmssw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PFAlgo.cc
3795 lines (3145 loc) · 133 KB
/
PFAlgo.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "RecoParticleFlow/PFProducer/interface/PFAlgo.h"
#include "RecoParticleFlow/PFProducer/interface/PFMuonAlgo.h" //PFMuons
#include "RecoParticleFlow/PFProducer/interface/PFElectronAlgo.h"
#include "RecoParticleFlow/PFProducer/interface/PFPhotonAlgo.h"
#include "RecoParticleFlow/PFProducer/interface/PFElectronExtraEqual.h"
#include "RecoParticleFlow/PFClusterTools/interface/PFEnergyCalibration.h"
#include "RecoParticleFlow/PFClusterTools/interface/PFEnergyCalibrationHF.h"
#include "RecoParticleFlow/PFClusterTools/interface/PFSCEnergyCalibration.h"
#include "DataFormats/ParticleFlowReco/interface/PFRecHit.h"
#include "DataFormats/ParticleFlowReco/interface/PFBlock.h"
#include "DataFormats/ParticleFlowReco/interface/PFBlockElementTrack.h"
#include "DataFormats/ParticleFlowReco/interface/PFBlockElementCluster.h"
#include "DataFormats/ParticleFlowReco/interface/PFRecTrack.h"
#include "DataFormats/ParticleFlowReco/interface/PFCluster.h"
#include "DataFormats/ParticleFlowReco/interface/PFLayer.h"
#include "DataFormats/VertexReco/interface/Vertex.h"
#include "DataFormats/TrackReco/interface/Track.h"
#include "DataFormats/MuonReco/interface/Muon.h"
// #include "FWCore/Framework/interface/OrphanHandle.h"
#include "DataFormats/Common/interface/OrphanHandle.h"
// #include "DataFormats/Common/interface/ProductID.h"
#include "DataFormats/Provenance/interface/ProductID.h"
#include "DataFormats/Math/interface/LorentzVector.h"
#include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
#include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h"
#include "DataFormats/ParticleFlowReco/interface/PFDisplacedVertex.h"
#include "DataFormats/ParticleFlowReco/interface/PFDisplacedVertexFwd.h"
#include "Math/PxPyPzM4D.h"
#include "Math/LorentzVector.h"
#include "Math/DisplacementVector3D.h"
#include "Math/SMatrix.h"
#include "TDecompChol.h"
#include "boost/graph/adjacency_matrix.hpp"
#include "boost/graph/graph_utility.hpp"
using namespace std;
using namespace reco;
using namespace boost;
typedef std::list< reco::PFBlockRef >::iterator IBR;
PFAlgo::PFAlgo()
: pfCandidates_( new PFCandidateCollection),
nSigmaECAL_(0),
nSigmaHCAL_(1),
algo_(1),
debug_(false),
pfele_(0),
pfpho_(0),
pfegamma_(0),
useVertices_(false)
{}
PFAlgo::~PFAlgo() {
if (usePFElectrons_) delete pfele_;
if (usePFPhotons_) delete pfpho_;
if (useEGammaFilters_) delete pfegamma_;
}
void
PFAlgo::setParameters(double nSigmaECAL,
double nSigmaHCAL,
const boost::shared_ptr<PFEnergyCalibration>& calibration,
const boost::shared_ptr<PFEnergyCalibrationHF>& thepfEnergyCalibrationHF) {
nSigmaECAL_ = nSigmaECAL;
nSigmaHCAL_ = nSigmaHCAL;
calibration_ = calibration;
thepfEnergyCalibrationHF_ = thepfEnergyCalibrationHF;
}
PFMuonAlgo* PFAlgo::getPFMuonAlgo() {
return pfmu_;
}
//PFElectrons: a new method added to set the parameters for electron reconstruction.
void
PFAlgo::setPFEleParameters(double mvaEleCut,
string mvaWeightFileEleID,
bool usePFElectrons,
const boost::shared_ptr<PFSCEnergyCalibration>& thePFSCEnergyCalibration,
const boost::shared_ptr<PFEnergyCalibration>& thePFEnergyCalibration,
double sumEtEcalIsoForEgammaSC_barrel,
double sumEtEcalIsoForEgammaSC_endcap,
double coneEcalIsoForEgammaSC,
double sumPtTrackIsoForEgammaSC_barrel,
double sumPtTrackIsoForEgammaSC_endcap,
unsigned int nTrackIsoForEgammaSC,
double coneTrackIsoForEgammaSC,
bool applyCrackCorrections,
bool usePFSCEleCalib,
bool useEGElectrons,
bool useEGammaSupercluster) {
mvaEleCut_ = mvaEleCut;
usePFElectrons_ = usePFElectrons;
applyCrackCorrectionsElectrons_ = applyCrackCorrections;
usePFSCEleCalib_ = usePFSCEleCalib;
thePFSCEnergyCalibration_ = thePFSCEnergyCalibration;
useEGElectrons_ = useEGElectrons;
useEGammaSupercluster_ = useEGammaSupercluster;
sumEtEcalIsoForEgammaSC_barrel_ = sumEtEcalIsoForEgammaSC_barrel;
sumEtEcalIsoForEgammaSC_endcap_ = sumEtEcalIsoForEgammaSC_endcap;
coneEcalIsoForEgammaSC_ = coneEcalIsoForEgammaSC;
sumPtTrackIsoForEgammaSC_barrel_ = sumPtTrackIsoForEgammaSC_barrel;
sumPtTrackIsoForEgammaSC_endcap_ = sumPtTrackIsoForEgammaSC_endcap;
coneTrackIsoForEgammaSC_ = coneTrackIsoForEgammaSC;
nTrackIsoForEgammaSC_ = nTrackIsoForEgammaSC;
if(!usePFElectrons_) return;
mvaWeightFileEleID_ = mvaWeightFileEleID;
FILE * fileEleID = fopen(mvaWeightFileEleID_.c_str(), "r");
if (fileEleID) {
fclose(fileEleID);
}
else {
string err = "PFAlgo: cannot open weight file '";
err += mvaWeightFileEleID;
err += "'";
throw invalid_argument( err );
}
pfele_= new PFElectronAlgo(mvaEleCut_,mvaWeightFileEleID_,
thePFSCEnergyCalibration_,
thePFEnergyCalibration,
applyCrackCorrectionsElectrons_,
usePFSCEleCalib_,
useEGElectrons_,
useEGammaSupercluster_,
sumEtEcalIsoForEgammaSC_barrel_,
sumEtEcalIsoForEgammaSC_endcap_,
coneEcalIsoForEgammaSC_,
sumPtTrackIsoForEgammaSC_barrel_,
sumPtTrackIsoForEgammaSC_endcap_,
nTrackIsoForEgammaSC_,
coneTrackIsoForEgammaSC_);
}
void
PFAlgo::setPFPhotonParameters(bool usePFPhotons,
std::string mvaWeightFileConvID,
double mvaConvCut,
bool useReg,
std::string X0_Map,
const boost::shared_ptr<PFEnergyCalibration>& thePFEnergyCalibration,
double sumPtTrackIsoForPhoton,
double sumPtTrackIsoSlopeForPhoton)
{
usePFPhotons_ = usePFPhotons;
//for MVA pass PV if there is one in the collection otherwise pass a dummy
reco::Vertex dummy;
if(useVertices_)
{
dummy = primaryVertex_;
}
else { // create a dummy PV
reco::Vertex::Error e;
e(0, 0) = 0.0015 * 0.0015;
e(1, 1) = 0.0015 * 0.0015;
e(2, 2) = 15. * 15.;
reco::Vertex::Point p(0, 0, 0);
dummy = reco::Vertex(p, e, 0, 0, 0);
}
// pv=&dummy;
if(! usePFPhotons_) return;
FILE * filePhotonConvID = fopen(mvaWeightFileConvID.c_str(), "r");
if (filePhotonConvID) {
fclose(filePhotonConvID);
}
else {
string err = "PFAlgo: cannot open weight file '";
err += mvaWeightFileConvID;
err += "'";
throw invalid_argument( err );
}
const reco::Vertex* pv=&dummy;
pfpho_ = new PFPhotonAlgo(mvaWeightFileConvID,
mvaConvCut,
useReg,
X0_Map,
*pv,
thePFEnergyCalibration,
sumPtTrackIsoForPhoton,
sumPtTrackIsoSlopeForPhoton
);
return;
}
void PFAlgo::setEGammaParameters(bool use_EGammaFilters,
std::string ele_iso_path_mvaWeightFile,
double ele_iso_pt,
double ele_iso_mva_barrel,
double ele_iso_mva_endcap,
double ele_iso_combIso_barrel,
double ele_iso_combIso_endcap,
double ele_noniso_mva,
unsigned int ele_missinghits,
bool useProtectionsForJetMET,
const edm::ParameterSet& ele_protectionsForJetMET,
double ph_MinEt,
double ph_combIso,
double ph_HoE,
double ph_sietaieta_eb,
double ph_sietaieta_ee,
const edm::ParameterSet& ph_protectionsForJetMET
)
{
useEGammaFilters_ = use_EGammaFilters;
if(!useEGammaFilters_ ) return;
FILE * fileEGamma_ele_iso_ID = fopen(ele_iso_path_mvaWeightFile.c_str(), "r");
if (fileEGamma_ele_iso_ID) {
fclose(fileEGamma_ele_iso_ID);
}
else {
string err = "PFAlgo: cannot open weight file '";
err += ele_iso_path_mvaWeightFile;
err += "'";
throw invalid_argument( err );
}
// ele_iso_mvaID_ = new ElectronMVAEstimator(ele_iso_path_mvaWeightFile_);
useProtectionsForJetMET_ = useProtectionsForJetMET;
pfegamma_ = new PFEGammaFilters(ph_MinEt,
ph_combIso,
ph_HoE,
ph_sietaieta_eb,
ph_sietaieta_ee,
ph_protectionsForJetMET,
ele_iso_pt,
ele_iso_mva_barrel,
ele_iso_mva_endcap,
ele_iso_combIso_barrel,
ele_iso_combIso_endcap,
ele_noniso_mva,
ele_missinghits,
ele_iso_path_mvaWeightFile,
ele_protectionsForJetMET);
return;
}
void PFAlgo::setEGammaCollections(const edm::View<reco::PFCandidate> & pfEgammaCandidates,
const edm::ValueMap<reco::GsfElectronRef> & valueMapGedElectrons,
const edm::ValueMap<reco::PhotonRef> & valueMapGedPhotons){
if(useEGammaFilters_) {
pfEgammaCandidates_ = & pfEgammaCandidates;
valueMapGedElectrons_ = & valueMapGedElectrons;
valueMapGedPhotons_ = & valueMapGedPhotons;
}
}
/*
void PFAlgo::setPFPhotonRegWeights(
const GBRForest *LCorrForest,
const GBRForest *GCorrForest,
const GBRForest *ResForest
) {
if(usePFPhotons_)
pfpho_->setGBRForest(LCorrForest, GCorrForest, ResForest);
}
*/
void PFAlgo::setPFPhotonRegWeights(
const GBRForest *LCorrForestEB,
const GBRForest *LCorrForestEE,
const GBRForest *GCorrForestBarrel,
const GBRForest *GCorrForestEndcapHr9,
const GBRForest *GCorrForestEndcapLr9, const GBRForest *PFEcalResolution
){
pfpho_->setGBRForest(LCorrForestEB,LCorrForestEE,
GCorrForestBarrel, GCorrForestEndcapHr9,
GCorrForestEndcapLr9, PFEcalResolution);
}
void
PFAlgo::setPFMuonAndFakeParameters(const edm::ParameterSet& pset)
// std::vector<double> muonHCAL,
// std::vector<double> muonECAL,
// std::vector<double> muonHO,
// double nSigmaTRACK,
// double ptError,
// std::vector<double> factors45)
{
pfmu_ = new PFMuonAlgo();
pfmu_->setParameters(pset);
// Muon parameters
muonHCAL_= pset.getParameter<std::vector<double> >("muon_HCAL");
muonECAL_= pset.getParameter<std::vector<double> >("muon_ECAL");
muonHO_= pset.getParameter<std::vector<double> >("muon_HO");
assert ( muonHCAL_.size() == 2 && muonECAL_.size() == 2 && muonHO_.size() == 2);
nSigmaTRACK_= pset.getParameter<double>("nsigma_TRACK");
ptError_= pset.getParameter<double>("pt_Error");
factors45_ = pset.getParameter<std::vector<double> >("factors_45");
assert ( factors45_.size() == 2 );
}
void
PFAlgo::setMuonHandle(const edm::Handle<reco::MuonCollection>& muons) {
muonHandle_ = muons;
}
void
PFAlgo::setPostHFCleaningParameters(bool postHFCleaning,
double minHFCleaningPt,
double minSignificance,
double maxSignificance,
double minSignificanceReduction,
double maxDeltaPhiPt,
double minDeltaMet) {
postHFCleaning_ = postHFCleaning;
minHFCleaningPt_ = minHFCleaningPt;
minSignificance_ = minSignificance;
maxSignificance_ = maxSignificance;
minSignificanceReduction_= minSignificanceReduction;
maxDeltaPhiPt_ = maxDeltaPhiPt;
minDeltaMet_ = minDeltaMet;
}
void
PFAlgo::setDisplacedVerticesParameters(bool rejectTracks_Bad,
bool rejectTracks_Step45,
bool usePFNuclearInteractions,
bool usePFConversions,
bool usePFDecays,
double dptRel_DispVtx){
rejectTracks_Bad_ = rejectTracks_Bad;
rejectTracks_Step45_ = rejectTracks_Step45;
usePFNuclearInteractions_ = usePFNuclearInteractions;
usePFConversions_ = usePFConversions;
usePFDecays_ = usePFDecays;
dptRel_DispVtx_ = dptRel_DispVtx;
}
void
PFAlgo::setPFVertexParameters(bool useVertex,
const reco::VertexCollection* primaryVertices) {
useVertices_ = useVertex;
//Set the vertices for muon cleaning
pfmu_->setInputsForCleaning(primaryVertices);
//Now find the primary vertex!
bool primaryVertexFound = false;
nVtx_ = primaryVertices->size();
if(usePFPhotons_){
pfpho_->setnPU(nVtx_);
}
for (unsigned short i=0 ;i<primaryVertices->size();++i)
{
if(primaryVertices->at(i).isValid()&&(!primaryVertices->at(i).isFake()))
{
primaryVertex_ = primaryVertices->at(i);
primaryVertexFound = true;
break;
}
}
//Use vertices if the user wants to but only if it exists a good vertex
useVertices_ = useVertex && primaryVertexFound;
if(usePFPhotons_) {
if (useVertices_ ){
pfpho_->setPhotonPrimaryVtx(primaryVertex_ );
}
else{
reco::Vertex::Error e;
e(0, 0) = 0.0015 * 0.0015;
e(1, 1) = 0.0015 * 0.0015;
e(2, 2) = 15. * 15.;
reco::Vertex::Point p(0, 0, 0);
reco::Vertex dummy = reco::Vertex(p, e, 0, 0, 0);
// std::cout << " PFPho " << pfpho_ << std::endl;
pfpho_->setPhotonPrimaryVtx(dummy);
}
}
}
void PFAlgo::reconstructParticles( const reco::PFBlockHandle& blockHandle ) {
blockHandle_ = blockHandle;
reconstructParticles( *blockHandle_ );
}
void PFAlgo::reconstructParticles( const reco::PFBlockCollection& blocks ) {
// reset output collection
if(pfCandidates_.get() )
pfCandidates_->clear();
else
pfCandidates_.reset( new reco::PFCandidateCollection );
if(pfElectronCandidates_.get() )
pfElectronCandidates_->clear();
else
pfElectronCandidates_.reset( new reco::PFCandidateCollection);
// Clearing pfPhotonCandidates
if( pfPhotonCandidates_.get() )
pfPhotonCandidates_->clear();
else
pfPhotonCandidates_.reset( new reco::PFCandidateCollection);
if(pfCleanedCandidates_.get() )
pfCleanedCandidates_->clear();
else
pfCleanedCandidates_.reset( new reco::PFCandidateCollection );
// not a auto_ptr; shout not be deleted after transfer
pfElectronExtra_.clear();
pfPhotonExtra_.clear();
if( debug_ ) {
cout<<"*********************************************************"<<endl;
cout<<"***** Particle flow algorithm *****"<<endl;
cout<<"*********************************************************"<<endl;
}
// sort elements in three lists:
std::list< reco::PFBlockRef > hcalBlockRefs;
std::list< reco::PFBlockRef > ecalBlockRefs;
std::list< reco::PFBlockRef > hoBlockRefs;
std::list< reco::PFBlockRef > otherBlockRefs;
for( unsigned i=0; i<blocks.size(); ++i ) {
// reco::PFBlockRef blockref( blockh,i );
reco::PFBlockRef blockref = createBlockRef( blocks, i);
const reco::PFBlock& block = *blockref;
const edm::OwnVector< reco::PFBlockElement >&
elements = block.elements();
bool singleEcalOrHcal = false;
if( elements.size() == 1 ){
if( elements[0].type() == reco::PFBlockElement::ECAL ){
ecalBlockRefs.push_back( blockref );
singleEcalOrHcal = true;
}
if( elements[0].type() == reco::PFBlockElement::HCAL ){
hcalBlockRefs.push_back( blockref );
singleEcalOrHcal = true;
}
if( elements[0].type() == reco::PFBlockElement::HO ){
// Single HO elements are likely to be noise. Not considered for now.
hoBlockRefs.push_back( blockref );
singleEcalOrHcal = true;
}
}
if(!singleEcalOrHcal) {
otherBlockRefs.push_back( blockref );
}
}//loop blocks
if( debug_ ){
cout<<"# Ecal blocks: "<<ecalBlockRefs.size()
<<", # Hcal blocks: "<<hcalBlockRefs.size()
<<", # HO blocks: "<<hoBlockRefs.size()
<<", # Other blocks: "<<otherBlockRefs.size()<<endl;
}
// loop on blocks that are not single ecal,
// and not single hcal.
unsigned nblcks = 0;
for( IBR io = otherBlockRefs.begin(); io!=otherBlockRefs.end(); ++io) {
if ( debug_ ) std::cout << "Block number " << nblcks++ << std::endl;
processBlock( *io, hcalBlockRefs, ecalBlockRefs );
}
std::list< reco::PFBlockRef > empty;
unsigned hblcks = 0;
// process remaining single hcal blocks
for( IBR ih = hcalBlockRefs.begin(); ih!=hcalBlockRefs.end(); ++ih) {
if ( debug_ ) std::cout << "HCAL block number " << hblcks++ << std::endl;
processBlock( *ih, empty, empty );
}
unsigned eblcks = 0;
// process remaining single ecal blocks
for( IBR ie = ecalBlockRefs.begin(); ie!=ecalBlockRefs.end(); ++ie) {
if ( debug_ ) std::cout << "ECAL block number " << eblcks++ << std::endl;
processBlock( *ie, empty, empty );
}
// Post HF Cleaning
postCleaning();
//Muon post cleaning
pfmu_->postClean(pfCandidates_.get());
//Add Missing muons
if( muonHandle_.isValid())
pfmu_->addMissingMuons(muonHandle_,pfCandidates_.get());
}
void PFAlgo::processBlock( const reco::PFBlockRef& blockref,
std::list<reco::PFBlockRef>& hcalBlockRefs,
std::list<reco::PFBlockRef>& ecalBlockRefs ) {
// debug_ = false;
assert(!blockref.isNull() );
const reco::PFBlock& block = *blockref;
typedef std::multimap<double, unsigned>::iterator IE;
typedef std::multimap<double, std::pair<unsigned,::math::XYZVector> >::iterator IS;
typedef std::multimap<double, std::pair<unsigned,bool> >::iterator IT;
typedef std::multimap< unsigned, std::pair<double, unsigned> >::iterator II;
if(debug_) {
cout<<"#########################################################"<<endl;
cout<<"##### Process Block: #####"<<endl;
cout<<"#########################################################"<<endl;
cout<<block<<endl;
}
const edm::OwnVector< reco::PFBlockElement >& elements = block.elements();
// make a copy of the link data, which will be edited.
PFBlock::LinkData linkData = block.linkData();
// keep track of the elements which are still active.
vector<bool> active( elements.size(), true );
// //PFElectrons:
// usePFElectrons_ external configurable parameter to set the usage of pf electron
std::vector<reco::PFCandidate> tempElectronCandidates;
tempElectronCandidates.clear();
if (usePFElectrons_) {
if (pfele_->isElectronValidCandidate(blockref,active, primaryVertex_ )){
// if there is at least a valid candidate is get the vector of pfcandidates
const std::vector<reco::PFCandidate> PFElectCandidates_(pfele_->getElectronCandidates());
for ( std::vector<reco::PFCandidate>::const_iterator ec=PFElectCandidates_.begin(); ec != PFElectCandidates_.end(); ++ec )tempElectronCandidates.push_back(*ec);
// (***) We're filling the ElectronCandidates into the PFCandiate collection
// ..... Once we let PFPhotonAlgo over-write electron-decision, we need to move this to
// ..... after the PhotonAlgo has run (Fabian)
}
// The vector active is automatically changed (it is passed by ref) in PFElectronAlgo
// for all the electron candidate
pfElectronCandidates_->insert(pfElectronCandidates_->end(),
pfele_->getAllElectronCandidates().begin(),
pfele_->getAllElectronCandidates().end());
pfElectronExtra_.insert(pfElectronExtra_.end(),
pfele_->getElectronExtra().begin(),
pfele_->getElectronExtra().end());
}
if( /* --- */ usePFPhotons_ /* --- */ ) {
if(debug_)
cout<<endl<<"--------------- entering PFPhotonAlgo ----------------"<<endl;
vector<PFCandidatePhotonExtra> pfPhotonExtraCand;
if ( pfpho_->isPhotonValidCandidate(blockref, // passing the reference to the PFBlock
active, // std::vector<bool> containing information about acitivity
pfPhotonCandidates_, // pointer to candidate vector, to be filled by the routine
pfPhotonExtraCand, // candidate extra vector, to be filled by the routine
tempElectronCandidates
//pfElectronCandidates_ // pointer to some auziliary UNTOUCHED FOR NOW
) ) {
if(debug_)
std::cout<< " In this PFBlock we found "<<pfPhotonCandidates_->size()<<" Photon Candidates."<<std::endl;
// CAUTION: In case we want to allow the PhotonAlgo to 'over-write' what the ElectronAlgo did above
// ........ we should NOT fill the PFCandidate-vector with the electrons above (***)
// Here we need to add all the photon cands to the pfCandidate list
unsigned int extracand =0;
PFCandidateCollection::const_iterator cand = pfPhotonCandidates_->begin();
for( ; cand != pfPhotonCandidates_->end(); ++cand, ++extracand) {
pfCandidates_->push_back(*cand);
pfPhotonExtra_.push_back(pfPhotonExtraCand[extracand]);
}
} // end of 'if' in case photons are found
pfPhotonExtraCand.clear();
pfPhotonCandidates_->clear();
} // end of Photon algo
if (usePFElectrons_) {
for ( std::vector<reco::PFCandidate>::const_iterator ec=tempElectronCandidates.begin(); ec != tempElectronCandidates.end(); ++ec ){
pfCandidates_->push_back(*ec);
}
tempElectronCandidates.clear();
}
// New EGamma Reconstruction 10/10/2013
if(useEGammaFilters_) {
// const edm::ValueMap<reco::GsfElectronRef> & myGedElectronValMap(*valueMapGedElectrons_);
bool egmLocalDebug = false;
bool egmLocalBlockDebug = false;
unsigned int negmcandidates = pfEgammaCandidates_->size();
for(unsigned int ieg=0 ; ieg < negmcandidates; ++ieg) {
// const reco::PFCandidate & egmcand((*pfEgammaCandidates_)[ieg]);
reco::PFCandidateRef pfEgmRef = pfEgammaCandidates_->refAt(ieg).castTo<reco::PFCandidateRef>();
const PFCandidate::ElementsInBlocks& theElements = (*pfEgmRef).elementsInBlocks();
PFCandidate::ElementsInBlocks::const_iterator iegfirst = theElements.begin();
bool sameBlock = false;
bool isGoodElectron = false;
bool isGoodPhoton = false;
bool isPrimaryElectron = false;
if(iegfirst->first == blockref)
sameBlock = true;
if(sameBlock) {
if(egmLocalDebug)
cout << " I am in looping on EGamma Candidates: pt " << (*pfEgmRef).pt()
<< " eta,phi " << (*pfEgmRef).eta() << ", " << (*pfEgmRef).phi()
<< " charge " << (*pfEgmRef).charge() << endl;
if((*pfEgmRef).gsfTrackRef().isNonnull()) {
reco::GsfElectronRef gedEleRef = (*valueMapGedElectrons_)[pfEgmRef];
if(gedEleRef.isNonnull()) {
isGoodElectron = pfegamma_->passElectronSelection(*gedEleRef,*pfEgmRef,nVtx_);
isPrimaryElectron = pfegamma_->isElectron(*gedEleRef);
if(egmLocalDebug){
if(isGoodElectron)
cout << "** Good Electron, pt " << gedEleRef->pt()
<< " eta, phi " << gedEleRef->eta() << ", " << gedEleRef->phi()
<< " charge " << gedEleRef->charge()
<< " isPrimary " << isPrimaryElectron << endl;
}
}
}
if((*pfEgmRef).superClusterRef().isNonnull()) {
reco::PhotonRef gedPhoRef = (*valueMapGedPhotons_)[pfEgmRef];
if(gedPhoRef.isNonnull()) {
isGoodPhoton = pfegamma_->passPhotonSelection(*gedPhoRef);
if(egmLocalDebug) {
if(isGoodPhoton)
cout << "** Good Photon, pt " << gedPhoRef->pt()
<< " eta, phi " << gedPhoRef->eta() << ", " << gedPhoRef->phi() << endl;
}
}
}
} // end same block
if(isGoodElectron && isGoodPhoton) {
if(isPrimaryElectron)
isGoodPhoton = false;
else
isGoodElectron = false;
}
// isElectron
if(isGoodElectron) {
reco::GsfElectronRef gedEleRef = (*valueMapGedElectrons_)[pfEgmRef];
reco::PFCandidate myPFElectron = *pfEgmRef;
// run protections
bool lockTracks = false;
bool isSafe = true;
if( useProtectionsForJetMET_) {
lockTracks = true;
isSafe = pfegamma_->isElectronSafeForJetMET(*gedEleRef,myPFElectron,primaryVertex_,lockTracks);
}
if(isSafe) {
reco::PFCandidate::ParticleType particleType = reco::PFCandidate::e;
myPFElectron.setParticleType(particleType);
myPFElectron.setCharge(gedEleRef->charge());
myPFElectron.setP4(gedEleRef->p4());
myPFElectron.set_mva_e_pi(gedEleRef->mva_e_pi());
myPFElectron.set_mva_Isolated(gedEleRef->mva_Isolated());
if(egmLocalDebug) {
cout << " PFAlgo: found an electron with NEW EGamma code " << endl;
cout << " myPFElectron: pt " << myPFElectron.pt()
<< " eta,phi " << myPFElectron.eta() << ", " <<myPFElectron.phi()
<< " mva " << myPFElectron.mva_e_pi()
<< " charge " << myPFElectron.charge() << endl;
}
// Lock all the elements
if(egmLocalBlockDebug)
cout << " THE BLOCK " << *blockref << endl;
for (PFCandidate::ElementsInBlocks::const_iterator ieb = theElements.begin();
ieb<theElements.end(); ++ieb) {
active[ieb->second] = false;
if(egmLocalBlockDebug)
cout << " Elements used " << ieb->second << endl;
}
// The electron is considered safe for JetMET and the additional tracks pointing to it are locked
if(lockTracks) {
const PFCandidate::ElementsInBlocks& extraTracks = myPFElectron.egammaExtraRef()->extraNonConvTracks();
for (PFCandidate::ElementsInBlocks::const_iterator itrk = extraTracks.begin();
itrk<extraTracks.end(); ++itrk) {
active[itrk->second] = false;
}
}
pfCandidates_->push_back(myPFElectron);
}
else {
if(egmLocalDebug)
cout << "PFAlgo: Electron DISCARDED, NOT SAFE FOR JETMET " << endl;
}
}
if(isGoodPhoton) {
reco::PhotonRef gedPhoRef = (*valueMapGedPhotons_)[pfEgmRef];
reco::PFCandidate myPFPhoton = *pfEgmRef;
bool isSafe = true;
if( useProtectionsForJetMET_) {
isSafe = pfegamma_->isPhotonSafeForJetMET(*gedPhoRef,myPFPhoton);
}
if(isSafe) {
reco::PFCandidate::ParticleType particleType = reco::PFCandidate::gamma;
myPFPhoton.setParticleType(particleType);
myPFPhoton.setCharge(0);
myPFPhoton.set_mva_nothing_gamma(1.);
::math::XYZPoint v(primaryVertex_.x(), primaryVertex_.y(), primaryVertex_.z());
myPFPhoton.setVertex( v );
myPFPhoton.setP4(gedPhoRef->p4());
if(egmLocalDebug) {
cout << " PFAlgo: found a photon with NEW EGamma code " << endl;
cout << " myPFPhoton: pt " << myPFPhoton.pt()
<< " eta,phi " << myPFPhoton.eta() << ", " <<myPFPhoton.phi()
<< " charge " << myPFPhoton.charge() << endl;
}
// Lock all the elements
if(egmLocalBlockDebug)
cout << " THE BLOCK " << *blockref << endl;
for (PFCandidate::ElementsInBlocks::const_iterator ieb = theElements.begin();
ieb<theElements.end(); ++ieb) {
active[ieb->second] = false;
if(egmLocalBlockDebug)
cout << " Elements used " << ieb->second << endl;
}
pfCandidates_->push_back(myPFPhoton);
} // end isSafe
} // end isGoodPhoton
} // end loop on EGM candidates
} // end if use EGammaFilters
//Lock extra conversion tracks not used by Photon Algo
if (usePFConversions_ )
{
for(unsigned iEle=0; iEle<elements.size(); iEle++) {
PFBlockElement::Type type = elements[iEle].type();
if(type==PFBlockElement::TRACK)
{
if(elements[iEle].trackRef()->algo() == reco::TrackBase::conversionStep)
active[iEle]=false;
if(elements[iEle].trackRef()->quality(reco::TrackBase::highPurity))continue;
const reco::PFBlockElementTrack * trackRef = dynamic_cast<const reco::PFBlockElementTrack*>((&elements[iEle]));
if(!(trackRef->trackType(reco::PFBlockElement::T_FROM_GAMMACONV)))continue;
if(elements[iEle].convRefs().size())active[iEle]=false;
}
}
}
if(debug_)
cout<<endl<<"--------------- loop 1 ------------------"<<endl;
//COLINFEB16
// In loop 1, we loop on all elements.
// The primary goal is to deal with tracks that are:
// - not associated to an HCAL cluster
// - not identified as an electron.
// Those tracks should be predominantly relatively low energy charged
// hadrons which are not detected in the ECAL.
// The secondary goal is to prepare for the next loops
// - The ecal and hcal elements are sorted in separate vectors
// which will be used as a base for the corresponding loops.
// - For tracks which are connected to more than one HCAL cluster,
// the links between the track and the cluster are cut for all clusters
// but the closest one.
// - HF only blocks ( HFEM, HFHAD, HFEM+HFAD) are identified
// obsolete comments?
// loop1:
// - sort ecal and hcal elements in separate vectors
// - for tracks:
// - lock closest ecal cluster
// - cut link to farthest hcal cluster, if more than 1.
// vectors to store indices to ho, hcal and ecal elements
vector<unsigned> hcalIs;
vector<unsigned> hoIs;
vector<unsigned> ecalIs;
vector<unsigned> trackIs;
vector<unsigned> ps1Is;
vector<unsigned> ps2Is;
vector<unsigned> hfEmIs;
vector<unsigned> hfHadIs;
for(unsigned iEle=0; iEle<elements.size(); iEle++) {
PFBlockElement::Type type = elements[iEle].type();
if(debug_ && type != PFBlockElement::BREM ) cout<<endl<<elements[iEle];
switch( type ) {
case PFBlockElement::TRACK:
if ( active[iEle] ) {
trackIs.push_back( iEle );
if(debug_) cout<<"TRACK, stored index, continue"<<endl;
}
break;
case PFBlockElement::ECAL:
if ( active[iEle] ) {
ecalIs.push_back( iEle );
if(debug_) cout<<"ECAL, stored index, continue"<<endl;
}
continue;
case PFBlockElement::HCAL:
if ( active[iEle] ) {
hcalIs.push_back( iEle );
if(debug_) cout<<"HCAL, stored index, continue"<<endl;
}
continue;
case PFBlockElement::HO:
if (useHO_) {
if ( active[iEle] ) {
hoIs.push_back( iEle );
if(debug_) cout<<"HO, stored index, continue"<<endl;
}
}
continue;
case PFBlockElement::HFEM:
if ( active[iEle] ) {
hfEmIs.push_back( iEle );
if(debug_) cout<<"HFEM, stored index, continue"<<endl;
}
continue;
case PFBlockElement::HFHAD:
if ( active[iEle] ) {
hfHadIs.push_back( iEle );
if(debug_) cout<<"HFHAD, stored index, continue"<<endl;
}
continue;
default:
continue;
}
// we're now dealing with a track
unsigned iTrack = iEle;
reco::MuonRef muonRef = elements[iTrack].muonRef();
//Check if the track is a primary track of a secondary interaction
//If that is the case reconstruct a charged hadron noly using that
//track
if (active[iTrack] && isFromSecInt(elements[iEle], "primary")){
bool isPrimaryTrack = elements[iEle].displacedVertexRef(PFBlockElement::T_TO_DISP)->displacedVertexRef()->isTherePrimaryTracks();
if (isPrimaryTrack) {
if (debug_) cout << "Primary Track reconstructed alone" << endl;
unsigned tmpi = reconstructTrack(elements[iEle]);
(*pfCandidates_)[tmpi].addElementInBlock( blockref, iEle );
active[iTrack] = false;
}
}
if(debug_) {
if ( !active[iTrack] )
cout << "Already used by electrons, muons, conversions" << endl;
}
// Track already used as electron, muon, conversion?
// Added a check on the activated element
if ( ! active[iTrack] ) continue;
reco::TrackRef trackRef = elements[iTrack].trackRef();
assert( !trackRef.isNull() );
if (debug_ ) {
cout <<"PFAlgo:processBlock "<<" "<<trackIs.size()<<" "<<ecalIs.size()<<" "<<hcalIs.size()<<" "<<hoIs.size()<<endl;
}
// look for associated elements of all types
//COLINFEB16
// all types of links are considered.
// the elements are sorted by increasing distance
std::multimap<double, unsigned> ecalElems;
block.associatedElements( iTrack, linkData,
ecalElems ,
reco::PFBlockElement::ECAL,
reco::PFBlock::LINKTEST_ALL );
std::multimap<double, unsigned> hcalElems;
block.associatedElements( iTrack, linkData,
hcalElems,
reco::PFBlockElement::HCAL,
reco::PFBlock::LINKTEST_ALL );
// When a track has no HCAL cluster linked, but another track is linked to the same
// ECAL cluster and an HCAL cluster, link the track to the HCAL cluster for
// later analysis
if ( hcalElems.empty() && !ecalElems.empty() ) {
// debug_ = true;
unsigned ntt = 1;
unsigned index = ecalElems.begin()->second;
std::multimap<double, unsigned> sortedTracks;
block.associatedElements( index, linkData,
sortedTracks,
reco::PFBlockElement::TRACK,
reco::PFBlock::LINKTEST_ALL );
// std::cout << "The ECAL is linked to " << sortedTracks.size() << std::endl;
// Loop over all tracks
for(IE ie = sortedTracks.begin(); ie != sortedTracks.end(); ++ie ) {
unsigned jTrack = ie->second;
// std::cout << "Track " << jTrack << std::endl;
// Track must be active
if ( !active[jTrack] ) continue;
//std::cout << "Active " << std::endl;
// The loop is on the other tracks !
if ( jTrack == iTrack ) continue;
//std::cout << "A different track ! " << std::endl;
// Check if the ECAL closest to this track is the current ECAL
// Otherwise ignore this track in the neutral energy determination
std::multimap<double, unsigned> sortedECAL;
block.associatedElements( jTrack, linkData,
sortedECAL,
reco::PFBlockElement::ECAL,
reco::PFBlock::LINKTEST_ALL );
if ( sortedECAL.begin()->second != index ) continue;
//std::cout << "With closest ECAL identical " << std::endl;
// Check if this track is also linked to an HCAL
std::multimap<double, unsigned> sortedHCAL;
block.associatedElements( jTrack, linkData,
sortedHCAL,
reco::PFBlockElement::HCAL,
reco::PFBlock::LINKTEST_ALL );
if ( !sortedHCAL.size() ) continue;
//std::cout << "With an HCAL cluster " << sortedHCAL.begin()->first << std::endl;
ntt++;
// In that case establish a link with the first track