CMS 3D CMS Logo

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