forked from cms-sw/cmssw
/
EcalTBMCInfoProducer.cc
162 lines (126 loc) · 5.43 KB
/
EcalTBMCInfoProducer.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* \file EcalTBMCInfoProducer.cc
*
*
*/
#include "SimG4CMS/EcalTestBeam/interface/EcalTBMCInfoProducer.h"
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "FWCore/Utilities/interface/RandomNumberGenerator.h"
#include "CLHEP/Random/RandFlat.h"
#include "FWCore/Utilities/interface/Exception.h"
#include "DataFormats/Math/interface/Point3D.h"
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
using namespace cms;
EcalTBMCInfoProducer::EcalTBMCInfoProducer(const edm::ParameterSet& ps) {
produces<PEcalTBInfo>();
edm::FileInPath CrystalMapFile = ps.getParameter<edm::FileInPath>("CrystalMapFile");
GenVtxToken = consumes<edm::HepMCProduct>(edm::InputTag("moduleLabelVtx","source"));
double fMinEta = ps.getParameter<double>("MinEta");
double fMaxEta = ps.getParameter<double>("MaxEta");
double fMinPhi = ps.getParameter<double>("MinPhi");
double fMaxPhi = ps.getParameter<double>("MaxPhi");
beamEta = (fMaxEta+fMinEta)/2.;
beamPhi = (fMaxPhi+fMinPhi)/2.;
beamTheta = 2.0*atan(exp(-beamEta));
beamXoff = ps.getParameter<double>("BeamMeanX");
beamYoff = ps.getParameter<double>("BeamMeanX");
string fullMapName = CrystalMapFile.fullPath();
theTestMap = new EcalTBCrystalMap(fullMapName);
crysNumber = 0;
double deltaEta = 999.;
double deltaPhi = 999.;
for ( int cryIndex = 1; cryIndex <= EcalTBCrystalMap::NCRYSTAL; ++cryIndex) {
double eta = 0;
double phi = 0.;
theTestMap->findCrystalAngles(cryIndex, eta, phi);
if ( fabs(beamEta - eta) < deltaEta && fabs(beamPhi - phi) < deltaPhi ) {
deltaEta = fabs(beamEta - eta);
deltaPhi = fabs(beamPhi - phi);
crysNumber = cryIndex;
}
else if (fabs(beamEta - eta)<deltaEta && fabs(beamPhi - phi)>deltaPhi ) {
if ( fabs(beamPhi - phi) < 0.017 ) {
deltaEta = fabs(beamEta - eta);
deltaPhi = fabs(beamPhi - phi);
crysNumber = cryIndex;
}
}
else if (fabs(beamEta - eta)>deltaEta && fabs(beamPhi - phi)<deltaPhi ) {
if ( fabs(beamEta - eta) < 0.017 ) {
deltaEta = fabs(beamEta - eta);
deltaPhi = fabs(beamPhi - phi);
crysNumber = cryIndex;
}
}
}
edm::LogInfo("EcalTBInfo") << "Initialize TB MC ECAL info producer with parameters: \n"
<< "Crystal map file: " << CrystalMapFile << "\n"
<< "Beam average eta = " << beamEta << "\n"
<< "Beam average phi = " << beamPhi << "\n"
<< "Corresponding to crystal number = " << crysNumber << "\n"
<< "Beam X offset = " << beamXoff << "\n"
<< "Beam Y offset = " << beamYoff;
// rotation matrix to move from the CMS reference frame to the test beam one
double xx = -cos(beamTheta)*cos(beamPhi);
double xy = -cos(beamTheta)*sin(beamPhi);
double xz = sin(beamTheta);
double yx = sin(beamPhi);
double yy = -cos(beamPhi);
double yz = 0.;
double zx = sin(beamTheta)*cos(beamPhi);
double zy = sin(beamTheta)*sin(beamPhi);
double zz = cos(beamTheta);
fromCMStoTB = new ROOT::Math::Rotation3D(xx, xy, xz, yx, yy, yz, zx, zy, zz);
// random number
edm::Service<edm::RandomNumberGenerator> rng;
if ( ! rng.isAvailable()) {
throw cms::Exception("Configuration")
<< "EcalTBMCInfoProducer requires the RandomNumberGeneratorService\n"
"which is not present in the configuration file. You must add the service\n"
"in the configuration file or remove the modules that require it.";
}
}
EcalTBMCInfoProducer::~EcalTBMCInfoProducer() {
delete theTestMap;
}
void EcalTBMCInfoProducer::produce(edm::Event & event, const edm::EventSetup& eventSetup)
{
edm::Service<edm::RandomNumberGenerator> rng;
CLHEP::HepRandomEngine* engine = &rng->getEngine(event.streamID());
auto_ptr<PEcalTBInfo> product(new PEcalTBInfo());
// Fill the run information
product->setCrystal(crysNumber);
product->setBeamDirection(beamEta, beamPhi);
product->setBeamOffset(beamXoff, beamYoff);
// Compute the event x,y vertex coordinates in the beam reference system
// e.g. in the place orthogonal to the beam average direction
partXhodo = partYhodo = 0.;
edm::Handle<edm::HepMCProduct> GenEvt;
event.getByToken(GenVtxToken,GenEvt);
const HepMC::GenEvent* Evt = GenEvt->GetEvent() ;
HepMC::GenEvent::vertex_const_iterator Vtx = Evt->vertices_begin();
math::XYZPoint eventCMSVertex((*Vtx)->position().x(),
(*Vtx)->position().y(),
(*Vtx)->position().z());
LogDebug("EcalTBInfo") << "Generated vertex position = "
<< eventCMSVertex.x() << " "
<< eventCMSVertex.y() << " "
<< eventCMSVertex.z();
math::XYZPoint eventTBVertex = (*fromCMStoTB)*eventCMSVertex;
LogDebug("EcalTBInfo") << "Rotated vertex position = "
<< eventTBVertex.x() << " "
<< eventTBVertex.y() << " "
<< eventTBVertex.z();
partXhodo = eventTBVertex.x();
partYhodo = eventTBVertex.y();
product->setBeamPosition(partXhodo, partYhodo);
// Asynchronous phase shift
double thisPhaseShift = CLHEP::RandFlat::shoot(engine);
product->setPhaseShift(thisPhaseShift);
LogDebug("EcalTBInfo") << "Asynchronous Phaseshift = " << thisPhaseShift;
// store the object in the framework event
event.put(product);
}