CMS 3D CMS Logo

FileRandomKEThetaGunProducer.cc
Go to the documentation of this file.
1 #include <iostream>
2 
4 
7 
14 
15 #include "CLHEP/Random/RandFlat.h"
16 #include "CLHEP/Random/RandomEngine.h"
17 
18 using namespace edm;
19 
22 
23  edm::ParameterSet defpset ;
24  edm::ParameterSet pgun_params =
25  pset.getParameter<edm::ParameterSet>("PGunParameters") ;
26 
27  edm::FileInPath fp = pgun_params.getParameter<edm::FileInPath>("File");
28  std::string file = fp.fullPath();
29  particleN = pgun_params.getParameter<int>("Particles");
30  if (particleN <= 0) particleN = 1;
31  edm::LogInfo("FlatThetaGun") << "Internal FileRandomKEThetaGun is initialzed"
32  << " with data read from " << file << " and "
33  << particleN << " particles created/event";
34  std::ifstream is(file.c_str(), std::ios::in);
35  if (is) {
36  double energy, elem, sum=0;
37  while (!is.eof()) {
38  is >> energy >> elem;
39  kineticE.push_back(0.001*energy);
40  fdistn.push_back(elem);
41  sum += elem;
42  if (fVerbosity > 0) LogDebug("FlatThetaGun") << "KE " << energy
43  <<" GeV Count rate " <<elem;
44  }
45  is.close();
46  double last = 0;
47  for (unsigned int i=0; i<fdistn.size(); i++) {
48  fdistn[i] /= sum;
49  fdistn[i] += last;
50  last = fdistn[i];
51  if (fVerbosity > 0) LogDebug("FlatThetaGun") << "Bin [" << i << "]: KE "
52  << kineticE[i] << " Distn "
53  << fdistn[i];
54  }
55  }
56  if (kineticE.size() < 2)
57  throw cms::Exception("FileNotFound","Not enough data point found in file ")
58  << file << "\n";
59 
60 
61  produces<HepMCProduct>("unsmeared");
62  produces<GenEventInfoProduct>();
63 }
64 
66 
68 
69  if (fVerbosity > 0)
70  LogDebug("FlatThetaGun") << "FileRandomKEThetaGunProducer : Begin New Event Generation";
71 
73  CLHEP::HepRandomEngine* engine = &rng->getEngine(e.streamID());
74 
75  // event loop (well, another step in it...)
76 
77  // no need to clean up GenEvent memory - done in HepMCProduct
78 
79  // here re-create fEvt (memory)
80  //
81  fEvt = new HepMC::GenEvent() ;
82 
83  // now actualy, cook up the event from PDGTable and gun parameters
84  //
85 
86  // 1st, primary vertex
87  //
88  HepMC::GenVertex* Vtx = new HepMC::GenVertex( HepMC::FourVector(0.,0.,0.));
89 
90  // loop over particles
91  //
92  int barcode = 1;
93  for (int ip=0; ip<particleN; ip++) {
94  double keMin=kineticE[0], keMax=kineticE[1];
95  double rMin=fdistn[0], rMax=fdistn[1];
96  double r1 = engine->flat();
97  for (unsigned int ii=kineticE.size()-2; ii>0; --ii) {
98  if (r1 > fdistn[ii]) {
99  keMin = kineticE[ii]; keMax = kineticE[ii+1];
100  rMin = fdistn[ii]; rMax = fdistn[ii+1]; break;
101  }
102  }
103  double ke = (keMin*(rMax-r1) + keMax*(r1-rMin))/(rMax-rMin);
104  if (fVerbosity > 1)
105  LogDebug("FlatThetaGun") << "FileRandomKEThetaGunProducer: KE " << ke
106  << " in range " << keMin << ":" << keMax
107  << " with " << r1 <<" in " << rMin <<":" <<rMax;
108  double theta = CLHEP::RandFlat::shoot(engine, fMinTheta, fMaxTheta);
109  double phi = CLHEP::RandFlat::shoot(engine, fMinPhi, fMaxPhi);
110  int PartID = fPartIDs[0];
111  const HepPDT::ParticleData*
112  PData = fPDGTable->particle(HepPDT::ParticleID(abs(PartID))) ;
113  double mass = PData->mass().value() ;
114  double energy = ke + mass;
115  double mom2 = ke*ke + 2.*ke*mass ;
116  double mom = std::sqrt(mom2);
117  double px = mom*sin(theta)*cos(phi);
118  double py = mom*sin(theta)*sin(phi);
119  double pz = mom*cos(theta);
120 
121  HepMC::FourVector p(px,py,pz,energy) ;
122  HepMC::GenParticle* Part = new HepMC::GenParticle(p,PartID,1);
123  Part->suggest_barcode( barcode ) ;
124  barcode++ ;
125  Vtx->add_particle_out(Part);
126 
127  }
128  fEvt->add_vertex(Vtx) ;
129  fEvt->set_event_number(e.id().event()) ;
130  fEvt->set_signal_process_id(20) ;
131 
132 
133  if (fVerbosity > 0) {
134  fEvt->print() ;
135  }
136 
137  std::unique_ptr<HepMCProduct> BProduct(new HepMCProduct()) ;
138  BProduct->addHepMCData( fEvt );
139  e.put(std::move(BProduct), "unsmeared");
140 
141  std::unique_ptr<GenEventInfoProduct> genEventInfo(new GenEventInfoProduct(fEvt));
142  e.put(std::move(genEventInfo));
143 
144  if ( fVerbosity > 0 )
145  LogDebug("FlatThetaGun") << "FileRandomKEThetaGunProducer : Event Generation Done";
146 }
#define LogDebug(id)
T getParameter(std::string const &) const
EventNumber_t event() const
Definition: EventID.h:41
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
ESHandle< HepPDT::ParticleDataTable > fPDGTable
Geom::Theta< T > theta() const
void produce(Event &e, const EventSetup &es) override
virtual CLHEP::HepRandomEngine & getEngine(StreamID const &)=0
Use this engine in event methods.
T sqrt(T t)
Definition: SSEVec.h:18
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
HepPDT::ParticleData ParticleData
def elem(elemtype, innerHTML='', html_class='', kwargs)
Definition: HTMLExport.py:19
ii
Definition: cuy.py:590
int ke
edm::EventID id() const
Definition: EventBase.h:59
HLT enums.
StreamID streamID() const
Definition: Event.h:95
def move(src, dest)
Definition: eostools.py:511