ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/yangyong/src/RecoAnalyzer.cc
Revision: 1.2
Committed: Mon Feb 28 16:50:13 2011 UTC (14 years, 2 months ago) by yangyong
Content type: text/plain
Branch: MAIN
CVS Tags: V01-00-00, HEAD
Changes since 1.1: +13 -6 lines
Log Message:
new files for running regionalUnpacking sequence on top of RECO data-format

File Contents

# User Rev Content
1 yangyong 1.1 #include "UserCode/yangyong/interface/RecoAnalyzer.h"
2    
3     #include "DataFormats/EgammaReco/interface/BasicCluster.h"
4     #include "DataFormats/EcalRecHit/interface/EcalRecHitCollections.h"
5     #include "DataFormats/EcalDetId/interface/EBDetId.h"
6     #include "DataFormats/EcalDetId/interface/EEDetId.h"
7     #include "DataFormats/EcalDetId/interface/ESDetId.h"
8     #include "DataFormats/DetId/interface/DetId.h"
9     #include "DataFormats/Math/interface/Point3D.h"
10    
11     #include "FWCore/Framework/interface/Event.h"
12     #include "FWCore/Framework/interface/EventSetup.h"
13     #include "DataFormats/Common/interface/Handle.h"
14     #include "FWCore/Framework/interface/ESHandle.h"
15     #include "FWCore/MessageLogger/interface/MessageLogger.h"
16    
17     #include "DataFormats/RecoCandidate/interface/RecoEcalCandidate.h"
18     #include "DataFormats/RecoCandidate/interface/RecoEcalCandidateFwd.h"
19    
20     #include "DataFormats/GeometryVector/interface/LocalPoint.h"
21    
22     //Level 1 Trigger
23     #include "DataFormats/L1Trigger/interface/L1EmParticle.h"
24     #include "DataFormats/L1Trigger/interface/L1EmParticleFwd.h"
25     #include "CondFormats/L1TObjects/interface/L1CaloGeometry.h"
26     #include "CondFormats/DataRecord/interface/L1CaloGeometryRecord.h"
27     #include "DataFormats/HLTReco/interface/TriggerFilterObjectWithRefs.h"
28     #include "Geometry/Records/interface/CaloGeometryRecord.h"
29    
30     /// EgammaCoreTools
31    
32     #include "RecoEcal/EgammaCoreTools/interface/PositionCalc.h"
33     #include "RecoEcal/EgammaCoreTools/interface/EcalEtaPhiRegion.h"
34    
35    
36     // Ecal Mapping
37     #include "DataFormats/EcalRawData/interface/EcalListOfFEDS.h"
38     #include "Geometry/EcalMapping/interface/EcalElectronicsMapping.h"
39     #include "Geometry/EcalMapping/interface/EcalMappingRcd.h"
40     #include <DataFormats/FEDRawData/interface/FEDNumbering.h>
41    
42     // Jets stuff
43     #include "DataFormats/L1Trigger/interface/L1JetParticle.h"
44     #include "DataFormats/L1Trigger/interface/L1JetParticleFwd.h"
45    
46     //// Ecal Electrons Id
47     #include "DataFormats/EcalDetId/interface/EcalElectronicsId.h"
48     #include "DataFormats/EcalDetId/interface/EcalTriggerElectronicsId.h"
49    
50    
51     // ES stuff
52     #include "Geometry/EcalAlgo/interface/EcalPreshowerGeometry.h"
53     #include "Geometry/CaloTopology/interface/EcalPreshowerTopology.h"
54     #include "DataFormats/EcalDetId/interface/EcalSubdetector.h"
55     #include "DataFormats/EgammaReco/interface/PreshowerClusterFwd.h"
56    
57     //Ecal status
58     #include "CondFormats/DataRecord/interface/EcalChannelStatusRcd.h"
59     #include "CondFormats/EcalObjects/interface/EcalChannelStatus.h"
60    
61     #include "CondFormats/L1TObjects/interface/L1GtTriggerMenuFwd.h"
62     #include "CondFormats/L1TObjects/interface/L1GtTriggerMenu.h"
63     #include "CondFormats/DataRecord/interface/L1GtTriggerMenuRcd.h"
64    
65    
66     #include "CondFormats/EcalObjects/interface/EcalPedestals.h"
67     #include "CondFormats/DataRecord/interface/EcalPedestalsRcd.h"
68    
69     #include "CondFormats/EcalObjects/interface/EcalMGPAGainRatio.h"
70     #include "CondFormats/EcalObjects/interface/EcalGainRatios.h"
71     #include "CondFormats/DataRecord/interface/EcalGainRatiosRcd.h"
72    
73     #include "CondFormats/EcalObjects/interface/EcalIntercalibConstantsMC.h"
74     #include "CondFormats/DataRecord/interface/EcalIntercalibConstantsMCRcd.h"
75    
76     #include "TVector3.h"
77    
78     #include "DataFormats/TrackerRecHit2D/interface/SiPixelRecHitCollection.h"
79    
80     #include "CLHEP/Matrix/Vector.h"
81     #include "CLHEP/Matrix/SymMatrix.h"
82    
83     #include "FWCore/ServiceRegistry/interface/Service.h"
84     #include "FWCore/Utilities/interface/RandomNumberGenerator.h"
85     #include "CLHEP/Random/RandGaussQ.h"
86    
87     #include "DataFormats/VertexReco/interface/Vertex.h"
88     #include "DataFormats/VertexReco/interface/VertexFwd.h"
89     #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
90     #include "Geometry/TrackerGeometryBuilder/interface/PixelGeomDetUnit.h"
91     #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
92     ///#include "Geometry/TrackerTopology/interface/RectangularPixelTopology.h"
93     #include "Geometry/TrackerGeometryBuilder/interface/RectangularPixelTopology.h"
94    
95     // needed for trigger bits from EventSetup as in ALCARECO paths
96     #include "CondFormats/HLTObjects/interface/AlCaRecoTriggerBits.h"
97     #include "CondFormats/DataRecord/interface/AlCaRecoTriggerBitsRcd.h"
98    
99    
100     //#include "HLTrigger/HLTfilters/interface/HLTHighLevelDev.h"
101     #include "DataFormats/HcalDetId/interface/HcalDetId.h"
102     #include "DataFormats/HcalRecHit/interface/HcalRecHitCollections.h"
103    
104     #include "DataFormats/HcalDetId/interface/HcalDetId.h"
105     #include "DataFormats/HcalRecHit/interface/HcalRecHitCollections.h"
106     #include "DataFormats/CaloTowers/interface/CaloTower.h"
107     #include "DataFormats/CaloTowers/interface/CaloTowerFwd.h"
108    
109     #include "DataFormats/TrackReco/interface/Track.h"
110     #include "DataFormats/TrackCandidate/interface/TrackCandidateCollection.h"
111     #include "DataFormats/TrackCandidate/interface/TrackCandidate.h"
112     #include "DataFormats/TrackReco/interface/Track.h"
113     #include "DataFormats/TrackReco/interface/TrackFwd.h"
114    
115     #include "PhysicsTools/FWLite/interface/CommandLineParser.h"
116     #include "DataFormats/EgammaCandidates/interface/Conversion.h"
117    
118    
119     #define TWOPI 6.283185308
120    
121     using namespace l1extra;
122     using namespace edm;
123     using namespace std;
124     using namespace reco;
125     //using namespace trigger;
126    
127    
128     class PtSorter {
129     public:
130     template <class T> bool operator() ( const T& a, const T& b ) {
131     return ( a.pt() > b.pt() );
132     }
133     };
134    
135    
136    
137     bool sort_pred(const std::pair<double,int> & left, const std::pair<double,int>& right)
138     {
139     return left.first < right.first;
140     }
141    
142    
143     RecoAnalyzer::RecoAnalyzer(const edm::ParameterSet& iConfig){
144    
145     cout<<"gettting parameters.." <<endl;
146     clusSeedThr_ = iConfig.getParameter<double> ("clusSeedThr");
147     clusSeedThrEndCap_ = iConfig.getParameter<double> ("clusSeedThrEndCap");
148    
149     clusEtaSize_ = iConfig.getParameter<int> ("clusEtaSize");
150     clusPhiSize_ = iConfig.getParameter<int> ("clusPhiSize");
151     if ( clusPhiSize_ % 2 == 0 || clusEtaSize_ % 2 == 0) {
152     edm::LogError("AlCaPi0RecHitsProducerError") << "Size of eta/phi for simple clustering should be odd numbers, reset to be 3 ";
153     clusPhiSize_ = 3;
154     clusEtaSize_ = 3;
155     }
156    
157    
158     removeSpike_ = iConfig.getUntrackedParameter<int>("removeSpike",0);
159    
160     doSelForPi0Barrel_ = iConfig.getParameter<bool> ("doSelForPi0Barrel");
161     if(doSelForPi0Barrel_){
162     barrelHits_ = iConfig.getParameter< edm::InputTag > ("barrelHits");
163     ///for Pi0 barrel selection
164     selePtGamma_ = iConfig.getParameter<double> ("selePtGamma");
165     selePtPi0_ = iConfig.getParameter<double> ("selePtPi0");
166     seleMinvMaxPi0_ = iConfig.getParameter<double> ("seleMinvMaxPi0");
167     seleMinvMinPi0_ = iConfig.getParameter<double> ("seleMinvMinPi0");
168     seleS4S9Gamma_ = iConfig.getParameter<double> ("seleS4S9Gamma");
169     selePi0BeltDR_ = iConfig.getParameter<double> ("selePi0BeltDR");
170     selePi0BeltDeta_ = iConfig.getParameter<double> ("selePi0BeltDeta");
171     selePi0Iso_ = iConfig.getParameter<double> ("selePi0Iso");
172     ptMinForIsolation_ = iConfig.getParameter<double>("ptMinForIsolation");
173    
174     }
175    
176     doSelForPi0Endcap_ = iConfig.getParameter<bool>("doSelForPi0Endcap");
177     if(doSelForPi0Endcap_){
178     endcapHits_ = iConfig.getParameter< edm::InputTag > ("endcapHits");
179    
180     ///for Pi0 endcap selection
181     /// selePtGammaEndCap_ = iConfig.getParameter<double> ("selePtGammaEndCap");
182     /// selePtPi0EndCap_ = iConfig.getParameter<double> ("selePtPi0EndCap");
183    
184     ///try to divide endcap region into 3 parts
185     /// eta< 2 ; eta>2 && eta<2.5 ; eta>2.5;
186     region1_Pi0EndCap_ = iConfig.getParameter<double> ("region1_Pi0EndCap");
187     selePtGammaPi0EndCap_region1_ = iConfig.getParameter<double> ("selePtGammaPi0EndCap_region1");
188     selePtPi0EndCap_region1_ = iConfig.getParameter<double> ("selePtPi0EndCap_region1");
189    
190     region2_Pi0EndCap_ = iConfig.getParameter<double> ("region2_Pi0EndCap");
191     selePtGammaPi0EndCap_region2_ = iConfig.getParameter<double> ("selePtGammaPi0EndCap_region2");
192     selePtPi0EndCap_region2_ = iConfig.getParameter<double> ("selePtPi0EndCap_region2");
193    
194     selePtGammaPi0EndCap_region3_ = iConfig.getParameter<double> ("selePtGammaPi0EndCap_region3");
195     selePtPi0EndCap_region3_ = iConfig.getParameter<double> ("selePtPi0EndCap_region3");
196 yangyong 1.2 selePtPi0MaxEndCap_region3_ = iConfig.getParameter<double> ("selePtPi0MaxEndCap_region3");
197 yangyong 1.1
198     seleS4S9GammaEndCap_ = iConfig.getParameter<double> ("seleS4S9GammaEndCap");
199     seleMinvMaxPi0EndCap_ = iConfig.getParameter<double> ("seleMinvMaxPi0EndCap");
200     seleMinvMinPi0EndCap_ = iConfig.getParameter<double> ("seleMinvMinPi0EndCap");
201     selePi0BeltDREndCap_ = iConfig.getParameter<double> ("selePi0BeltDREndCap");
202     selePi0BeltDetaEndCap_ = iConfig.getParameter<double> ("selePi0BeltDetaEndCap");
203    
204     selePi0IsoEndCap_ = iConfig.getParameter<double> ("selePi0IsoEndCap");
205    
206     preshHitProducer_ = iConfig.getParameter<edm::InputTag>("preshRecHitProducer");
207     ptMinForIsolationEndCap_ = iConfig.getParameter<double>("ptMinForIsolationEndCap");
208    
209     }
210    
211     doSelForEtaBarrel_ = iConfig.getParameter<bool> ("doSelForEtaBarrel");
212     if(doSelForEtaBarrel_){
213     barrelHitsEta_ = iConfig.getParameter< edm::InputTag > ("barrelHitsEta");
214     selePtGammaEta_ = iConfig.getParameter<double> ("selePtGammaEta");
215     selePtEta_ = iConfig.getParameter<double> ("selePtEta");
216     seleMinvMaxEta_ = iConfig.getParameter<double> ("seleMinvMaxEta");
217     seleMinvMinEta_ = iConfig.getParameter<double> ("seleMinvMinEta");
218     seleS4S9GammaEta_ = iConfig.getParameter<double> ("seleS4S9GammaEta");
219     seleS9S25GammaEta_ = iConfig.getParameter<double> ("seleS9S25GammaEta");
220    
221     seleEtaBeltDR_ = iConfig.getParameter<double> ("seleEtaBeltDR");
222     seleEtaBeltDeta_ = iConfig.getParameter<double> ("seleEtaBeltDeta");
223    
224     seleEtaIso_ = iConfig.getParameter<double> ("seleEtaIso");
225     ptMinForIsolationEta_ = iConfig.getParameter<double>("ptMinForIsolationEta");
226    
227     removePi0CandidatesForEta_ = iConfig.getParameter<bool>("removePi0CandidatesForEta");
228     massLowPi0Cand_ = iConfig.getParameter<double> ("massLowPi0Cand");
229     massHighPi0Cand_ = iConfig.getParameter<double> ("massHighPi0Cand");
230    
231     }
232    
233    
234     doSelForEtaEndcap_ = iConfig.getParameter<bool>("doSelForEtaEndcap");
235     if(doSelForEtaEndcap_){
236     endcapHitsEta_ = iConfig.getParameter< edm::InputTag > ("endcapHitsEta");
237    
238     ///for Eta endcap selection
239     /// selePtGammaEndCap_ = iConfig.getParameter<double> ("selePtGammaEndCap");
240     /// selePtEtaEndCap_ = iConfig.getParameter<double> ("selePtEtaEndCap");
241    
242     ///try to divide endcap region into 3 parts
243     /// eta< 2 ; eta>2 && eta<2.5 ; eta>2.5;
244     region1_EtaEndCap_ = iConfig.getParameter<double> ("region1_EtaEndCap");
245     selePtGammaEtaEndCap_region1_ = iConfig.getParameter<double> ("selePtGammaEtaEndCap_region1");
246     selePtEtaEndCap_region1_ = iConfig.getParameter<double> ("selePtEtaEndCap_region1");
247    
248     region2_EtaEndCap_ = iConfig.getParameter<double> ("region2_EtaEndCap");
249     selePtGammaEtaEndCap_region2_ = iConfig.getParameter<double> ("selePtGammaEtaEndCap_region2");
250     selePtEtaEndCap_region2_ = iConfig.getParameter<double> ("selePtEtaEndCap_region2");
251    
252     selePtGammaEtaEndCap_region3_ = iConfig.getParameter<double> ("selePtGammaEtaEndCap_region3");
253     selePtEtaEndCap_region3_ = iConfig.getParameter<double> ("selePtEtaEndCap_region3");
254 yangyong 1.2 selePtEtaMaxEndCap_region3_ = iConfig.getParameter<double> ("selePtEtaMaxEndCap_region3");
255 yangyong 1.1
256 yangyong 1.2
257 yangyong 1.1 seleS4S9GammaEtaEndCap_ = iConfig.getParameter<double> ("seleS4S9GammaEtaEndCap");
258     seleS9S25GammaEtaEndCap_ = iConfig.getParameter<double> ("seleS9S25GammaEtaEndCap");
259    
260     seleMinvMaxEtaEndCap_ = iConfig.getParameter<double> ("seleMinvMaxEtaEndCap");
261     seleMinvMinEtaEndCap_ = iConfig.getParameter<double> ("seleMinvMinEtaEndCap");
262     seleEtaBeltDREndCap_ = iConfig.getParameter<double> ("seleEtaBeltDREndCap");
263     seleEtaBeltDetaEndCap_ = iConfig.getParameter<double> ("seleEtaBeltDetaEndCap");
264     ptMinForIsolationEtaEndCap_ = iConfig.getParameter<double>("ptMinForIsolationEtaEndCap");
265    
266     seleEtaIsoEndCap_ = iConfig.getParameter<double> ("seleEtaIsoEndCap");
267     preshHitProducerEta_ = iConfig.getParameter<edm::InputTag>("preshRecHitProducerEta");
268     }
269    
270    
271     // input tag for L1GtTriggerMenuLite
272     m_l1GtTmLInputTag = iConfig.getParameter<edm::InputTag> ("L1GtTmLInputTag");
273    
274    
275     ///for storing rechits ES for each selected EE clusters.
276     storeRecHitES_ = iConfig.getParameter<bool>("storeRecHitES");
277     if(storeRecHitES_){
278     // maximum number of matched ES clusters (in each ES layer) to each BC
279     preshNclust_ = iConfig.getParameter<int>("preshNclust");
280     // min energy of ES clusters
281     preshClustECut = iConfig.getParameter<double>("preshClusterEnergyCut");
282     // algo params
283     double preshStripECut = iConfig.getParameter<double>("preshStripEnergyCut");
284     int preshSeededNst = iConfig.getParameter<int>("preshSeededNstrip");
285     // calibration parameters:
286     calib_planeX_ = iConfig.getParameter<double>("preshCalibPlaneX");
287     calib_planeY_ = iConfig.getParameter<double>("preshCalibPlaneY");
288     gamma_ = iConfig.getParameter<double>("preshCalibGamma");
289     mip_ = iConfig.getParameter<double>("preshCalibMIP");
290    
291     // The debug level
292     std::string debugString = iConfig.getParameter<std::string>("debugLevelES");
293     if (debugString == "DEBUG") debugL = PreshowerClusterAlgo::pDEBUG;
294     else if (debugString == "INFO") debugL = PreshowerClusterAlgo::pINFO;
295     else debugL = PreshowerClusterAlgo::pERROR;
296     // ES algo constructor:
297     presh_algo = new PreshowerClusterAlgo(preshStripECut,preshClustECut,preshSeededNst,debugL);
298    
299    
300     }
301    
302     InputDataFormat_ = iConfig.getParameter<int>("dataformat"); ///defautl input data
303    
304     m_l1GtRecordInputTag = iConfig.getParameter<edm::InputTag>("L1GtRecordInputTag");
305    
306    
307     posCalculator_ = PositionCalc( iConfig.getParameter<edm::ParameterSet>("posCalcParameters") );
308    
309     inputTag_ = iConfig.getParameter<edm::InputTag> ("TriggerResultsTag");
310    
311     outputFile_ = iConfig.getParameter<std::string>("outputFile");
312     rootFile_ = new TFile(outputFile_.c_str(),"RECREATE"); // open output file to store histograms
313    
314     std::cout<<"RecoAnalyzer initilized .." <<" "<<outputFile_.c_str()<<" opened."<<endl;
315    
316     std::cout<<"dataformat: "<< InputDataFormat_ <<endl;
317     alcaL1trigNames_ = iConfig.getParameter<vstring>("alcaL1trigNames");
318    
319     nEventsProcessed = 0;
320    
321     nErrorPrinted = 0;
322    
323     nPassedBSC = 0;
324     nPassedBSC_noBeamHalo = 0;
325    
326     for(int j=0; j< 200; j++){
327     nL1bits_fired[j] = 0;
328     nL1bitsTech_fired[j] = 0;
329     }
330    
331    
332     }
333    
334    
335     RecoAnalyzer::~RecoAnalyzer()
336     {
337    
338     if(storeRecHitES_){
339     delete presh_algo;
340     }
341    
342     cout<<"totalProcessed: "<< nEventsProcessed<< " BSC4041 "<< nPassedBSC <<" noBeamHalo "<< nPassedBSC_noBeamHalo <<endl;
343     // TimingReport::current()->dump(std::cout);
344    
345     }
346    
347    
348     // ------------ method called to produce the data ------------
349     void
350     RecoAnalyzer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
351     //RecoAnalyzer::analyze(edm::Event& iEvent, const edm::EventSetup& iSetup)
352     {
353    
354    
355     if( nEventsProcessed ==0){
356    
357    
358     // edm::ESHandle<L1GtTriggerMenu> menuRcd;
359     // iSetup.get<L1GtTriggerMenuRcd>().get(menuRcd) ;
360     // const L1GtTriggerMenu* menu = menuRcd.product();
361     // cout <<" L1 Menu Name: "<< menuRcd->gtTriggerMenuName()<<endl;
362     // for (CItAlgo algo = menu->gtAlgorithmMap().begin(); algo!=menu->gtAlgorithmMap().end(); ++algo) {
363     // cout<< "L1MenuPhy" << "Name: " << (algo->second).algoName() << " Alias: " << (algo->second).algoAlias() << std::endl;
364     // }
365     // for (CItAlgo techTrig = menu->gtTechnicalTriggerMap().begin(); techTrig != menu->gtTechnicalTriggerMap().end(); ++techTrig) {
366     // cout << "L1MenuTech" << (techTrig->second).algoName() << std::endl;
367     // }
368    
369     iSetup.get<EcalADCToGeVConstantRcd>().get(agc);
370     agcp = agc.product();
371     cout<<"agc: "<< agc->getEBValue() << " "<< agc->getEEValue() <<endl;
372    
373     }
374    
375    
376     edm::ESHandle<CaloGeometry> geoHandle;
377     iSetup.get<CaloGeometryRecord>().get(geoHandle);
378    
379    
380    
381    
382     ////////////////////for MC information of pi0s and eta////////////
383     nMCpart =0;
384     if( InputDataFormat_ < 10){
385    
386     for(int i=0; i<MAXMC; i++){ ///MC
387     pxMCpart[i] = 0;
388     pyMCpart[i] = 0;
389     pzMCpart[i] = 0;
390     ptotMCpart[i] = 0;
391     ptMCpart[i]=0;
392     etMCpart[i] = 0;
393     eMCpart[i] = 0;
394     etaMCpart[i] = -10;
395     phiMCpart[i] = -10;
396     pidMCpart[i] = 0;
397     pidmomMCpart[i] = 0;
398     vtxXMCpart[i] =0;
399     vtxYMCpart[i] =0;
400     vtxZMCpart[i] =0;
401     barcodemomMCpart[i]=-1;
402     statusMCpart[i] =-1;
403    
404     nDauMCpart[i] =0;
405     for( int j=0; j<MAXDAU; j++){
406     barcodeDauMCpart[i][j] = -1;
407     }
408    
409     convPhtMCpart[i][0] = 0;
410     convPhtMCpart[i][1] = -1;
411     convPhtMCpart[i][2] = -1;
412    
413     }
414    
415     }
416    
417     nGenpi0 = 0;
418     nGeneta = 0;
419    
420     procID = -1;
421     ptHAT = -1;
422    
423     npizallgen = 0;
424     netaallgen = 0;
425    
426     nChaGen = 0;
427    
428    
429    
430    
431     runNumber = iEvent.id().run();
432     evtNumber = iEvent.id().event();
433     lumiBlock = iEvent.luminosityBlock();
434     bunchX = iEvent.bunchCrossing();
435     orbitNumber = iEvent.orbitNumber();
436     const edm::Timestamp jtime = iEvent.time();
437     evtTime = jtime.value() >> 32;
438    
439    
440     /////*******************
441     //// Get generator Info
442     /////*******************
443    
444     if( InputDataFormat_ < 10){
445    
446     Handle<GenEventInfoProduct> geninfos;
447     try{
448     iEvent.getByLabel("generator",geninfos);
449    
450     procID = int( geninfos->signalProcessID());
451     ///this is q, not pthat.
452     /// ptHAT = geninfos->qScale();
453     ptHAT = geninfos->binningValues()[0]; //this is ptHAT
454    
455    
456     }catch(std::exception& ex ){
457     nErrorPrinted++;
458     if(nErrorPrinted< maxErrorToPrint) cout<<"generator not working.."<<endl;
459     }
460    
461    
462     Handle<GenParticleCollection> genPart;
463     try {
464     iEvent.getByLabel( "genParticles", genPart);
465     nMCpart = 0;
466     for(int i=0; i< int(genPart->size())&& nMCpart<MAXMC; i++){
467     const GenParticle & p = (*genPart)[i];
468     eMCpart[nMCpart] = p.energy();
469     ptotMCpart[nMCpart] = p.p();
470     pxMCpart[nMCpart] =p.px();
471     pyMCpart[nMCpart] =p.py();
472     pzMCpart[nMCpart] =p.pz();
473     ptMCpart[nMCpart] =p.pt();
474     etMCpart[nMCpart] = p.et();
475     etaMCpart[nMCpart] = p.eta();
476     phiMCpart[nMCpart] = p.phi();
477     mMCpart[nMCpart] = p.mass();
478     pidMCpart[nMCpart] = p.pdgId();
479     chargeMCpart[nMCpart] = p.charge();
480     statusMCpart[nMCpart] = p.status();
481     nDauMCpart[nMCpart] = p.numberOfDaughters();
482     vtxXMCpart[nMCpart] = p.vx();
483     vtxYMCpart[nMCpart] = p.vy();
484     vtxZMCpart[nMCpart] = p.vz();
485    
486    
487    
488     if(p.charge() !=0 && p.status() ==1 ){
489     ptChaGen[nChaGen] = p.pt();
490     etaChaGen[nChaGen] = p.eta();
491     pidChaGen[nChaGen] = p.pdgId();
492     chaChaGen[nChaGen] = p.charge();
493     staChaGen[nChaGen] = p.status();
494     nChaGen ++;
495     }
496    
497    
498     nMCpart++;
499     }
500     if(debug_>=1) cout<<"n total MCpart: "<<nMCpart<<endl;
501    
502     ///let's try to find the index of mother and daughters.
503     for(int i=0; i< int(genPart->size())&& nMCpart<MAXMC; i++){
504     const Candidate & p = (*genPart)[i];
505     ///don't look if it's not photon
506     // if( p.pdgId() != 22) continue;
507     int pid = p.pdgId();
508     if( pid != 22 && pid != 111 && pid != 221 ) continue;
509     const Candidate * mom = p.mother();
510     if( mom !=NULL){
511     pidmomMCpart[i] = mom->pdgId();
512     barcodemomMCpart[i] = indexofParticle(mom->px(),mom->pz(),mom->status());
513     }
514     }
515    
516    
517    
518     } catch( std::exception& ex){
519     nErrorPrinted++;
520     if(nErrorPrinted< maxErrorToPrint) cout<<"genparticle not working.."<<endl;
521    
522     }
523     // Simulated tracks (i.e. GEANT particles).
524     Handle<edm::SimTrackContainer> psimtracks;
525     // Get the associated vertices
526     Handle<SimVertexContainer> simvertices;
527     try{
528     iEvent.getByLabel("g4SimHits", simvertices);
529    
530     }catch( std::exception& ex){
531     nErrorPrinted++;
532     if(nErrorPrinted< maxErrorToPrint) cout<<" SimVertexContainer not working.."<<endl;
533    
534     }
535    
536    
537     try{
538     iEvent.getByLabel("g4SimHits", psimtracks);
539     // Need to check that SimTrackContainer is sorted; otherwise, copy and sort :-(
540     std::auto_ptr<SimTrackContainer> simtracksTmp;
541     const SimTrackContainer * simtracksSorted = &* psimtracks;
542     if (!__gnu_cxx::is_sorted(psimtracks->begin(), psimtracks->end(), LessById())) {
543     simtracksTmp.reset(new SimTrackContainer(*psimtracks));
544     std::sort(simtracksTmp->begin(), simtracksTmp->end(), LessById());
545     if(debug_ >=1) cout<<"get g4SimHits..sorted."<<endl;
546    
547     simtracksSorted = &* simtracksTmp;
548     }
549    
550    
551     int nMCpartGen = nMCpart;
552    
553    
554     map<unsigned int,int> map_simtrackid;
555    
556     // loop through all simParticles
557     for (SimTrackContainer::const_iterator iM = psimtracks->begin();
558     iM != psimtracks->end(); ++iM) {
559    
560     // Skip PYTHIA tracks.
561     if (iM->genpartIndex() != -1) continue;
562    
563     /// skip if it is not electron
564     if( iM->type() != 11 && iM->type() != -11) continue;
565    
566    
567    
568     if( nMCpart >= MAXMC) break;
569    
570     map_simtrackid.insert(make_pair(iM->trackId(),nMCpart));
571    
572     pxMCpart[nMCpart] = iM->momentum().px();
573     pyMCpart[nMCpart] = iM->momentum().py();
574     pzMCpart[nMCpart] = iM->momentum().pz();
575     ptMCpart[nMCpart] = iM->momentum().pt();
576     etaMCpart[nMCpart] = iM->momentum().eta();
577     phiMCpart[nMCpart] = iM->momentum().phi();
578     pidMCpart[nMCpart] = iM->type();
579    
580     chargeMCpart[nMCpart] = int(iM->charge());
581    
582     eMCpart[nMCpart] = sqrt(0.000511* 0.000511 + iM->momentum().px()*iM->momentum().px() + iM->momentum().py()*iM->momentum().py() + iM->momentum().pz()*iM->momentum().pz());
583    
584     statusMCpart[nMCpart] = -99;
585    
586     vtxXMCpart[nMCpart] = 0;
587     vtxYMCpart[nMCpart] = 0;
588     vtxZMCpart[nMCpart] = 0;
589    
590     if (! iM->noVertex()) {
591     const SimVertex &vtx = (*simvertices)[iM->vertIndex()];
592     vtxXMCpart[nMCpart] = vtx.position().x();
593     vtxYMCpart[nMCpart] = vtx.position().y();
594     vtxZMCpart[nMCpart] = vtx.position().z();
595     }
596    
597     barcodemomMCpart[nMCpart] = -2;
598     nMCpart ++;
599    
600     }
601    
602    
603     vector<int> inde1; ///+ /
604     vector<int> inde2; ///-
605     vector<int> indpht;
606    
607    
608    
609     int nSimTrack = 0;
610    
611     ///loop over again
612     // loop through all simParticles
613     for (SimTrackContainer::const_iterator iM = psimtracks->begin();
614     iM != psimtracks->end(); ++iM) {
615    
616     // Skip PYTHIA tracks.
617     if (iM->genpartIndex() != -1) continue;
618    
619     /// skip if it is not electron
620     if( iM->type() != 11 && iM->type() != -11) continue;
621    
622    
623     if( nMCpartGen + nSimTrack >= MAXMC) break;
624    
625    
626     barcodemomMCpart[nMCpartGen+nSimTrack] = -2;
627    
628    
629     // find simtrack that has a genParticle match to its parent
630     // Look at the production vertex. If there is no vertex, I can do nothing...
631     if (! iM->noVertex()) {
632     // Pick the vertex (isimtrk.vertIndex() is really an index)
633     const SimVertex &vtx = (*simvertices)[iM->vertIndex()];
634    
635     // Now note that vtx.parentIndex() is NOT an index, it's a track id, so I have to search for it
636     unsigned int idx = vtx.parentIndex();
637     SimTrackContainer::const_iterator it = std::lower_bound(simtracksSorted->begin(), simtracksSorted->end(), idx, LessById());
638    
639     if ((it != simtracksSorted->end()) && (it->trackId() == idx)) { //it is the parent sim track
640     if (it->genpartIndex() != -1) {
641    
642     barcodemomMCpart[nMCpartGen+nSimTrack] = it->genpartIndex() -1;
643    
644     convPhtMCpart[it->genpartIndex() -1][0] = 1; ///this gen photon converts
645    
646    
647     vector<int>::iterator iit = find(indpht.begin(),indpht.end(),it->genpartIndex() -1);
648     if( iit == indpht.end()){
649     indpht.push_back(it->genpartIndex() -1);
650     }
651     if( iM->type() == 11) inde1.push_back(nMCpartGen+nSimTrack);
652     else inde2.push_back(nMCpartGen+nSimTrack);
653    
654    
655     }else{
656     map<unsigned int,int>::iterator intt = map_simtrackid.find(idx);
657     if( intt != map_simtrackid.end()){
658     barcodemomMCpart[nMCpartGen+nSimTrack] = intt->second;
659     }else{
660     barcodemomMCpart[nMCpartGen+nSimTrack] = -3; ////check this if any
661     }
662    
663     }
664     } ///found parent in simtrack
665    
666     }
667    
668     nSimTrack ++;
669    
670    
671     } ///end of 2nd loop
672    
673    
674     ///now let's try to find for each indpht, it's correponding two electrons index
675     vector<int> pht;
676     vector<int> e1;
677     vector<int> e2;
678    
679    
680    
681     for(int j =0; j< int(indpht.size());j++){
682     int k = indpht[j];
683    
684     int found1 = -1;
685     int found2 = -1;
686     vector<int>::iterator it1= inde1.begin();
687     for(int j1 =0; j1< int(inde1.size()); j1++){
688     if(k == barcodemomMCpart[inde1[j1]]){
689     found1 = inde1[j1];
690     inde1.erase(it1+j1);
691     break;
692     }
693     }
694     vector<int>::iterator it2= inde2.begin();
695     for(int j2 =0; j2< int(inde2.size()); j2++){
696     if(k == barcodemomMCpart[inde2[j2]]){
697     found2 = inde2[j2];
698     inde2.erase(it2+j2);
699     break;
700     }
701     }
702    
703     convPhtMCpart[k][1] = found1;
704     convPhtMCpart[k][2] = found2;
705    
706    
707     if( found1 > 0 && found2 >0) convPhtMCpart[k][0] = 2; /// both electron found in sim
708     else if( found1 >0 && found2 < 0) convPhtMCpart[k][0] = 3; /// only one found in sim
709     else if( found1 <0 && found2 > 0) convPhtMCpart[k][0] = 3; /// only one found in sim
710     else if( found1 <0 && found2 < 0) convPhtMCpart[k][0] = -10; /// neither found in sim
711    
712     }
713    
714    
715     }catch( std::exception& ex){
716     nErrorPrinted++;
717     if(nErrorPrinted< maxErrorToPrint) cout<<"SimTrackContainer not working.."<<endl;
718    
719     }
720    
721     findgenpi0eta();
722    
723    
724     } ///If for MC samples
725    
726    
727    
728    
729     /////*******************
730     //// Get Level-1 Info
731     /////*******************
732    
733     edm::Handle<L1GlobalTriggerReadoutRecord> gtReadoutRecord;
734     m_l1GtUtils.retrieveL1EventSetup(iSetup);
735     m_l1GtUtils.retrieveL1GtTriggerMenuLite(iEvent, m_l1GtTmLInputTag);
736    
737     int iErrorCode = -1;
738     vector<int> l1res;
739     nL1Alca = int(alcaL1trigNames_.size());
740     for(int j=0; j< nL1Alca; j++){
741     string m_nameAlgTechTrig = alcaL1trigNames_[j];
742     bool decisionBeforeMaskAlgTechTrig = m_l1GtUtils.decisionBeforeMask(iEvent, m_nameAlgTechTrig, iErrorCode);
743     if (iErrorCode == 0) {
744     L1Alca[j] = int(decisionBeforeMaskAlgTechTrig);
745     } else if (iErrorCode == 1) {
746     l1res.push_back(-1);
747     L1Alca[j] = -1;
748     } else {
749     L1Alca[j] = -2;
750     }
751    
752     }
753    
754     try{
755     iEvent.getByLabel(m_l1GtRecordInputTag , gtReadoutRecord);
756     DecisionWord gtDecisionWord = gtReadoutRecord->decisionWord();
757    
758     nL1bits = int( gtDecisionWord.size());
759     if(debug_>=1) cout<<"nL1bits; "<<nL1bits<<" # physics triggers: "<< L1GlobalTriggerReadoutSetup::NumberPhysTriggers <<endl;
760     for (int iBit = 0; iBit < nL1bits; ++iBit) {
761     L1bits[iBit] = gtDecisionWord[iBit] ;
762     if(L1bits[iBit] ==1) nL1bits_fired[iBit] ++;
763     }
764     ///acess techinical trigger
765     const TechnicalTriggerWord& technicalTriggerWordBeforeMask = gtReadoutRecord->technicalTriggerWord();
766     //// Replacing L1GlobalTriggerReadoutRecord with L1GlobalTriggerRecord and the corresponding label, one gets the values after masking.
767     nL1bitsTech = int(technicalTriggerWordBeforeMask.size());
768     for(int iBit = 0; iBit < nL1bitsTech; ++iBit) {
769     L1bitsTech[iBit] = technicalTriggerWordBeforeMask.at(iBit);
770     if( L1bitsTech[iBit] ==1) nL1bitsTech_fired[iBit] ++;
771     }
772     }catch(std::exception& ex ){
773     nErrorPrinted++;
774     if(nErrorPrinted< maxErrorToPrint) cout<<"L1 record not working.."<<endl;
775     }
776    
777    
778    
779     if( nEventsProcessed % 10000 ==0) {
780     cout<<"nEventProcessed: "<< nEventsProcessed<< endl;
781     }
782     nEventsProcessed ++;
783    
784    
785    
786     /////*******************
787     //// Get HLT Info
788     /////*******************
789     Handle<TriggerResults> trh;
790    
791     nHLTbits = 0;
792     try {
793     iEvent.getByLabel(inputTag_, trh);
794     if(trh.isValid()) {
795     nHLTbits = int(trh->size());
796     if(nHLTbits>MAXHLTbits){
797     cout<<"nhltbits exeed max: "<<nHLTbits<<endl;
798     nHLTbits=MAXHLTbits;
799     }
800     for(int i=0; i< int(trh->size()) && i<MAXHLTbits; i++) {
801     //hltbits.push_back((*trh)[i].accept());
802     HLTbits[i] = (*trh)[i].accept();
803     }
804     }
805     }catch(std::exception& ex ){
806     nErrorPrinted++;
807     if(nErrorPrinted< maxErrorToPrint) cout<<" HLT not working.."<<endl;
808     }
809    
810    
811     /////*******************
812     //// Selection and root-trees for Pi0 in the barrel
813     /////*******************
814    
815     if( doSelForPi0Barrel_){
816    
817     try{
818     Handle<EBRecHitCollection> barrelRecHitsHandle;
819     iEvent.getByLabel(barrelHits_,barrelRecHitsHandle);
820     const EcalRecHitCollection *hitCollection_p = barrelRecHitsHandle.product();
821    
822     if(debug_>=2) cout<<"making cluster pi0 barrel.." <<" "<< hitCollection_p->size()<<endl;
823    
824     makeNxNClusters(iEvent,iSetup,hitCollection_p, reco::CaloID::DET_ECAL_BARREL);
825    
826     if(debug_>=2) cout<<"do selection pi0 barrel.." <<endl;
827    
828     doSelectionAndFillTree(iEvent,iSetup,reco::CaloID::DET_ECAL_BARREL,false,PIZ,seleS4S9Gamma_,-999,seleMinvMinPi0_,seleMinvMaxPi0_,selePi0BeltDeta_,selePi0BeltDR_,ptMinForIsolation_,selePi0Iso_);
829    
830    
831     }catch(std::exception& ex ){
832     nErrorPrinted++;
833     if(nErrorPrinted< maxErrorToPrint) cout<<"barrelRecHits NA.."<<endl;
834     }
835    
836     }
837    
838    
839     /////*******************
840     //// Selection and root-trees for Eta in the barrel
841     /////*******************
842    
843     if(doSelForEtaBarrel_){
844     try{
845     Handle<EBRecHitCollection> barrelRecHitsHandle;
846     iEvent.getByLabel(barrelHitsEta_,barrelRecHitsHandle);
847     const EcalRecHitCollection *hitCollection_p = barrelRecHitsHandle.product();
848    
849     if(debug_>=2) cout<<"making cluster eta barrel.."<<" "<< hitCollection_p->size()<<endl;
850    
851     makeNxNClusters(iEvent,iSetup,hitCollection_p, reco::CaloID::DET_ECAL_BARREL);
852    
853     if(debug_>=2) cout<<"do selection eta barrel.." <<endl;
854    
855     doSelectionAndFillTree(iEvent,iSetup,reco::CaloID::DET_ECAL_BARREL,removePi0CandidatesForEta_,ETA,seleS4S9GammaEta_,seleS9S25GammaEta_,seleMinvMinEta_,seleMinvMaxEta_,seleEtaBeltDeta_,seleEtaBeltDR_,ptMinForIsolationEta_,seleEtaIso_);
856    
857    
858     }catch(std::exception& ex ){
859     nErrorPrinted++;
860     if(nErrorPrinted< maxErrorToPrint) cout<<"barrelRecHitsEta NA.."<<endl;
861     }
862     }
863    
864    
865     /////*******************
866     //// Selection and root-trees for Pi0 in the endcap
867     /////*******************
868    
869     if(doSelForPi0Endcap_){
870     try{
871     Handle<EERecHitCollection> endcapRecHitsHandle;
872     iEvent.getByLabel(endcapHits_,endcapRecHitsHandle);
873     const EcalRecHitCollection *hitCollection_p = endcapRecHitsHandle.product();
874    
875     makeNxNClusters(iEvent,iSetup,hitCollection_p, reco::CaloID::DET_ECAL_ENDCAP);
876    
877     if(debug_>=2) cout<<"do selection pi0 endcap.." <<endl;
878    
879     doSelectionAndFillTree(iEvent,iSetup,reco::CaloID::DET_ECAL_ENDCAP,false,PIZ,seleS4S9GammaEndCap_,-999,seleMinvMinPi0EndCap_,seleMinvMaxPi0EndCap_,selePi0BeltDetaEndCap_,selePi0BeltDREndCap_,ptMinForIsolationEndCap_,selePi0IsoEndCap_);
880    
881    
882     }catch(std::exception& ex ){
883     nErrorPrinted++;
884 yangyong 1.2 if(nErrorPrinted< maxErrorToPrint) cout<<"encapRecHitsPi0 NA.."<<endl;
885 yangyong 1.1 }
886     }
887    
888     /////*******************
889     //// Selection and root-trees for Eta in the endcap
890     /////*******************
891    
892     if(doSelForEtaEndcap_){
893     try{
894     Handle<EERecHitCollection> endcapRecHitsHandle;
895     iEvent.getByLabel(endcapHitsEta_,endcapRecHitsHandle);
896     const EcalRecHitCollection *hitCollection_p = endcapRecHitsHandle.product();
897    
898     makeNxNClusters(iEvent,iSetup,hitCollection_p, reco::CaloID::DET_ECAL_ENDCAP);
899    
900     if(debug_>=2) cout<<"do selection eta endcap.." <<endl;
901    
902    
903     doSelectionAndFillTree(iEvent,iSetup,reco::CaloID::DET_ECAL_ENDCAP,false,ETA,seleS4S9GammaEtaEndCap_,seleS9S25GammaEtaEndCap_,seleMinvMinEtaEndCap_,seleMinvMaxEtaEndCap_,seleEtaBeltDetaEndCap_,seleEtaBeltDREndCap_,ptMinForIsolationEtaEndCap_,seleEtaIsoEndCap_);
904    
905    
906     }catch(std::exception& ex ){
907     nErrorPrinted++;
908     if(nErrorPrinted< maxErrorToPrint) cout<<"endcapRecHitsEta NA.."<<endl;
909     }
910     }
911    
912    
913     }
914    
915    
916    
917     void RecoAnalyzer::beginJob()
918     {
919    
920    
921     rootFile_->cd();
922    
923     if( doSelForPi0Barrel_ ){
924    
925     mytree_pizeb = new TTree("pizSelb","Reco Simple Analysis");
926     mytree_pizeb->Branch("lumiBlock",&lumiBlock,"lumiBlock/I");
927     mytree_pizeb->Branch("runNumber",&runNumber,"runNumber/I");
928     mytree_pizeb->Branch("evtNumber",&evtNumber,"evtNumber/I");
929    
930     // bunchX ,, those are never used. ??
931     //mytree_pizeb->Branch("bunchX",&bunchX,"bunchX/I");
932     //mytree_pizeb->Branch("orbitNumber",&orbitNumber,"orbitNumber/I");
933     mytree_pizeb->Branch("evtTime",&evtTime,"evtTime/I");
934    
935     mytree_pizeb->Branch("nL1Alca",&nL1Alca,"nL1Alca/I");
936     mytree_pizeb->Branch("L1Alca",L1Alca,"L1Alca[nL1Alca]/I");
937    
938    
939     mytree_pizeb->Branch("mpair",&mpair,"mpair/F");
940     mytree_pizeb->Branch("ptpair",&ptpair,"ptpair/F");
941     mytree_pizeb->Branch("etapair",&etapair,"etapair/F");
942     ///mytree_pizeb->Branch("phipair",&phipair,"phipair/F");
943     mytree_pizeb->Branch("ptmin",&ptmin,"ptmin/F");
944     mytree_pizeb->Branch("isolation",&isolation,"isolation/F");
945    
946     mytree_pizeb->Branch("s4s9min",&s4s9min,"s4s9min/F");
947     //mytree_pizeb->Branch("s9s25min",&s9s25min,"s9s25min/F");
948    
949    
950    
951     mytree_pizeb->Branch("nxtClus1",&nxtClus1,"nxtClus1/I");
952     mytree_pizeb->Branch("eXtalClus1",eXtalClus1,"eXtalClus1[nxtClus1]/F");
953     mytree_pizeb->Branch("ietaXtalClus1",ietaXtalClus1,"ietaXtalClus1[nxtClus1]/I");
954     mytree_pizeb->Branch("iphiXtalClus1",iphiXtalClus1,"iphiXtalClus1[nxtClus1]/I");
955     mytree_pizeb->Branch("tXtalClus1",tXtalClus1,"tXtalClus1[nxtClus1]/F"); //timing
956    
957     mytree_pizeb->Branch("nxtClus2",&nxtClus2,"nxtClus2/I");
958     mytree_pizeb->Branch("eXtalClus2",eXtalClus2,"eXtalClus2[nxtClus2]/F");
959     mytree_pizeb->Branch("ietaXtalClus2",ietaXtalClus2,"ietaXtalClus2[nxtClus2]/I");
960     mytree_pizeb->Branch("iphiXtalClus2",iphiXtalClus2,"iphiXtalClus2[nxtClus2]/I");
961     mytree_pizeb->Branch("tXtalClus2",tXtalClus2,"tXtalClus2[nxtClus2]/F"); //timing
962    
963     ///gen info
964     if( InputDataFormat_ <10){
965     mytree_pizeb->Branch("genMatched",&genMatched,"genMatched/I");
966     mytree_pizeb->Branch("geninfo",geninfo,"geninfo[6]/F");
967     mytree_pizeb->Branch("convinfo",convinfo,"convinfo[2]/I");
968     }
969    
970     }
971    
972    
973    
974     if( doSelForPi0Endcap_ ){
975    
976     mytree_pizee = new TTree("pizSele","Reco Simple Analysis");
977     mytree_pizee->Branch("lumiBlock",&lumiBlock,"lumiBlock/I");
978     mytree_pizee->Branch("runNumber",&runNumber,"runNumber/I");
979     mytree_pizee->Branch("evtNumber",&evtNumber,"evtNumber/I");
980     // bunchX
981     //mytree_pizee->Branch("bunchX",&bunchX,"bunchX/I");
982     // mytree_pizee->Branch("orbitNumber",&orbitNumber,"orbitNumber/I");
983     mytree_pizee->Branch("evtTime",&evtTime,"evtTime/I");
984    
985    
986     mytree_pizee->Branch("nL1Alca",&nL1Alca,"nL1Alca/I");
987     mytree_pizee->Branch("L1Alca",L1Alca,"L1Alca[nL1Alca]/I");
988    
989    
990    
991     mytree_pizee->Branch("mpair",&mpair,"mpair/F");
992     mytree_pizee->Branch("ptpair",&ptpair,"ptpair/F");
993     mytree_pizee->Branch("etapair",&etapair,"etapair/F");
994     ////mytree_pizee->Branch("phipair",&phipair,"phipair/F");
995     mytree_pizee->Branch("ptmin",&ptmin,"ptmin/F");
996     mytree_pizee->Branch("isolation",&isolation,"isolation/F");
997    
998     mytree_pizee->Branch("s4s9min",&s4s9min,"s4s9min/F");
999     //mytree_pizee->Branch("s9s25min",&s9s25min,"s9s25min/F");
1000    
1001    
1002    
1003     mytree_pizee->Branch("nxtClus1",&nxtClus1,"nxtClus1/I");
1004     mytree_pizee->Branch("eXtalClus1",eXtalClus1,"eXtalClus1[nxtClus1]/F");
1005     mytree_pizee->Branch("ietaXtalClus1",ietaXtalClus1,"ietaXtalClus1[nxtClus1]/I");
1006     mytree_pizee->Branch("iphiXtalClus1",iphiXtalClus1,"iphiXtalClus1[nxtClus1]/I");
1007     mytree_pizee->Branch("izXtalClus1",&izXtalClus1,"izXtalClus1/I");
1008     mytree_pizee->Branch("tXtalClus1",tXtalClus1,"tXtalClus1[nxtClus1]/F"); //timing
1009    
1010     mytree_pizee->Branch("nxtClus2",&nxtClus2,"nxtClus2/I");
1011     mytree_pizee->Branch("eXtalClus2",eXtalClus2,"eXtalClus2[nxtClus2]/F");
1012     mytree_pizee->Branch("izXtalClus2",&izXtalClus2,"izXtalClus2/I");
1013     mytree_pizee->Branch("ietaXtalClus2",ietaXtalClus2,"ietaXtalClus2[nxtClus2]/I");
1014     mytree_pizee->Branch("iphiXtalClus2",iphiXtalClus2,"iphiXtalClus2[nxtClus2]/I");
1015     mytree_pizee->Branch("tXtalClus2",tXtalClus2,"tXtalClus2[nxtClus2]/F"); //timing
1016    
1017    
1018     //PreshowerS infoESX[2][8], infoESY[2][8],
1019     mytree_pizee->Branch("infoESX",infoESX,"infoESX[2][8]/F"); //timing
1020     mytree_pizee->Branch("infoESY",infoESY,"infoESY[2][8]/F"); //timing
1021    
1022    
1023     ///gen info
1024     if( InputDataFormat_ <10){
1025     mytree_pizee->Branch("genMatched",&genMatched,"genMatched/I");
1026     mytree_pizee->Branch("geninfo",geninfo,"geninfo[6]/F");
1027     mytree_pizee->Branch("convinfo",convinfo,"convinfo[2]/I");
1028    
1029     }
1030    
1031     }
1032    
1033    
1034    
1035    
1036     if( doSelForEtaBarrel_ ){
1037    
1038     mytree_etaeb = new TTree("etaSelb","Reco Simple Analysis");
1039     mytree_etaeb->Branch("lumiBlock",&lumiBlock,"lumiBlock/I");
1040     mytree_etaeb->Branch("runNumber",&runNumber,"runNumber/I");
1041     mytree_etaeb->Branch("evtNumber",&evtNumber,"evtNumber/I");
1042     // bunchX
1043    
1044     //mytree_etaeb->Branch("bunchX",&bunchX,"bunchX/I");
1045     // mytree_etaeb->Branch("orbitNumber",&orbitNumber,"orbitNumber/I");
1046     mytree_etaeb->Branch("evtTime",&evtTime,"evtTime/I");
1047    
1048    
1049     mytree_etaeb->Branch("nL1Alca",&nL1Alca,"nL1Alca/I");
1050     mytree_etaeb->Branch("L1Alca",L1Alca,"L1Alca[nL1Alca]/I");
1051    
1052    
1053    
1054     mytree_etaeb->Branch("mpair",&mpair,"mpair/F");
1055     mytree_etaeb->Branch("ptpair",&ptpair,"ptpair/F");
1056     mytree_etaeb->Branch("etapair",&etapair,"etapair/F");
1057     ///mytree_etaeb->Branch("phipair",&phipair,"phipair/F");
1058     mytree_etaeb->Branch("ptmin",&ptmin,"ptmin/F");
1059     mytree_etaeb->Branch("isolation",&isolation,"isolation/F");
1060    
1061     mytree_etaeb->Branch("s4s9min",&s4s9min,"s4s9min/F");
1062     mytree_etaeb->Branch("s9s25min",&s9s25min,"s9s25min/F");
1063    
1064    
1065    
1066     mytree_etaeb->Branch("nxtClus1",&nxtClus1,"nxtClus1/I");
1067     mytree_etaeb->Branch("eXtalClus1",eXtalClus1,"eXtalClus1[nxtClus1]/F");
1068     mytree_etaeb->Branch("ietaXtalClus1",ietaXtalClus1,"ietaXtalClus1[nxtClus1]/I");
1069     mytree_etaeb->Branch("iphiXtalClus1",iphiXtalClus1,"iphiXtalClus1[nxtClus1]/I");
1070     mytree_etaeb->Branch("tXtalClus1",tXtalClus1,"tXtalClus1[nxtClus1]/F"); //timing
1071    
1072     mytree_etaeb->Branch("nxtClus2",&nxtClus2,"nxtClus2/I");
1073     mytree_etaeb->Branch("eXtalClus2",eXtalClus2,"eXtalClus2[nxtClus2]/F");
1074     mytree_etaeb->Branch("ietaXtalClus2",ietaXtalClus2,"ietaXtalClus2[nxtClus2]/I");
1075     mytree_etaeb->Branch("iphiXtalClus2",iphiXtalClus2,"iphiXtalClus2[nxtClus2]/I");
1076     mytree_etaeb->Branch("tXtalClus2",tXtalClus2,"tXtalClus2[nxtClus2]/F"); //timing
1077    
1078     ///5x5
1079     mytree_etaeb->Branch("nxt5x5Clus1",&nxt5x5Clus1,"nxt5x5Clus1/I");
1080     mytree_etaeb->Branch("eXtal5x5Clus1",eXtal5x5Clus1,"eXtal5x5Clus1[nxt5x5Clus1]/F");
1081     mytree_etaeb->Branch("ietaXtal5x5Clus1",ietaXtal5x5Clus1,"ietaXtal5x5Clus1[nxt5x5Clus1]/I");
1082     mytree_etaeb->Branch("iphiXtal5x5Clus1",iphiXtal5x5Clus1,"iphiXtal5x5Clus1[nxt5x5Clus1]/I");
1083     mytree_etaeb->Branch("tXtal5x5Clus1",tXtal5x5Clus1,"tXtal5x5Clus1[nxt5x5Clus1]/F"); //timing
1084    
1085     mytree_etaeb->Branch("nxt5x5Clus2",&nxt5x5Clus2,"nxt5x5Clus2/I");
1086     mytree_etaeb->Branch("eXtal5x5Clus2",eXtal5x5Clus2,"eXtal5x5Clus2[nxt5x5Clus2]/F");
1087     mytree_etaeb->Branch("ietaXtal5x5Clus2",ietaXtal5x5Clus2,"ietaXtal5x5Clus2[nxt5x5Clus2]/I");
1088     mytree_etaeb->Branch("iphiXtal5x5Clus2",iphiXtal5x5Clus2,"iphiXtal5x5Clus2[nxt5x5Clus2]/I");
1089     mytree_etaeb->Branch("tXtal5x5Clus2",tXtal5x5Clus2,"tXtal5x5Clus2[nxt5x5Clus2]/F"); //timing
1090    
1091    
1092    
1093     ///gen info
1094     if( InputDataFormat_ <10){
1095     mytree_etaeb->Branch("genMatched",&genMatched,"genMatched/I");
1096     mytree_etaeb->Branch("geninfo",geninfo,"geninfo[6]/F");
1097     mytree_etaeb->Branch("convinfo",convinfo,"convinfo[2]/I");
1098     }
1099    
1100     }
1101    
1102    
1103     if( doSelForEtaEndcap_ ){
1104    
1105     mytree_etaee = new TTree("etaSele","Reco Simple Analysis");
1106     mytree_etaee->Branch("lumiBlock",&lumiBlock,"lumiBlock/I");
1107     mytree_etaee->Branch("runNumber",&runNumber,"runNumber/I");
1108     mytree_etaee->Branch("evtNumber",&evtNumber,"evtNumber/I");
1109     // bunchX
1110     //mytree_etaee->Branch("bunchX",&bunchX,"bunchX/I");
1111     // mytree_etaee->Branch("orbitNumber",&orbitNumber,"orbitNumber/I");
1112     mytree_etaee->Branch("evtTime",&evtTime,"evtTime/I");
1113    
1114     mytree_etaee->Branch("nL1Alca",&nL1Alca,"nL1Alca/I");
1115     mytree_etaee->Branch("L1Alca",L1Alca,"L1Alca[nL1Alca]/I");
1116    
1117     mytree_etaee->Branch("mpair",&mpair,"mpair/F");
1118     mytree_etaee->Branch("ptpair",&ptpair,"ptpair/F");
1119     mytree_etaee->Branch("etapair",&etapair,"etapair/F");
1120     ///mytree_etaee->Branch("phipair",&phipair,"phipair/F");
1121     mytree_etaee->Branch("ptmin",&ptmin,"ptmin/F");
1122     mytree_etaee->Branch("isolation",&isolation,"isolation/F");
1123    
1124     mytree_etaee->Branch("s4s9min",&s4s9min,"s4s9min/F");
1125     mytree_etaee->Branch("s9s25min",&s9s25min,"s9s25min/F");
1126     ///mytree_etaee->Branch("s9s25min",&s9s25min,"s9s25min/F");
1127    
1128    
1129    
1130     mytree_etaee->Branch("nxtClus1",&nxtClus1,"nxtClus1/I");
1131     mytree_etaee->Branch("eXtalClus1",eXtalClus1,"eXtalClus1[nxtClus1]/F");
1132     mytree_etaee->Branch("ietaXtalClus1",ietaXtalClus1,"ietaXtalClus1[nxtClus1]/I");
1133     mytree_etaee->Branch("iphiXtalClus1",iphiXtalClus1,"iphiXtalClus1[nxtClus1]/I");
1134     mytree_etaee->Branch("tXtalClus1",tXtalClus1,"tXtalClus1[nxtClus1]/F"); //timing
1135     mytree_etaee->Branch("izXtalClus1",&izXtalClus1,"izXtalClus1/I");
1136    
1137     mytree_etaee->Branch("nxtClus2",&nxtClus2,"nxtClus2/I");
1138     mytree_etaee->Branch("eXtalClus2",eXtalClus2,"eXtalClus2[nxtClus2]/F");
1139     mytree_etaee->Branch("ietaXtalClus2",ietaXtalClus2,"ietaXtalClus2[nxtClus2]/I");
1140     mytree_etaee->Branch("iphiXtalClus2",iphiXtalClus2,"iphiXtalClus2[nxtClus2]/I");
1141     mytree_etaee->Branch("tXtalClus2",tXtalClus2,"tXtalClus2[nxtClus2]/F"); //timing
1142     mytree_etaee->Branch("izXtalClus2",&izXtalClus2,"izXtalClus2/I");
1143    
1144    
1145     ///5x5
1146     mytree_etaee->Branch("nxt5x5Clus1",&nxt5x5Clus1,"nxt5x5Clus1/I");
1147     mytree_etaee->Branch("eXtal5x5Clus1",eXtal5x5Clus1,"eXtal5x5Clus1[nxt5x5Clus1]/F");
1148     mytree_etaee->Branch("ietaXtal5x5Clus1",ietaXtal5x5Clus1,"ietaXtal5x5Clus1[nxt5x5Clus1]/I");
1149     mytree_etaee->Branch("iphiXtal5x5Clus1",iphiXtal5x5Clus1,"iphiXtal5x5Clus1[nxt5x5Clus1]/I");
1150     ////mytree_etaee->Branch("iphiXtal5x5Clus1",iphiXtal5x5Clus1,"izXtal5x5Clus1[nxt5x5Clus1]/I"); for 5x5 don't save iz, the same as
1151     mytree_etaee->Branch("tXtal5x5Clus1",tXtal5x5Clus1,"tXtal5x5Clus1[nxt5x5Clus1]/F"); //timing
1152    
1153     mytree_etaee->Branch("nxt5x5Clus2",&nxt5x5Clus2,"nxt5x5Clus2/I");
1154     mytree_etaee->Branch("eXtal5x5Clus2",eXtal5x5Clus2,"eXtal5x5Clus2[nxt5x5Clus2]/F");
1155     mytree_etaee->Branch("ietaXtal5x5Clus2",ietaXtal5x5Clus2,"ietaXtal5x5Clus2[nxt5x5Clus2]/I");
1156     mytree_etaee->Branch("iphiXtal5x5Clus2",iphiXtal5x5Clus2,"iphiXtal5x5Clus2[nxt5x5Clus2]/I");
1157     mytree_etaee->Branch("tXtal5x5Clus2",tXtal5x5Clus2,"tXtal5x5Clus2[nxt5x5Clus2]/F"); //timing
1158    
1159     //PreshowerS infoESX[2][8], infoESY[2][8],
1160     mytree_etaee->Branch("infoESX",infoESX,"infoESX[2][8]/F"); //timing
1161     mytree_etaee->Branch("infoESY",infoESY,"infoESY[2][8]/F"); //timing
1162    
1163     ///gen info
1164     if( InputDataFormat_ <10){
1165     mytree_etaee->Branch("genMatched",&genMatched,"genMatched/I");
1166     mytree_etaee->Branch("geninfo",geninfo,"geninfo[6]/F");
1167     mytree_etaee->Branch("convinfo",convinfo,"convinfo[2]/I");
1168     }
1169    
1170     }
1171    
1172    
1173    
1174    
1175     }
1176    
1177    
1178    
1179     // ------------ method called once each job just after ending the event loop ------------
1180    
1181     void
1182     RecoAnalyzer::endJob() {
1183    
1184    
1185     rootFile_->cd();
1186    
1187    
1188    
1189     if(doSelForPi0Barrel_){
1190     mytree_pizeb->Write();
1191     }
1192     if(doSelForPi0Endcap_){
1193     mytree_pizee->Write();
1194     }
1195    
1196     if(doSelForEtaBarrel_){
1197     mytree_etaeb->Write();
1198     }
1199     if(doSelForEtaEndcap_){
1200     mytree_etaee->Write();
1201     }
1202    
1203    
1204     rootFile_->Write() ;
1205     rootFile_->Close() ;
1206    
1207    
1208    
1209    
1210    
1211     }
1212    
1213    
1214    
1215    
1216    
1217    
1218     void RecoAnalyzer::convxtalid(int &nphi,int &neta)
1219     {
1220     // Barrel only
1221     // Output nphi 0...359; neta 0...84; nside=+1 (for eta>0), or 0 (for eta<0).
1222     // neta will be [-85,-1] , or [0,84], the minus sign indicates the z<0 side.
1223    
1224     if(neta > 0) neta -= 1;
1225     if(nphi > 359) nphi=nphi-360;
1226    
1227     // final check
1228     if(nphi >359 || nphi <0 || neta< -85 || neta > 84)
1229     {
1230     std::cout <<" unexpected fatal error in RecoAnalyzer::convxtalid "<< nphi << " " << neta << " " <<std::endl;
1231     //exit(1);
1232     }
1233     } //end of convxtalid
1234    
1235    
1236    
1237    
1238     int RecoAnalyzer::diff_neta_s(int neta1, int neta2){
1239     int mdiff;
1240     mdiff=(neta1-neta2);
1241     return mdiff;
1242     }
1243    
1244     // Calculate the distance in xtals taking into account the periodicity of the Barrel
1245     int RecoAnalyzer::diff_nphi_s(int nphi1,int nphi2) {
1246     int mdiff;
1247     if(abs(nphi1-nphi2) < (360-abs(nphi1-nphi2))) {
1248     mdiff=nphi1-nphi2;
1249     }
1250     else {
1251     mdiff=360-abs(nphi1-nphi2);
1252     if(nphi1>nphi2) mdiff=-mdiff;
1253     }
1254     return mdiff;
1255     }
1256    
1257    
1258    
1259    
1260     double RecoAnalyzer::DeltaPhi(double v1, double v2)
1261     { // Computes the correctly normalized phi difference
1262     // v1, v2 = phi of object 1 and 2
1263     double diff = fabs(v2 - v1);
1264     double corr = 2*acos(-1.) - diff; //// 2*pi - diff;
1265     if (diff < acos(-1.)){ return diff;} else { return corr;}
1266    
1267     }
1268    
1269    
1270     double RecoAnalyzer::GetDeltaR(double eta1, double eta2, double phi1, double phi2){
1271     // Computes the DeltaR of two objects from their eta and phi values
1272    
1273     return sqrt( (eta1-eta2)*(eta1-eta2)
1274     + DeltaPhi(phi1, phi2)*DeltaPhi(phi1, phi2) );
1275    
1276     }
1277    
1278     double RecoAnalyzer::getcosd(double eta1, double phi1, double eta2, double phi2) {
1279     double theta1 = 2*atan(exp(-eta1));
1280     double theta2 = 2*atan(exp(-eta2));
1281     double cosd;
1282     double dphi = DeltaPhi(phi1,phi2);
1283     cosd = cos(theta1)*cos(theta2)+sin(theta1)*sin(theta2)*cos(dphi); //opening angle
1284     return cosd;
1285     }
1286    
1287    
1288    
1289     int RecoAnalyzer::indexofParticle(float px, float pz, int status){
1290    
1291     float err = 0.00001;
1292    
1293     // int ind = -10;
1294    
1295     vector<int> nn;
1296    
1297     for( int j=0; j<nMCpart; j++){
1298     if(fabs(px-pxMCpart[j])<err && fabs(pz-pzMCpart[j])<err
1299     && status == statusMCpart[j]){
1300     nn.push_back(j);
1301     }
1302     }
1303     // if(int(nn.size()) !=1){ ///diquarks or gluon has two copy sometimesss!!!
1304     // cout<<"wrong! indexofParticle: n: "<<int(nn.size())<<endl;
1305     // for(int j=0; j<int(nn.size()); j++){
1306     // cout<<" dup j: "<<j<<" ind "<<nn[j]<<endl;
1307     // }
1308     // }
1309     return nn[0];
1310    
1311     }
1312    
1313    
1314     int RecoAnalyzer::getMotherIndex(int j){
1315     if(j<0) {
1316     cout<<"no mother. input -1!!!!!!!!"<<endl;
1317     // cout<<"entry: "<<entry<<" input j: "<<j<<endl;
1318     // exit(1);
1319     return -1;
1320     }
1321    
1322     int indmom = barcodemomMCpart[j];
1323     if(indmom>=0){
1324     int st = statusMCpart[indmom];
1325     int indf = indmom;
1326     if(st==3 && pidMCpart[indmom]==pidMCpart[j]){ //if ==3 && it's itself. trace back more.
1327     indf = barcodemomMCpart[indmom];
1328     if(indf>=0){
1329     st = statusMCpart[indf];
1330     }
1331     else cout<<"warning! ptcl has no mother!"<<endl;
1332     }
1333     return indf;
1334    
1335     }
1336     else{
1337     return -1;
1338     }
1339    
1340     }
1341    
1342    
1343    
1344    
1345     void RecoAnalyzer::findgenpi0eta(){
1346    
1347     indpi0Gen.clear();
1348     indetaGen.clear();
1349     indpht1pi0Gen.clear();
1350     indpht2pi0Gen.clear();
1351     indpht1etaGen.clear();
1352     indpht2etaGen.clear();
1353    
1354    
1355     int pidPi0 = 111;
1356     int pidPht = 22;
1357     int pidEta = 221;
1358    
1359     ////float eta_max = 1000000;
1360     // float et_min = 2.0;
1361    
1362     ////float epht_min = 0.1;
1363    
1364     ///epht_min = 0.;
1365    
1366    
1367     ////txtout<<"entry: "<<entry<<endl;
1368    
1369     vector<int> indphtpi0Gen;
1370     vector<int> indphtetaGen;
1371    
1372     int npi0all = 0;
1373     int netaall = 0;
1374    
1375    
1376     for( int j=0; j<nMCpart; j++){
1377    
1378    
1379     if( pidMCpart[j] == pidPi0 ) npi0all++;
1380     if( pidMCpart[j] == pidEta ) netaall++;
1381     if( statusMCpart[j] ==1 && pidMCpart[j] == pidPht ){
1382     int indmom = getMotherIndex(j);
1383    
1384     if(pidMCpart[indmom] == pidPi0){
1385     indphtpi0Gen.push_back(j);
1386     }
1387     if(pidMCpart[indmom] == pidEta){
1388     indphtetaGen.push_back(j);
1389     }
1390     } //end of found one photon
1391    
1392     }
1393    
1394     vector< std::pair<double,int> > indpiz_eta;
1395     vector< std::pair<double,int> > indeta_eta;
1396    
1397     int npi0_sel = 0;
1398     int neta_sel = 0;
1399    
1400    
1401     for( int j=0; j<int(indphtpi0Gen.size());j++){
1402     int ind = getMotherIndex(indphtpi0Gen[j]);
1403     int nsame = 0;
1404     vector<int> isame;
1405     for(int k=0; k<int(indphtpi0Gen.size());k++){
1406     if( k!=j && getMotherIndex(indphtpi0Gen[k]) == ind) {
1407     isame.push_back(k);
1408     nsame++;
1409     }
1410     }
1411     if(nsame==1){
1412     int ind1 =indphtpi0Gen[j];
1413     int ind2 = indphtpi0Gen[isame[0]];
1414    
1415     indpht1pi0Gen.push_back(ind1);
1416     indpht2pi0Gen.push_back(ind2);
1417    
1418     indpi0Gen.push_back(ind);
1419     indphtpi0Gen.erase(indphtpi0Gen.begin()+j);
1420     indphtpi0Gen.erase(indphtpi0Gen.begin()+isame[0]-1);
1421    
1422     indpiz_eta.push_back( make_pair(fabs(etaMCpart[ind]), npi0_sel));
1423     npi0_sel ++;
1424    
1425    
1426     j = j-1; ////start from previous j again becasue size(indphtpi0Gen) decreased by 2.
1427     }
1428    
1429     }
1430     /////eta
1431     for( int j=0; j<int(indphtetaGen.size());j++){
1432     int ind = getMotherIndex(indphtetaGen[j]);
1433     int nsame = 0;
1434     vector<int> isame;
1435     for(int k=0; k<int(indphtetaGen.size());k++){
1436     if( k!=j && getMotherIndex(indphtetaGen[k]) == ind) {
1437     isame.push_back(k);
1438     nsame++;
1439     }
1440     }
1441     if(nsame==1){
1442     int ind1 =indphtetaGen[j];
1443     int ind2 = indphtetaGen[isame[0]];
1444    
1445     indpht1etaGen.push_back(ind1);
1446     indpht2etaGen.push_back(ind2);
1447    
1448     indetaGen.push_back(ind);
1449     indphtetaGen.erase(indphtetaGen.begin()+j);
1450     indphtetaGen.erase(indphtetaGen.begin()+isame[0]-1);
1451    
1452     indeta_eta.push_back( make_pair(fabs(etaMCpart[ind]), neta_sel) );
1453     neta_sel ++;
1454    
1455     j = j-1; ////start from previous j again becasue size(indphtpi0Gen) decreased by 2.
1456     }
1457    
1458     }
1459    
1460    
1461     //fillthe tree
1462    
1463     sort(indpiz_eta.begin(),indpiz_eta.end(),sort_pred);
1464     sort(indeta_eta.begin(),indeta_eta.end(),sort_pred);
1465    
1466    
1467     nGenpi0 = 0;
1468    
1469    
1470     if(debug_>=1) cout<<"ngenpi0: "<< npi0all <<" "<< int(indpi0Gen.size())<<endl;
1471    
1472    
1473     for( int j=0; j<int(indpiz_eta.size()) && nGenpi0<MAXGenPIZ; j++){
1474     int k = indpiz_eta[j].second;
1475    
1476    
1477     int indmc = indpi0Gen[k];
1478     int indph1 = indpht1pi0Gen[k];
1479     int indph2 = indpht2pi0Gen[k];
1480    
1481    
1482     eGenpi0[nGenpi0] = eMCpart[indmc];
1483     phiGenpi0[nGenpi0] = phiMCpart[indmc];
1484     etaGenpi0[nGenpi0] = etaMCpart[indmc];
1485    
1486    
1487     vtxGenpi0[nGenpi0][0] = vtxXMCpart[indmc];
1488     vtxGenpi0[nGenpi0][1] = vtxYMCpart[indmc];
1489     vtxGenpi0[nGenpi0][2] = vtxZMCpart[indmc];
1490    
1491     barcodeMomGenpi0[nGenpi0]= getMotherIndex(indmc);
1492     pidMomGenpi0[nGenpi0]= pidMCpart[barcodeMomGenpi0[nGenpi0]];
1493    
1494    
1495     ///a littel difference
1496     vtxPhtGenpi0[nGenpi0][0] = vtxXMCpart[indph1];
1497     vtxPhtGenpi0[nGenpi0][1] = vtxYMCpart[indph1];
1498     vtxPhtGenpi0[nGenpi0][2] = vtxZMCpart[indph1];
1499    
1500    
1501     ePhtGenpi0[nGenpi0][0] = eMCpart[indph1];
1502     ePhtGenpi0[nGenpi0][1] = eMCpart[indph2];
1503     etaPhtGenpi0[nGenpi0][0] = etaMCpart[indph1];
1504     etaPhtGenpi0[nGenpi0][1] = etaMCpart[indph2];
1505     phiPhtGenpi0[nGenpi0][0] = phiMCpart[indph1];
1506     phiPhtGenpi0[nGenpi0][1] = phiMCpart[indph2];
1507    
1508     isConvPhtGenpi0[nGenpi0][0] = convPhtMCpart[indph1][0];
1509     isConvPhtGenpi0[nGenpi0][1] = convPhtMCpart[indph2][0];
1510    
1511     int e1 = convPhtMCpart[indph1][1];
1512     int e2 = convPhtMCpart[indph1][2];
1513    
1514     if( e1 >0){
1515     convPht1Genpi0[nGenpi0][0] = eMCpart[e1];
1516     convPht1Genpi0[nGenpi0][1] = etaMCpart[e1];
1517     convPht1Genpi0[nGenpi0][2] = phiMCpart[e1];
1518    
1519    
1520     }
1521     if( e2 >0){
1522     convPht1Genpi0[nGenpi0][3] = eMCpart[e2];
1523     convPht1Genpi0[nGenpi0][4] = etaMCpart[e2];
1524     convPht1Genpi0[nGenpi0][5] = phiMCpart[e2];
1525     }
1526    
1527     for(int jj=0; jj<6; jj++){
1528     convVtxPhtGenpi0[nGenpi0][jj] = 0;
1529     }
1530     if( e1 >0){
1531     convVtxPhtGenpi0[nGenpi0][0] = vtxXMCpart[e1];
1532     convVtxPhtGenpi0[nGenpi0][1] = vtxYMCpart[e1];
1533     convVtxPhtGenpi0[nGenpi0][2] = vtxZMCpart[e1];
1534     }else if( e2 >0){
1535     convVtxPhtGenpi0[nGenpi0][0] = vtxXMCpart[e2];
1536     convVtxPhtGenpi0[nGenpi0][1] = vtxYMCpart[e2];
1537     convVtxPhtGenpi0[nGenpi0][2] = vtxZMCpart[e2];
1538     }else{
1539     convVtxPhtGenpi0[nGenpi0][0] = 0;
1540     convVtxPhtGenpi0[nGenpi0][1] = 0;
1541     convVtxPhtGenpi0[nGenpi0][2] = 0;
1542     }
1543    
1544    
1545    
1546     e1 = convPhtMCpart[indph2][1];
1547     e2 = convPhtMCpart[indph2][2];
1548     if( e1 >0){
1549     convPht2Genpi0[nGenpi0][0] = eMCpart[e1];
1550     convPht2Genpi0[nGenpi0][1] = etaMCpart[e1];
1551     convPht2Genpi0[nGenpi0][2] = phiMCpart[e1];
1552     }
1553     if( e2 >0){
1554     convPht2Genpi0[nGenpi0][3] = eMCpart[e2];
1555     convPht2Genpi0[nGenpi0][4] = etaMCpart[e2];
1556     convPht2Genpi0[nGenpi0][5] = phiMCpart[e2];
1557     }
1558     if( e1 >0){
1559     convVtxPhtGenpi0[nGenpi0][3] = vtxXMCpart[e1];
1560     convVtxPhtGenpi0[nGenpi0][4] = vtxYMCpart[e1];
1561     convVtxPhtGenpi0[nGenpi0][5] = vtxZMCpart[e1];
1562     }else if( e2 >0){
1563     convVtxPhtGenpi0[nGenpi0][3] = vtxXMCpart[e2];
1564     convVtxPhtGenpi0[nGenpi0][4] = vtxYMCpart[e2];
1565     convVtxPhtGenpi0[nGenpi0][5] = vtxZMCpart[e2];
1566     }else {
1567     convVtxPhtGenpi0[nGenpi0][3] = 0;
1568     convVtxPhtGenpi0[nGenpi0][4] = 0;
1569     convVtxPhtGenpi0[nGenpi0][5] = 0;
1570     }
1571    
1572     nGenpi0++;
1573    
1574    
1575     }
1576    
1577    
1578     if(debug_>=1) cout<<"ngenpi0gg: "<< npi0all <<" "<< int(indpi0Gen.size())<<" "<< nGenpi0 <<endl;
1579    
1580    
1581    
1582     nGeneta = 0;
1583    
1584    
1585     for( int j=0; j<int(indeta_eta.size()) && nGeneta<MAXGenPIZ; j++){
1586     int k = indeta_eta[j].second;
1587    
1588    
1589     int indmc = indetaGen[k];
1590     int indph1 = indpht1etaGen[k];
1591     int indph2 = indpht2etaGen[k];
1592    
1593     eGeneta[nGeneta] = eMCpart[indmc];
1594     phiGeneta[nGeneta] = phiMCpart[indmc];
1595     etaGeneta[nGeneta] = etaMCpart[indmc];
1596    
1597     vtxGeneta[nGeneta][0] = vtxXMCpart[indmc];
1598     vtxGeneta[nGeneta][1] = vtxYMCpart[indmc];
1599     vtxGeneta[nGeneta][2] = vtxZMCpart[indmc];
1600     vtxPhtGeneta[nGeneta][0] = vtxXMCpart[indph1];
1601     vtxPhtGeneta[nGeneta][1] = vtxYMCpart[indph1];
1602     vtxPhtGeneta[nGeneta][2] = vtxZMCpart[indph1];
1603    
1604    
1605     barcodeMomGeneta[nGeneta] = getMotherIndex(indmc);
1606     pidMomGeneta[nGeneta]= pidMCpart[barcodeMomGeneta[nGeneta]];
1607    
1608    
1609    
1610     ePhtGeneta[nGeneta][0] = eMCpart[indph1];
1611     ePhtGeneta[nGeneta][1] = eMCpart[indph2];
1612     etaPhtGeneta[nGeneta][0] = etaMCpart[indph1];
1613     etaPhtGeneta[nGeneta][1] = etaMCpart[indph2];
1614     phiPhtGeneta[nGeneta][0] = phiMCpart[indph1];
1615     phiPhtGeneta[nGeneta][1] = phiMCpart[indph2];
1616    
1617    
1618    
1619     isConvPhtGeneta[nGeneta][0] = convPhtMCpart[indph1][0];
1620     isConvPhtGeneta[nGeneta][1] = convPhtMCpart[indph2][0];
1621    
1622     int e1 = convPhtMCpart[indph1][1];
1623     int e2 = convPhtMCpart[indph1][2];
1624    
1625     if( e1 >0){
1626     convPht1Geneta[nGeneta][0] = eMCpart[e1];
1627     convPht1Geneta[nGeneta][1] = etaMCpart[e1];
1628     convPht1Geneta[nGeneta][2] = phiMCpart[e1];
1629     }else{
1630     convPht1Geneta[nGeneta][0] = 0;
1631     convPht1Geneta[nGeneta][1] = 0;
1632     convPht1Geneta[nGeneta][2] = 0;
1633     }
1634     if( e2 >0){
1635     convPht1Geneta[nGeneta][3] = eMCpart[e2];
1636     convPht1Geneta[nGeneta][4] = etaMCpart[e2];
1637     convPht1Geneta[nGeneta][5] = phiMCpart[e2];
1638     }else{
1639     convPht1Geneta[nGeneta][3] = 0;
1640     convPht1Geneta[nGeneta][4] = 0;
1641     convPht1Geneta[nGeneta][5] = 0;
1642     }
1643    
1644    
1645     if( e1 >0){
1646     convVtxPhtGeneta[nGeneta][0] = vtxXMCpart[e1];
1647     convVtxPhtGeneta[nGeneta][1] = vtxYMCpart[e1];
1648     convVtxPhtGeneta[nGeneta][2] = vtxZMCpart[e1];
1649     }else if( e2 >0){
1650     convVtxPhtGeneta[nGeneta][0] = vtxXMCpart[e2];
1651     convVtxPhtGeneta[nGeneta][1] = vtxYMCpart[e2];
1652     convVtxPhtGeneta[nGeneta][2] = vtxZMCpart[e2];
1653     }else{
1654     convVtxPhtGeneta[nGeneta][0] = 0;
1655     convVtxPhtGeneta[nGeneta][1] = 0;
1656     convVtxPhtGeneta[nGeneta][2] = 0;
1657     }
1658    
1659    
1660     e1 = convPhtMCpart[indph2][1];
1661     e2 = convPhtMCpart[indph2][2];
1662     if( e1 >0){
1663     convPht2Geneta[nGeneta][0] = eMCpart[e1];
1664     convPht2Geneta[nGeneta][1] = etaMCpart[e1];
1665     convPht2Geneta[nGeneta][2] = phiMCpart[e1];
1666     }else{
1667     convPht2Geneta[nGeneta][0] = 0;
1668     convPht2Geneta[nGeneta][1] = 0;
1669     convPht2Geneta[nGeneta][2] = 0;
1670     }
1671     if( e2 >0){
1672     convPht2Geneta[nGeneta][3] = eMCpart[e2];
1673     convPht2Geneta[nGeneta][4] = etaMCpart[e2];
1674     convPht2Geneta[nGeneta][5] = phiMCpart[e2];
1675     }else{
1676     convPht2Geneta[nGeneta][3] = 0;
1677     convPht2Geneta[nGeneta][4] = 0;
1678     convPht2Geneta[nGeneta][5] = 0;
1679     }
1680    
1681    
1682     if( e1 >0){
1683     convVtxPhtGeneta[nGeneta][3] = vtxXMCpart[e1];
1684     convVtxPhtGeneta[nGeneta][4] = vtxYMCpart[e1];
1685     convVtxPhtGeneta[nGeneta][5] = vtxZMCpart[e1];
1686     }else if( e2 >0){
1687     convVtxPhtGeneta[nGeneta][3] = vtxXMCpart[e2];
1688     convVtxPhtGeneta[nGeneta][4] = vtxYMCpart[e2];
1689     convVtxPhtGeneta[nGeneta][5] = vtxZMCpart[e2];
1690     }else{
1691     convVtxPhtGeneta[nGeneta][3] = 0;
1692     convVtxPhtGeneta[nGeneta][4] = 0;
1693     convVtxPhtGeneta[nGeneta][5] = 0;
1694     }
1695    
1696    
1697     nGeneta++;
1698    
1699     }
1700    
1701    
1702    
1703    
1704     npizallgen = npi0all;
1705     netaallgen = netaall;
1706    
1707    
1708     }
1709    
1710    
1711     ////transform eta ( z, pho), to eta at ecal ( w.r.t 0,0,0,)
1712     double RecoAnalyzer::ecalEta(double EtaParticle ,double Zvertex, double RhoVertex){
1713    
1714    
1715     // const Double_t PI = 3.1415927;
1716     double PI = acos(-1);
1717    
1718     //---Definitions for ECAL
1719     double R_ECAL = 136.5;
1720     double Z_Endcap = 328.0;
1721     double etaBarrelEndcap = 1.479;
1722    
1723     if (EtaParticle!= 0.)
1724     {
1725     double Theta = 0.0 ;
1726     double ZEcal = (R_ECAL-RhoVertex)*sinh(EtaParticle)+Zvertex;
1727    
1728     if(ZEcal != 0.0) Theta = atan(R_ECAL/ZEcal);
1729     if(Theta<0.0) Theta = Theta+PI;
1730    
1731     double ETA = - log(tan(0.5*Theta));
1732    
1733     if( fabs(ETA) > etaBarrelEndcap )
1734     {
1735     double Zend = Z_Endcap ;
1736     if(EtaParticle<0.0 ) Zend = -Zend ;
1737     double Zlen = Zend - Zvertex ;
1738     double RR = Zlen/sinh(EtaParticle);
1739     Theta = atan((RR+RhoVertex)/Zend);
1740     if(Theta<0.0) Theta = Theta+PI;
1741     ETA = - log(tan(0.5*Theta));
1742     }
1743     return ETA;
1744     }
1745     else
1746     {
1747     return EtaParticle;
1748     }
1749     }
1750    
1751    
1752    
1753     double RecoAnalyzer::ecalPhi(double phi,double x0,double y0){
1754    
1755     //double R_ECAL = 136.5; ///cm
1756     double r = 136.5;
1757    
1758     double r0 = sqrt(x0*x0 + y0*y0);
1759    
1760     if(r0<1E-5) return phi;
1761    
1762     if( r0 >= r){
1763     cout<<"warning. ecalPhi vtx outside ecal return input" << r0 <<" "<< r <<endl;
1764     return phi;
1765     }
1766    
1767     double theta0 ;
1768     if(fabs(y0)>0) theta0= y0/fabs(y0) * acos(x0/r0);
1769     else theta0 = acos(x0/r0);
1770    
1771     double theta = phi + asin( r0/r *sin(theta0-phi));
1772    
1773     double PI = acos(-1);
1774     while ( theta < -PI) theta += PI;
1775     while ( theta > PI) theta -= PI;
1776    
1777     return theta;
1778    
1779    
1780     }
1781    
1782    
1783     void RecoAnalyzer::makeNxNClusters(const edm::Event &evt, const edm::EventSetup &es,
1784     const EcalRecHitCollection *hits, const reco::CaloID::Detectors detector){
1785    
1786    
1787    
1788     nClus = 0;
1789    
1790     eClus.clear();
1791     etaClus.clear();
1792     phiClus.clear();
1793     thetaClus.clear();
1794     etClus.clear();
1795     s4s9Clus.clear();
1796     s9s25Clus.clear();
1797     xClus.clear();
1798     yClus.clear();
1799     zClus.clear();
1800    
1801    
1802     RecHitsCluster.clear();
1803     RecHitsCluster5x5.clear();
1804    
1805    
1806     std::vector<EcalRecHit> seeds;
1807    
1808     double clusterSeedThreshold ;
1809     if (detector == reco::CaloID::DET_ECAL_BARREL){
1810     clusterSeedThreshold = clusSeedThr_;
1811     }else{
1812     clusterSeedThreshold = clusSeedThrEndCap_;
1813     }
1814    
1815    
1816     for(EcalRecHitCollection::const_iterator itt = hits->begin(); itt != hits->end(); itt++){
1817     double energy = itt->energy();
1818     if (energy > clusterSeedThreshold ) seeds.push_back(*itt);
1819     }
1820    
1821    
1822     // get the geometry and topology from the event setup:
1823     edm::ESHandle<CaloGeometry> geoHandle;
1824     es.get<CaloGeometryRecord>().get(geoHandle);
1825    
1826     const CaloSubdetectorGeometry *geometry_p;
1827     CaloSubdetectorTopology *topology_p;
1828     if (detector == reco::CaloID::DET_ECAL_BARREL) {
1829     geometry_p = geoHandle->getSubdetectorGeometry(DetId::Ecal, EcalBarrel);
1830     topology_p = new EcalBarrelTopology(geoHandle);
1831     }else {
1832     geometry_p = geoHandle->getSubdetectorGeometry(DetId::Ecal, EcalEndcap);
1833     topology_p = new EcalEndcapTopology(geoHandle);
1834     }
1835    
1836     const CaloSubdetectorGeometry *geometryES_p;
1837     geometryES_p = geoHandle->getSubdetectorGeometry(DetId::Ecal, EcalPreshower);
1838    
1839    
1840     std::vector<reco::BasicCluster> clusters;
1841     std::vector<DetId> usedXtals;
1842     // sort seed according to Energy
1843     sort(seeds.begin(), seeds.end(), ecalRecHitLess());
1844    
1845    
1846     for (std::vector<EcalRecHit>::iterator itseed=seeds.begin(); itseed!=seeds.end(); itseed++) {
1847     DetId seed_id = itseed->id();
1848     std::vector<DetId>::const_iterator usedIds;
1849    
1850     ///sed not yet used
1851     std::vector<DetId>::iterator itdet = find(usedXtals.begin(),usedXtals.end(),seed_id);
1852     if(itdet != usedXtals.end()) continue;
1853    
1854     std::vector<DetId> clus_v = topology_p->getWindow(seed_id,clusEtaSize_,clusPhiSize_);
1855     float clus_energy = 0;
1856     vector<EcalRecHit> RecHitsInWindow;
1857     vector<EcalRecHit> RecHitsInWindow5x5;
1858     vector<DetId> DetIdInWindow;
1859     std::vector<std::pair<DetId, float> > clus_used;
1860    
1861     for (std::vector<DetId>::iterator det=clus_v.begin(); det!=clus_v.end(); det++) {
1862     DetId detid = *det;
1863    
1864     //not yet used
1865     std::vector<DetId>::iterator itdet = find(usedXtals.begin(),usedXtals.end(),detid);
1866     if(itdet != usedXtals.end()) continue;
1867     //inside the collection
1868     EcalRecHitCollection::const_iterator hit = hits->find(detid);
1869     if( hit == hits->end()) continue;
1870     usedXtals.push_back(detid);
1871     clus_used.push_back(std::pair<DetId, float>(detid, 1.) );
1872     clus_energy += hit->energy();
1873    
1874    
1875     RecHitsInWindow.push_back(*hit);
1876     DetIdInWindow.push_back(detid);
1877    
1878    
1879     } //// end of making one nxn simple cluster
1880    
1881     if( clus_energy <= 0 ) continue;
1882    
1883    
1884     math::XYZPoint clus_pos = posCalculator_.Calculate_Location(clus_used,hits,geometry_p,geometryES_p);
1885    
1886     sort(RecHitsInWindow.begin(), RecHitsInWindow.end(), ecalRecHitLess());
1887    
1888     if (debug_>=2 ) LogDebug("")<<"nxn_cluster in run "<< evt.id().run()<<" event "<<evt.id().event()<<" energy: "<<clus_energy <<" eta: "<< clus_pos.Eta()<<" phi: "<< clus_pos.Phi()<<" nRecHits: "<< RecHitsInWindow.size()<<endl;
1889    
1890    
1891     int seedx; /// = seed_id.ieta();
1892     int seedy; /// = seed_id.iphi();
1893    
1894     if (detector == reco::CaloID::DET_ECAL_BARREL) {
1895     EBDetId ebd = EBDetId(seed_id);
1896     seedx = ebd.ieta();
1897     seedy = ebd.iphi();
1898     convxtalid(seedy,seedx);
1899    
1900     }else{
1901     EEDetId eed = EEDetId(seed_id);
1902     seedx = eed.ix();
1903     seedy = eed.iy();
1904     }
1905    
1906    
1907     float e2x2[4]={0};
1908     float e3x3 = 0;
1909     float e5x5 = 0;
1910    
1911     std::vector<DetId> clus_v5x5;
1912     clus_v5x5 = topology_p->getWindow(seed_id,5,5);
1913    
1914     for( std::vector<DetId>::const_iterator idItr = clus_v5x5.begin(); idItr != clus_v5x5.end(); idItr++){
1915     DetId ed = *idItr;
1916     EcalRecHitCollection::const_iterator rit = hits ->find( ed );
1917     if ( rit == hits ->end() ) continue;
1918     float energy = (*rit).energy();
1919     e5x5 += energy;
1920    
1921     //check if already in 3x3 , so no need to push_back here
1922     std::vector<DetId>::const_iterator already3x3 = find(DetIdInWindow.begin(),DetIdInWindow.end(),ed);
1923     if( already3x3 != DetIdInWindow.end() ) continue;
1924     RecHitsInWindow5x5.push_back( *rit);
1925     }
1926    
1927    
1928     for(unsigned int j=0; j<RecHitsInWindow.size();j++){
1929     DetId det = RecHitsInWindow[j].id();
1930     int dx,dy;
1931     if (detector == reco::CaloID::DET_ECAL_BARREL) {
1932     EBDetId ebd = EBDetId(det);
1933     int x = ebd.ieta();
1934     int y = ebd.iphi();
1935     convxtalid(y,x);
1936     dx = diff_neta_s(x,seedx);
1937     dy = diff_nphi_s(y,seedy);
1938     }else{
1939     EEDetId eed = EEDetId(det);
1940     int x = eed.ix();
1941     int y = eed.iy();
1942     dx = x-seedx;
1943     dy = y-seedy;
1944     }
1945     float energy = RecHitsInWindow[j].energy();
1946     if(abs(dx)<=1 && abs(dy)<=1){
1947     if(dx <= 0 && dy <=0) e2x2[0] += energy;
1948     if(dx >= 0 && dy <=0) e2x2[1] += energy;
1949     if(dx <= 0 && dy >=0) e2x2[2] += energy;
1950     if(dx >= 0 && dy >=0) e2x2[3] += energy;
1951     e3x3 += energy;
1952     }
1953     }
1954    
1955     if (detector == reco::CaloID::DET_ECAL_BARREL ) {
1956     if( removeSpike_==1){ ///check E9 >2GeV && Emax/E9 > 0.95
1957     if( clus_energy >2 && e3x3>0 && RecHitsInWindow[0].energy() / e3x3 > 0.95) continue;
1958     }
1959     }
1960    
1961     ///Now fill the vector of everything
1962     eClus.push_back( clus_energy);
1963     etaClus.push_back( clus_pos.Eta());
1964     phiClus.push_back( clus_pos.Phi());
1965     thetaClus.push_back(2. * atan(exp(-clus_pos.Eta())));
1966     etClus.push_back( clus_energy * sin(2. * atan(exp(-clus_pos.Eta()))));
1967    
1968     float s4max = *max_element( e2x2,e2x2+4);
1969     s4s9Clus.push_back( s4max/ e3x3);
1970     float s9s25 = e5x5 !=0 ? e3x3/e5x5:-1;
1971     s9s25Clus.push_back(s9s25);
1972     xClus.push_back( clus_pos.X());
1973     yClus.push_back( clus_pos.Y());
1974     zClus.push_back( clus_pos.Z());
1975    
1976    
1977     RecHitsCluster.push_back(RecHitsInWindow);
1978     RecHitsCluster5x5.push_back(RecHitsInWindow5x5);
1979    
1980     nClus ++;
1981    
1982    
1983     }
1984    
1985    
1986    
1987     }
1988    
1989    
1990    
1991     void RecoAnalyzer::doSelectionAndFillTree(const edm::Event &evt, const edm::EventSetup &es, const reco::CaloID::Detectors detector, bool rmPiz, int pizEta, double s4s9Cut, double s9s25Cut, double minvMinCut, double minvMaxCut, double isodetaCut, double isodrCut, double isoptCut, double isoCut){
1992    
1993    
1994     //
1995    
1996     double ptminCut = 0 ;
1997     double ptpairCut = 0 ;/// double s4s9Cut, double s9s25Cut, double minvMinCut, double minvMaxCut, double isodetaCut, double isodrCut, double isoptCut, double isoCut;
1998     double ptminCut_endcap_region1 = 0;
1999     double ptminCut_endcap_region2 = 0 ;
2000     double ptminCut_endcap_region3 = 0 ;
2001     double ptpairCut_endcap_region1 = 0 ;
2002     double ptpairCut_endcap_region2 = 0 ;
2003     double ptpairCut_endcap_region3 = 0 ;
2004 yangyong 1.2 double ptpairMaxCut_endcap_region3 = 0 ;
2005    
2006 yangyong 1.1
2007     double eta_endcap_region1 = 0 ;
2008     double eta_endcap_region2 = 0 ;
2009    
2010    
2011     if( detector == reco::CaloID::DET_ECAL_ENDCAP){
2012    
2013     if(pizEta == PIZ){
2014    
2015     ptminCut_endcap_region1 = selePtGammaPi0EndCap_region1_;
2016     ptminCut_endcap_region2 = selePtGammaPi0EndCap_region2_;
2017     ptminCut_endcap_region3 = selePtGammaPi0EndCap_region3_;
2018    
2019     ptpairCut_endcap_region1 = selePtPi0EndCap_region1_;
2020     ptpairCut_endcap_region2 = selePtPi0EndCap_region2_;
2021     ptpairCut_endcap_region3 = selePtPi0EndCap_region3_;
2022 yangyong 1.2 ptpairMaxCut_endcap_region3 = selePtPi0MaxEndCap_region3_;
2023    
2024    
2025 yangyong 1.1 eta_endcap_region1 = region1_Pi0EndCap_;
2026     eta_endcap_region2 = region2_Pi0EndCap_;
2027    
2028     }else{
2029    
2030     ptminCut_endcap_region1 = selePtGammaEtaEndCap_region1_;
2031     ptminCut_endcap_region2 = selePtGammaEtaEndCap_region2_;
2032     ptminCut_endcap_region3 = selePtGammaEtaEndCap_region3_;
2033    
2034     ptpairCut_endcap_region1 = selePtEtaEndCap_region1_;
2035     ptpairCut_endcap_region2 = selePtEtaEndCap_region2_;
2036     ptpairCut_endcap_region3 = selePtEtaEndCap_region3_;
2037 yangyong 1.2 ptpairMaxCut_endcap_region3 = selePtEtaMaxEndCap_region3_;
2038    
2039 yangyong 1.1 eta_endcap_region1 = region1_EtaEndCap_;
2040     eta_endcap_region2 = region2_EtaEndCap_;
2041    
2042     }
2043    
2044     }else{
2045     if(pizEta == PIZ){
2046     ptminCut = selePtGamma_;
2047     ptpairCut = selePtPi0_;
2048     }else{
2049     ptminCut = selePtGammaEta_;
2050     ptpairCut = selePtEta_;
2051     }
2052     }
2053    
2054     const CaloSubdetectorGeometry *geometry_es;
2055     CaloSubdetectorTopology *topology_es=0;
2056     if( detector == reco::CaloID::DET_ECAL_ENDCAP){
2057     edm::ESHandle<CaloGeometry> geoHandle;
2058     es.get<CaloGeometryRecord>().get(geoHandle);
2059     geometry_es = geoHandle->getSubdetectorGeometry(DetId::Ecal, EcalPreshower);
2060     topology_es = new EcalPreshowerTopology(geoHandle);
2061     }
2062    
2063     esrechits_map.clear();
2064    
2065    
2066     if( detector == reco::CaloID::DET_ECAL_ENDCAP && storeRecHitES_){
2067     Handle<ESRecHitCollection> esRecHitsHandle;
2068     try{
2069     if(pizEta==1){
2070     evt.getByLabel(preshHitProducer_,esRecHitsHandle);
2071     }else{
2072     evt.getByLabel(preshHitProducerEta_,esRecHitsHandle);
2073     }
2074    
2075     ///const EcalRecHitCollection* hitCollection_es = esRecHitsHandle.product();
2076     EcalRecHitCollection::const_iterator iter;
2077     for (iter = esRecHitsHandle->begin(); iter != esRecHitsHandle->end(); iter++) {
2078     //Make the map of DetID, EcalRecHit pairs
2079     esrechits_map.insert(std::make_pair(iter->id(), *iter));
2080     }
2081    
2082     }catch(std::exception& ex ){
2083     storeRecHitES_ = false;
2084     cout<<"preshowerRecHit not working.."<<endl;
2085     }
2086    
2087     }
2088    
2089    
2090     vector<int> indClusPi0Candidates; ///those clusters identified as pi0s
2091     if( detector == reco::CaloID::DET_ECAL_BARREL && rmPiz){
2092     for(int i=0 ; i<nClus ; i++){
2093     for(int j=i+1 ; j<nClus ; j++){
2094    
2095     double p0x = etClus[i] * cos(phiClus[i]);
2096     double p1x = etClus[j] * cos(phiClus[j]);
2097     double p0y = etClus[i] * sin(phiClus[i]);
2098     double p1y = etClus[j] * sin(phiClus[j]);
2099     double p0z = eClus[i] * cos(thetaClus[i]);
2100     double p1z = eClus[j] * cos(thetaClus[j]);
2101    
2102     double m_inv = sqrt ( (eClus[i] + eClus[j])*(eClus[i] + eClus[j]) - (p0x+p1x)*(p0x+p1x) - (p0y+p1y)*(p0y+p1y) - (p0z+p1z)*(p0z+p1z) );
2103    
2104     if( m_inv > massLowPi0Cand_ && m_inv < massHighPi0Cand_){
2105    
2106     int indtmp[2] = {i,j};
2107     for(int k=0; k<2; k++){
2108     std::vector<int>::iterator it = find(indClusPi0Candidates.begin(),indClusPi0Candidates.end(),indtmp[k]);
2109     if( it == indClusPi0Candidates.end()) indClusPi0Candidates.push_back(indtmp[k]);
2110     }
2111    
2112     }
2113    
2114     }
2115    
2116     }
2117     }
2118    
2119    
2120     used_strips.clear();
2121     ///we need to make sure for every endcap cluster we have the same preshower cluster
2122     std::map<int, vector<float > > infoES_saved;
2123     vector<int> indClusSelected;
2124    
2125    
2126     ///after selection do NxN matching with MC
2127     vector<int> indPairCand; ///pair of cluster passed the cuts
2128     vector<float> mPairCand; /// mpair of of cluster passed the cuts
2129     vector<float> mPairv1Cand; /// mpair of of cluster passed the cuts
2130    
2131     vector<float> etaPairCand;
2132     vector<float> phiPairCand;
2133     vector<float> ptPairCand;
2134     vector<float> isoPairCand;
2135    
2136    
2137     for(int i=0 ; i<nClus ; i++){
2138    
2139     if( detector == reco::CaloID::DET_ECAL_BARREL && rmPiz){
2140     std::vector<int>::iterator it = find(indClusPi0Candidates.begin(),indClusPi0Candidates.end(),i);
2141     if( it != indClusPi0Candidates.end()) continue;
2142     }
2143    
2144     if ( s4s9Clus[i] < s4s9Cut || s9s25Clus[i] < s9s25Cut ) continue;
2145    
2146     for(int j=i+1 ; j<nClus ; j++){
2147    
2148     if( detector == reco::CaloID::DET_ECAL_BARREL && rmPiz){
2149     std::vector<int>::iterator it = find(indClusPi0Candidates.begin(),indClusPi0Candidates.end(),j);
2150     if( it != indClusPi0Candidates.end()) continue;
2151     }
2152    
2153     if ( s4s9Clus[j] < s4s9Cut || s9s25Clus[j] < s9s25Cut ) continue;
2154    
2155     double p0x = etClus[i] * cos(phiClus[i]);
2156     double p1x = etClus[j] * cos(phiClus[j]);
2157     double p0y = etClus[i] * sin(phiClus[i]);
2158     double p1y = etClus[j] * sin(phiClus[j]);
2159     double p0z = eClus[i] * cos(thetaClus[i]);
2160     double p1z = eClus[j] * cos(thetaClus[j]);
2161     ptpair = sqrt( (p0x+p1x)*(p0x+p1x) + (p0y+p1y)*(p0y+p1y));
2162     if (ptpair < ptpairCut )continue;
2163     TVector3 pairVect = TVector3((p0x+p1x), (p0y+p1y), (p0z+p1z));
2164     etapair = pairVect.Eta();
2165     phipair = pairVect.Phi();
2166    
2167     ptmin = etClus[i]< etClus[j] ? etClus[i] : etClus[j] ;
2168     if( detector == reco::CaloID::DET_ECAL_ENDCAP){
2169     if( fabs(etapair) < eta_endcap_region1){
2170     if(ptmin < ptminCut_endcap_region1 || ptpair < ptpairCut_endcap_region1) continue;
2171     }else if (fabs(etapair)< eta_endcap_region2){
2172     if(ptmin < ptminCut_endcap_region2 || ptpair < ptpairCut_endcap_region2) continue;
2173     }else{
2174     if(ptmin < ptminCut_endcap_region3 || ptpair < ptpairCut_endcap_region3) continue;
2175 yangyong 1.2 if( ptpair > ptpairMaxCut_endcap_region3 ) continue;
2176 yangyong 1.1 }
2177     }else{
2178    
2179     if(ptmin < ptminCut || ptpair < ptpairCut) continue;
2180    
2181     }
2182    
2183 yangyong 1.2
2184 yangyong 1.1 mpair = sqrt ( (eClus[i] + eClus[j])*(eClus[i] + eClus[j]) - (p0x+p1x)*(p0x+p1x) - (p0y+p1y)*(p0y+p1y) - (p0z+p1z)*(p0z+p1z) );
2185    
2186    
2187     if( mpair < minvMinCut || mpair > minvMaxCut ) continue;
2188    
2189     if(debug_>=2) cout<<"selpi0pair1: "<< ptpair <<" "<< etClus[i]<<" "<< etClus[j]<<" "<< s4s9Clus[i]<<" "<< s4s9Clus[j]<<" "<<mpair<<endl;
2190    
2191    
2192     ///Isolation
2193     isolation = 0;
2194     for(int k=0 ; k<nClus ; k++){
2195     if(k==i || k==j)continue;
2196     if( etClus[k] < isoptCut ) continue;
2197    
2198     TVector3 clusVect = TVector3(etClus[k] *cos(phiClus[k]), etClus[k] * sin(phiClus[k]) , eClus[k] * cos(thetaClus[k]));
2199     double dretacl = fabs(etaClus[k] - pairVect.Eta());
2200     double drcl = clusVect.DeltaR(pairVect);
2201    
2202     if( drcl < isodrCut && dretacl < isodetaCut ){
2203     isolation += etClus[k];
2204     }
2205     }
2206     isolation /= ptpair;
2207    
2208    
2209     if( isolation > isoCut ) continue;
2210    
2211    
2212     TVector3 vtmp0( xClus[i] - xOffPVwithBS, yClus[i] - yOffPVwithBS, zClus[i] - zOffPVwithBS);
2213     TVector3 vtmp1( xClus[j] - xOffPVwithBS, yClus[j] - yOffPVwithBS, zClus[j] - zOffPVwithBS);
2214     float cosd = getcosd( vtmp0.Eta(),vtmp0.Phi(),vtmp1.Eta(),vtmp1.Phi());
2215     mpairv1 = sqrt( 2 * eClus[i] * eClus[j] * (1- cosd));
2216    
2217     indPairCand.push_back(i * nClus + j);
2218     mPairCand.push_back(mpair);
2219     mPairv1Cand.push_back(mpairv1);
2220     ptPairCand.push_back(ptpair);
2221     etaPairCand.push_back(etapair);
2222     phiPairCand.push_back(phipair);
2223     isoPairCand.push_back(isolation);
2224    
2225     }
2226     }
2227    
2228    
2229     vector<int> pairRecoMatchedtoGenPiz;
2230     vector<int> pairRecoMatchedtoGen1stPhtInd; //the 1st photon int the pair matched to 0 /1 genpht
2231     vector<int> genpi0MatchedtoReco; //for pi0/eta both
2232    
2233     float drMatch = 0.05;
2234     if( detector == reco::CaloID::DET_ECAL_BARREL){
2235     drMatch = 0.03;
2236     }
2237    
2238     if( InputDataFormat_ <10){
2239     if(pizEta == PIZ){
2240     NtoNmatchingPi0(drMatch, indPairCand,pairRecoMatchedtoGenPiz,pairRecoMatchedtoGen1stPhtInd,genpi0MatchedtoReco);
2241     }else{
2242     NtoNmatchingEta(drMatch, indPairCand,pairRecoMatchedtoGenPiz,pairRecoMatchedtoGen1stPhtInd,genpi0MatchedtoReco);
2243     }
2244    
2245     }
2246    
2247    
2248     ///Now Fill what selected
2249     for(int k=0; k< int(indPairCand.size()); k++){
2250    
2251     mpair = mPairCand[k];
2252     mpairv1 = mPairv1Cand[k];
2253    
2254     int i = indPairCand[k] / nClus;
2255     int j = indPairCand[k] % nClus;
2256    
2257     ptpair = ptPairCand[k];
2258     etapair = etaPairCand[k];
2259     phipair = phiPairCand[k];
2260     isolation = isoPairCand[k];
2261     ptmin = etClus[i] < etClus[j]? etClus[i] : etClus[j];
2262    
2263     s4s9min = s4s9Clus[i] < s4s9Clus[j] ? s4s9Clus[i] : s4s9Clus[j];
2264     s9s25min = s9s25Clus[i] < s9s25Clus[j] ? s9s25Clus[i] : s9s25Clus[j];
2265    
2266    
2267    
2268    
2269     if( InputDataFormat_ <10){
2270     for(int kk =0; kk<6; kk++) geninfo[kk]= -99;
2271     genMatched = 0;
2272     int indgen = pairRecoMatchedtoGenPiz[k];
2273     if(indgen>=0){
2274     genMatched = 1;
2275     int indga1 = pairRecoMatchedtoGen1stPhtInd[k];
2276     if(pizEta == PIZ){
2277     geninfo[0] = ePhtGenpi0[indgen][indga1];
2278     geninfo[1] = etaPhtGenpi0[indgen][indga1];
2279     geninfo[2] = phiPhtGenpi0[indgen][indga1];
2280     geninfo[3] = ePhtGenpi0[indgen][1-indga1];
2281     geninfo[4] = etaPhtGenpi0[indgen][1-indga1];
2282     geninfo[5] = phiPhtGenpi0[indgen][1-indga1];
2283    
2284     convinfo[0] = isConvPhtGenpi0[indgen][indga1];
2285     convinfo[1] = isConvPhtGenpi0[indgen][1-indga1];
2286    
2287     }else{
2288     geninfo[0] = ePhtGeneta[indgen][indga1];
2289     geninfo[1] = etaPhtGeneta[indgen][indga1];
2290     geninfo[2] = phiPhtGeneta[indgen][indga1];
2291     geninfo[3] = ePhtGeneta[indgen][1-indga1];
2292     geninfo[4] = etaPhtGeneta[indgen][1-indga1];
2293     geninfo[5] = phiPhtGeneta[indgen][1-indga1];
2294    
2295     convinfo[0] = isConvPhtGeneta[indgen][indga1];
2296     convinfo[1] = isConvPhtGeneta[indgen][1-indga1];
2297    
2298     }
2299    
2300     }
2301     }
2302    
2303    
2304     int indtmp[2]={i,j};
2305    
2306    
2307     izXtalClus1 = 0;
2308     izXtalClus2 = 0;
2309    
2310    
2311     for(int jj =0; jj<2; jj++){
2312     int ind = indtmp[jj];
2313    
2314     if( jj==0) nxtClus1 = int(RecHitsCluster[ind].size());
2315     else nxtClus2 = int(RecHitsCluster[ind].size());
2316    
2317    
2318     for(unsigned int Rec=0;Rec<RecHitsCluster[ind].size();Rec++) {
2319     int ietathis;
2320     int iphithis;
2321    
2322     if( detector == reco::CaloID::DET_ECAL_BARREL){
2323     EBDetId det = (EBDetId)RecHitsCluster[ind][Rec].id();
2324     ietathis = det.ieta();
2325     iphithis = det.iphi();
2326     }else{
2327     EEDetId det = (EEDetId)RecHitsCluster[ind][Rec].id();
2328     ietathis = det.ix();
2329     iphithis = det.iy();
2330     if(jj==0) izXtalClus1 = det.zside();
2331     else izXtalClus2 = det.zside();
2332     }
2333     if( jj==0){
2334     eXtalClus1[Rec] = RecHitsCluster[ind][Rec].energy();
2335     ietaXtalClus1[Rec] = ietathis;
2336     iphiXtalClus1[Rec] = iphithis;
2337    
2338     fXtalClus1[Rec] = RecHitsCluster[ind][Rec].recoFlag();
2339     tXtalClus1[Rec] = RecHitsCluster[ind][Rec].time();
2340     }else{
2341     ietaXtalClus2[Rec] = ietathis;
2342     iphiXtalClus2[Rec] = iphithis;
2343     fXtalClus2[Rec] = RecHitsCluster[ind][Rec].recoFlag();
2344     eXtalClus2[Rec] = RecHitsCluster[ind][Rec].energy();
2345     tXtalClus2[Rec] = RecHitsCluster[ind][Rec].time();
2346     }
2347     }
2348     }
2349    
2350     if(pizEta==2){ //save also additional 5x5 around the 3x3 alredy saved
2351    
2352     for(int jj =0; jj<2; jj++){
2353     int ind = indtmp[jj];
2354    
2355     if( jj==0) nxt5x5Clus1 = int(RecHitsCluster5x5[ind].size());
2356     else nxt5x5Clus2 = int(RecHitsCluster5x5[ind].size());
2357    
2358     for(unsigned int Rec=0;Rec<RecHitsCluster5x5[ind].size();Rec++) {
2359     int ietathis;
2360     int iphithis;
2361     if( detector == reco::CaloID::DET_ECAL_BARREL){
2362     EBDetId det = (EBDetId)RecHitsCluster5x5[ind][Rec].id();
2363     ietathis = det.ieta();
2364     iphithis = det.iphi();
2365     }else{
2366     EEDetId det = (EEDetId)RecHitsCluster5x5[ind][Rec].id();
2367     ietathis = det.ix();
2368     iphithis = det.iy();
2369     }
2370    
2371     if( jj==0){
2372     eXtal5x5Clus1[Rec] = RecHitsCluster5x5[ind][Rec].energy();
2373     ietaXtal5x5Clus1[Rec] = ietathis;
2374     iphiXtal5x5Clus1[Rec] = iphithis;
2375     fXtal5x5Clus1[Rec] = RecHitsCluster5x5[ind][Rec].recoFlag();
2376     tXtal5x5Clus1[Rec] = RecHitsCluster5x5[ind][Rec].time();
2377     }else{
2378     ietaXtal5x5Clus2[Rec] = ietathis;
2379     iphiXtal5x5Clus2[Rec] = iphithis;
2380     fXtal5x5Clus2[Rec] = RecHitsCluster5x5[ind][Rec].recoFlag();
2381     eXtal5x5Clus2[Rec] = RecHitsCluster5x5[ind][Rec].energy();
2382     tXtal5x5Clus2[Rec] = RecHitsCluster5x5[ind][Rec].time();
2383     }
2384     }
2385     }
2386     }
2387    
2388     ///Now finally fill the trees
2389    
2390     if( detector == reco::CaloID::DET_ECAL_BARREL){
2391     if(pizEta ==1) mytree_pizeb->Fill();
2392     else mytree_etaeb->Fill();
2393     }else{
2394    
2395     //adding preshower information as well
2396     if( storeRecHitES_){
2397    
2398    
2399     for(int jj =0; jj<2; jj++){
2400     for(int n=0; n<8; n++) {
2401     infoESX[jj][n] = 0;
2402     infoESY[jj][n] = 0;
2403     }
2404     int ind = indtmp[jj];
2405     std::vector<int>::iterator it = find(indClusSelected.begin(),indClusSelected.end(),ind);
2406     if( it == indClusSelected.end()){
2407     indClusSelected.push_back(ind);
2408    
2409     double e1=0;
2410     double e2=0;
2411     double deltaE=0;
2412     double deltaE1=0;
2413     double deltaE2=0;
2414    
2415     const GlobalPoint point(xClus[ind],yClus[ind],zClus[ind]);
2416    
2417    
2418     DetId tmp1 = (dynamic_cast<const EcalPreshowerGeometry*>(geometry_es))->getClosestCellInPlane(point, 1);
2419     DetId tmp2 = (dynamic_cast<const EcalPreshowerGeometry*>(geometry_es))->getClosestCellInPlane(point, 2);
2420     ESDetId strip1 = (tmp1 == DetId(0)) ? ESDetId(0) : ESDetId(tmp1);
2421     ESDetId strip2 = (tmp2 == DetId(0)) ? ESDetId(0) : ESDetId(tmp2);
2422    
2423     std::set<DetId> used_strips_before = used_strips;
2424     // Get ES clusters (found by the PreshSeeded algorithm) associated with a given EE cluster.
2425     /// Energy weight cluster 1
2426     double wtposEs[2][3]={{0}};
2427     double mposEs[2][3]={{0}}; //energy highest
2428     double max_es[2]={0};
2429    
2430     for (int i2=0; i2<preshNclust_; i2++) {
2431     reco::PreshowerCluster cl1 = presh_algo->makeOneCluster(strip1,&used_strips,&esrechits_map,geometry_es,topology_es);
2432     if ( cl1.energy() > preshClustECut) {
2433     e1 += cl1.energy();
2434    
2435     wtposEs[0][0] += cl1.energy() * cl1.position().X();
2436     wtposEs[0][1] += cl1.energy() * cl1.position().Y();
2437     wtposEs[0][2] += cl1.energy() * cl1.position().Z();
2438    
2439     if( cl1.energy() > max_es[0]){
2440     max_es[0] = cl1.energy() ;
2441     mposEs[0][0] = cl1.position().X();
2442     mposEs[0][1] = cl1.position().Y();
2443     mposEs[0][2] = cl1.position().Z();
2444     }
2445    
2446     }
2447     reco::PreshowerCluster cl2 = presh_algo->makeOneCluster(strip2,&used_strips,&esrechits_map,geometry_es,topology_es);
2448     if ( cl2.energy() > preshClustECut) {
2449     e2 += cl2.energy();
2450     wtposEs[1][0] += cl2.energy() * cl2.position().X();
2451     wtposEs[1][1] += cl2.energy() * cl2.position().Y();
2452     wtposEs[1][2] += cl2.energy() * cl2.position().Z();
2453     if( cl2.energy() > max_es[1]){
2454     max_es[1] = cl2.energy();
2455     mposEs[1][0] = cl2.position().X();
2456     mposEs[1][1] = cl2.position().Y();
2457     mposEs[1][2] = cl2.position().Z();
2458     }
2459     }
2460     } // end of cycle over ES clusters
2461    
2462     if(e1+e2 > 1.0E-10) {
2463    
2464     if( e1 > 0){
2465     for(int n=0; n<3; n++){
2466     wtposEs[0][n] /= e1;
2467     }
2468     }
2469     if( e2 > 0){
2470     for(int n=0; n<3; n++){
2471     wtposEs[1][n] /= e2;
2472     }
2473     }
2474     // GeV to #MIPs
2475     e1 = e1 / mip_;
2476     e2 = e2 / mip_;
2477     deltaE = gamma_*(calib_planeX_*e1+calib_planeY_*e2);
2478     deltaE1 = gamma_*(calib_planeX_*e1);
2479     deltaE2 = gamma_*(calib_planeY_*e2);
2480    
2481     max_es[0] *= gamma_*(calib_planeX_)/ mip_;
2482     max_es[1] *= gamma_*(calib_planeY_)/ mip_;
2483    
2484     if(debug_>=2){
2485     cout<<"e1e1max: "<< deltaE1 <<" "<< max_es[0]<<" "<< deltaE2 <<" "<<max_es[1]<<endl;
2486     }
2487    
2488     ///x plane
2489     infoESX[jj][0] = deltaE1;
2490     for(int n=0; n<3; n++){
2491     infoESX[jj][n+1] = wtposEs[0][n];
2492     }
2493     infoESX[jj][4] = max_es[0];
2494     for(int n=0; n<3; n++){
2495     infoESX[jj][n+5] = mposEs[0][n];
2496     }
2497     //y plane
2498     infoESY[jj][0] = deltaE2;
2499     for(int n=0; n<3; n++){
2500     infoESY[jj][n+1] = wtposEs[1][n];
2501     }
2502     infoESY[jj][4] = max_es[1];
2503     for(int n=0; n<3; n++){
2504     infoESY[jj][n+5] = mposEs[1][n];
2505     }
2506    
2507     }
2508     //now push_back to the map of ES information
2509     vector<float> infoES_v;
2510     for(int n=0; n<8; n++) {
2511     infoES_v.push_back(infoESX[jj][n]) ;
2512     }
2513     for(int n=0; n<8; n++) {
2514     infoES_v.push_back(infoESY[jj][n]) ;
2515     }
2516     infoES_saved.insert( make_pair(ind, infoES_v) );
2517    
2518    
2519     }else{
2520     std::map<int, vector<float> >::iterator itmap = infoES_saved.find(ind);
2521     ///double es = (itmap->second).energy();
2522     if( itmap != infoES_saved.end()){
2523     for(int n=0;n<8; n++) infoESX[jj][n] = (itmap->second)[n];
2524     for(int n=0;n<8; n++) infoESY[jj][n] = (itmap->second)[n+8];
2525     }
2526     }
2527    
2528     }
2529    
2530     }
2531    
2532     if(pizEta ==1) mytree_pizee->Fill();
2533     else mytree_etaee->Fill();
2534     } /// endcap
2535    
2536    
2537    
2538     } ///end of filling tree
2539    
2540    
2541    
2542    
2543    
2544    
2545     delete topology_es;
2546    
2547    
2548    
2549     }
2550    
2551     void RecoAnalyzer::NtoNmatchingPi0(float drMatch, vector<int> pairReco ,vector<int> &pairRecoMatchedtoGen, vector<int> &pairRecoMatchedtoGen1stPhtInd,vector<int> &genpi0MatchedtoReco){
2552    
2553     float zpht;
2554     float rpht;
2555     float xpht;
2556     float ypht;
2557    
2558     //a list of pair reco ( direction w.r.t (0,0,0))
2559    
2560     ///matched to a list of gen
2561    
2562    
2563     ////based on the smallest dr of sum of dr_photon1+ dr_photon2
2564     vector< std::pair<double,int> > dr_jk;
2565     int n =0;
2566    
2567     ////try to use x,y,Z of cluster w.r.t to gen-Level pi0/eta
2568     ///bool useGenVtxforRecoGenMatch = true;
2569     int indga1 = 0;
2570    
2571     vector< std::pair<int,int> > indga1_jk;
2572    
2573    
2574     for(int j=0; j< int(pairReco.size()); j++){
2575    
2576     int j1 = pairReco[j]/nClus;
2577     int j2 = pairReco[j]%nClus;
2578    
2579     int jtmp[2] = {j1,j2};
2580    
2581     ////eta ,phi from (0,0,0);
2582     float eta_2pht[2] ={etaClus[j1],etaClus[j2]};
2583     float phi_2pht[2] ={phiClus[j1],phiClus[j2]};
2584     for(int tt=0;tt<2; tt++){
2585     TVector3 vv(xClus[jtmp[tt]],yClus[jtmp[tt]],zClus[jtmp[tt]]);
2586     eta_2pht[tt] = vv.Eta();
2587     phi_2pht[tt] = vv.Phi();
2588     }
2589    
2590    
2591     for(int k=0; k< nGenpi0; k++){
2592    
2593     float dr_twopht[2]={1,1};
2594     float drsum = 0;
2595     float allDR[4];
2596    
2597     int kk = 0;
2598     for( int k1 =0; k1<2; k1++){
2599     float eta = eta_2pht[k1];
2600     float phi = phi_2pht[k1];
2601    
2602     for( int k2 = 0; k2<2; k2++){
2603    
2604     zpht = vtxPhtGenpi0[k][2];
2605     rpht = sqrt( vtxPhtGenpi0[k][0] * vtxPhtGenpi0[k][0] + vtxPhtGenpi0[k][1] * vtxPhtGenpi0[k][1]);
2606     xpht = vtxPhtGenpi0[k][0];
2607     ypht = vtxPhtGenpi0[k][1];
2608    
2609     float etag = etaPhtGenpi0[k][k2];
2610     float phig = phiPhtGenpi0[k][k2];
2611    
2612     etag = ecalEta(etaPhtGenpi0[k][k2],zpht,rpht);
2613     phig = ecalPhi(phiPhtGenpi0[k][k2],xpht,ypht);
2614    
2615    
2616    
2617     allDR[kk] = GetDeltaR(eta,etag,phi,phig);
2618     kk++;
2619    
2620     }
2621     }
2622    
2623     float drsum1 = allDR[0] + allDR[3];
2624     float drsum2 = allDR[1] + allDR[2];
2625     drsum = drsum1 < drsum2? drsum1: drsum2;
2626    
2627     if(drsum1<drsum2){
2628     dr_twopht[0] = allDR[0];
2629     dr_twopht[1] = allDR[3];
2630     indga1 = 0;
2631     }else{
2632     dr_twopht[0] = allDR[1];
2633     dr_twopht[1] = allDR[2];
2634     indga1 = 1;
2635     }
2636    
2637     if( dr_twopht[0]< drMatch && dr_twopht[1] < drMatch){
2638     dr_jk.push_back( make_pair ( drsum, n));
2639    
2640     indga1_jk.push_back( make_pair ( indga1, n));
2641     }
2642    
2643     n++;
2644    
2645     }
2646     }
2647    
2648    
2649     sort(dr_jk.begin(),dr_jk.end(),sort_pred);
2650    
2651     vector<int> nn_matched;
2652     vector<int> j_used;
2653     vector<int> k_used;
2654     vector<int>::iterator it;
2655     vector<int>::iterator it2;
2656    
2657     int nn = int( dr_jk.size());
2658    
2659     for(int n=0; n< nn; n++){
2660    
2661     int nn0 = dr_jk[n].second;
2662     int j = nn0 / nGenpi0;
2663     int k = nn0 % nGenpi0;
2664    
2665     it = find(j_used.begin(),j_used.end(),j);
2666     it2 = find(k_used.begin(),k_used.end(),k);
2667    
2668     if( it == j_used.end() && it2 == k_used.end()){
2669     j_used.push_back(j);
2670     k_used.push_back(k);
2671     nn_matched.push_back(nn0); /// this matters a lot if to use more than once when matching...
2672     }
2673     }
2674    
2675    
2676     ///nn_matched are the final matched pair reco <==> gen pi0
2677     for(int j=0; j< int(pairReco.size()); j++){
2678     int matched = -1; //if matched, be the index in the array of genpi0
2679     int nn0 = -1;
2680     for(int n=0; n< int( nn_matched.size()); n++){
2681     int j1 = nn_matched[n]/ nGenpi0;
2682     int k = nn_matched[n] % nGenpi0;
2683     if( j1 == j){
2684     matched = k;
2685     nn0 = nn_matched[n];
2686     break;
2687     }
2688     }
2689    
2690     ////cout<<"pairREco Mathced: "<< j<<" "<< matched <<endl;
2691    
2692     pairRecoMatchedtoGen.push_back(matched);
2693     indga1 = 0;
2694     for(int n =0; n< int( indga1_jk.size()); n++){
2695     if( indga1_jk[n].second == nn0){
2696     indga1 = indga1_jk[n].first;
2697     n = int( indga1_jk.size()+1);
2698     }
2699     }
2700     pairRecoMatchedtoGen1stPhtInd.push_back(indga1);
2701    
2702     }
2703    
2704    
2705    
2706     }
2707    
2708     void RecoAnalyzer::NtoNmatchingEta(float drMatch, vector<int> pairReco ,vector<int> &pairRecoMatchedtoGen, vector<int> &pairRecoMatchedtoGen1stPhtInd, vector<int> &genetaMatchedtoReco){
2709    
2710    
2711     float zpht;
2712     float rpht;
2713     float xpht;
2714     float ypht;
2715    
2716     //a list of pair reco ( direction w.r.t (0,0,0))
2717    
2718     ///matched to a list of gen
2719     int indga1 = 0;
2720     vector< std::pair<int,int> > indga1_jk;
2721    
2722    
2723    
2724     ////based on the smallest dr of sum of dr_photon1+ dr_photon2
2725     vector< std::pair<double,int> > dr_jk;
2726     int n =0;
2727    
2728     ////try to use x,y,Z of cluster w.r.t to gen-Level eta/eta
2729     ///bool useGenVtxforRecoGenMatch = true;
2730    
2731    
2732     for(int j=0; j< int(pairReco.size()); j++){
2733    
2734     int j1 = pairReco[j]/nClus ;
2735     int j2 = pairReco[j]%nClus;
2736    
2737     int jtmp[2] = {j1,j2};
2738    
2739     ////eta ,phi from (0,0,0);
2740     float eta_2pht[2] ={etaClus[j1],etaClus[j2]};
2741     float phi_2pht[2] ={phiClus[j1],phiClus[j2]};
2742     for(int tt=0;tt<2; tt++){
2743     TVector3 vv(xClus[jtmp[tt]],yClus[jtmp[tt]],zClus[jtmp[tt]]);
2744     eta_2pht[tt] = vv.Eta();
2745     phi_2pht[tt] = vv.Phi();
2746     }
2747    
2748    
2749     for(int k=0; k< nGeneta; k++){
2750    
2751     float dr_twopht[2]={1,1};
2752     float drsum = 0;
2753     float allDR[4];
2754    
2755     int kk = 0;
2756     for( int k1 =0; k1<2; k1++){
2757     float eta = eta_2pht[k1];
2758     float phi = phi_2pht[k1];
2759    
2760     for( int k2 = 0; k2<2; k2++){
2761    
2762     zpht = vtxPhtGeneta[k][2];
2763     rpht = sqrt( vtxPhtGeneta[k][0] * vtxPhtGeneta[k][0] + vtxPhtGeneta[k][1] * vtxPhtGeneta[k][1]);
2764     xpht = vtxPhtGeneta[k][0];
2765     ypht = vtxPhtGeneta[k][1];
2766    
2767     float etag = etaPhtGeneta[k][k2];
2768     float phig = phiPhtGeneta[k][k2];
2769     etag = ecalEta(etaPhtGeneta[k][k2],zpht,rpht);
2770     phig = ecalPhi(phiPhtGeneta[k][k2],xpht,ypht);
2771    
2772     allDR[kk] = GetDeltaR(eta,etag,phi,phig);
2773     kk++;
2774    
2775     }
2776     }
2777    
2778     float drsum1 = allDR[0] + allDR[3];
2779     float drsum2 = allDR[1] + allDR[2];
2780     drsum = drsum1 < drsum2? drsum1: drsum2;
2781    
2782     if(drsum1<drsum2){
2783     dr_twopht[0] = allDR[0];
2784     dr_twopht[1] = allDR[3];
2785     }else{
2786     dr_twopht[0] = allDR[1];
2787     dr_twopht[1] = allDR[2];
2788     }
2789    
2790     if( dr_twopht[0]< drMatch && dr_twopht[1] <drMatch ){
2791     dr_jk.push_back( make_pair ( drsum, n));
2792     indga1_jk.push_back( make_pair ( indga1, n));
2793    
2794    
2795     }
2796    
2797     n++;
2798    
2799     }
2800     }
2801    
2802    
2803     sort(dr_jk.begin(),dr_jk.end(),sort_pred);
2804    
2805     vector<int> nn_matched;
2806     vector<int> j_used;
2807     vector<int> k_used;
2808     vector<int>::iterator it;
2809     vector<int>::iterator it2;
2810    
2811     int nn = int( dr_jk.size());
2812    
2813     for(int n=0; n< nn; n++){
2814    
2815     int nn0 = dr_jk[n].second;
2816     int j = nn0 / nGeneta;
2817     int k = nn0 % nGeneta;
2818    
2819     it = find(j_used.begin(),j_used.end(),j);
2820     it2 = find(k_used.begin(),k_used.end(),k);
2821    
2822     if( it == j_used.end() && it2 == k_used.end()){
2823     j_used.push_back(j);
2824     k_used.push_back(k);
2825     nn_matched.push_back(nn0); /// this matters a lot if to use more than once when matching...
2826     }
2827    
2828     }
2829    
2830    
2831     ///nn_matched are the final matched pair reco <==> gen eta
2832     for(int j=0; j< int(pairReco.size()); j++){
2833     int matched = -1; //if matched, be the index in the array of geneta
2834    
2835     int nn0 = -1;
2836    
2837     for(int n=0; n< int( nn_matched.size()); n++){
2838     int j1 = nn_matched[n]/ nGeneta;
2839     int k = nn_matched[n] % nGeneta;
2840     if( j1 == j){
2841     matched = k;
2842    
2843     nn0 = nn_matched[n];
2844    
2845     break;
2846     }
2847     }
2848    
2849     ////cout<<"pairREco Mathced: "<< j<<" "<< matched <<endl;
2850    
2851     pairRecoMatchedtoGen.push_back(matched);
2852     indga1 = 0;
2853     for(int n =0; n< int( indga1_jk.size()); n++){
2854     if( indga1_jk[n].second == nn0){
2855     indga1 = indga1_jk[n].first;
2856     n = int( indga1_jk.size()+1); ///break;
2857     }
2858     }
2859     pairRecoMatchedtoGen1stPhtInd.push_back(indga1);
2860    
2861    
2862     }
2863    
2864     }
2865    
2866    
2867    
2868     //define this as a plug-in
2869     DEFINE_FWK_MODULE(RecoAnalyzer);