CMS 3D CMS Logo

RandomtXiGunProducer.cc
Go to the documentation of this file.
1 /*
2  * $Date: 2011/12/19 23:10:40 $
3  * $Revision: 1.4 $
4  * \author Luiz Mundim
5  */
6 
7 #include <ostream>
8 
10 
13 
14 
20 
21 #include <CLHEP/Random/RandFlat.h>
22 
23 using namespace edm;
24 using namespace std;
25 
28 {
29 
30  ParameterSet defpset ;
31  edm::ParameterSet pgun_params =
32  pset.getParameter<edm::ParameterSet>("PGunParameters") ;
33 
34  fMint = pgun_params.getParameter<double>("Mint");
35  fMaxt = pgun_params.getParameter<double>("Maxt");
36  fMinXi= pgun_params.getParameter<double>("MinXi");
37  fMaxXi= pgun_params.getParameter<double>("MaxXi");
38  fLog_t= pgun_params.getUntrackedParameter<bool>("Log_t",false);
39 
40  produces<HepMCProduct>("unsmeared");
41  produces<GenEventInfoProduct>();
42 }
43 
45 {
46  // no need to cleanup GenEvent memory - done in HepMCProduct
47 }
48 
50 {
52  CLHEP::HepRandomEngine* engine = &rng->getEngine(e.streamID());
53 
54  if ( fVerbosity > 0 )
55  {
56  edm::LogInfo("RandomtXiGunProducer")<< "Begin New Event Generation\n";
57  }
58  // event loop (well, another step in it...)
59 
60  // no need to clean up GenEvent memory - done in HepMCProduct
61  //
62 
63  // here re-create fEvt (memory)
64  //
65  fEvt = new HepMC::GenEvent() ;
66 
67  // now actualy, cook up the event from PDGTable and gun parameters
68  //
69  // 1st, primary vertex
70  //
71  HepMC::GenVertex* Vtx = new HepMC::GenVertex(HepMC::FourVector(0.,0.,0.));
72 
73  // loop over particles
74  //
75  int barcode = 1 ;
76  for (unsigned int ip=0; ip<fPartIDs.size(); ++ip)
77  {
78  int PartID = fPartIDs[ip];
79 // t = -2*P*P'*(1-cos(theta)) -> t/(2*P*P')+1=cos(theta)
80 // xi = 1 - P'/P --> P'= (1-xi)*P
81 //
82  PData = fPDGTable->particle(HepPDT::ParticleID(abs(PartID)));
83  if (!PData) exit(1);
84  double t = 0;
85  double Xi = 0;
86  double phi=0;
87  if (fFireForward) {
88  while(true) {
89  Xi = CLHEP::RandFlat::shoot(engine,fMinXi,fMaxXi);
90  double min_t = std::max(fMint,Minimum_t(Xi));
91  double max_t = fMaxt;
92  if (min_t>max_t) {
93  edm::LogInfo("RandomtXiGunProducer") << "WARNING: t limits redefined (unphysical values for given xi).\n";
94  max_t = min_t;
95  }
96  t = (fLog_t)?pow(CLHEP::RandFlat::shoot(engine,log10(min_t),log10(max_t)),10):
97  CLHEP::RandFlat::shoot(engine,min_t,max_t);
98  break;
99  }
100  phi = CLHEP::RandFlat::shoot(engine,fMinPhi, fMaxPhi) ;
101  HepMC::GenParticle* Part =
102  new HepMC::GenParticle(make_particle(t,Xi,phi,PartID,1),PartID,1);
103  Part->suggest_barcode( barcode ) ;
104  barcode++ ;
105  Vtx->add_particle_out(Part);
106  }
107  if ( fFireBackward) {
108  while(true) {
109  Xi = CLHEP::RandFlat::shoot(engine,fMinXi,fMaxXi);
110  double min_t = std::max(fMint,Minimum_t(Xi));
111  double max_t = fMaxt;
112  if (min_t>max_t) {
113  edm::LogInfo("RandomtXiGunProducer") << "WARNING: t limits redefined (unphysical values for given xi)." << endl;
114  max_t = min_t;
115  }
116  t = (fLog_t)?pow(CLHEP::RandFlat::shoot(engine,log10(min_t),log10(max_t)),10):
117  CLHEP::RandFlat::shoot(engine,min_t,max_t);
118  break;
119  }
120  phi = CLHEP::RandFlat::shoot(engine,fMinPhi, fMaxPhi) ;
121  HepMC::GenParticle* Part2 =
122  new HepMC::GenParticle(make_particle(t,Xi,phi,PartID,-1),PartID,1);
123  Part2->suggest_barcode( barcode ) ;
124  barcode++ ;
125  Vtx->add_particle_out(Part2) ;
126  }
127  }
128 
129  fEvt->add_vertex(Vtx) ;
130  fEvt->set_event_number(e.id().event()) ;
131  fEvt->set_signal_process_id(20) ;
132 
133  if ( fVerbosity > 0 )
134  {
135  fEvt->print() ;
136  }
137 
138  std::unique_ptr<HepMCProduct> BProduct(new HepMCProduct()) ;
139  BProduct->addHepMCData( fEvt );
140  e.put(std::move(BProduct),"unsmeared");
141 
142  std::unique_ptr<GenEventInfoProduct> genEventInfo(new GenEventInfoProduct(fEvt));
143  e.put(std::move(genEventInfo));
144 
145  if ( fVerbosity > 0 )
146  {
147  // for testing purpose only
148  // fEvt->print() ; // prints empty info after it's made into edm::Event
149  edm::LogInfo("RandomtXiGunProducer") << " Event Generation Done \n";
150  }
151 }
152 HepMC::FourVector RandomtXiGunProducer::make_particle(double t,double Xi,double phi,int PartID, int direction)
153 {
154  double mass = PData->mass().value() ;
155  double fpMom = sqrt(fpEnergy*fpEnergy-mass*mass); // momentum of beam proton
156  double sEnergy = (1.0-Xi)*fpEnergy; // energy of scattered particle
157  double sMom = sqrt(sEnergy*sEnergy-mass*mass); // momentum of scattered particle
158  double min_t = -2.*(fpMom*sMom-fpEnergy*sEnergy+mass*mass);
159  if (t<min_t) t=min_t; // protect against kinemactically forbiden region
160  long double theta = acos((-t/2.- mass*mass + fpEnergy*sEnergy)/(sMom*fpMom)); // use t = -t
161 
162  if (direction<1) theta = acos(-1.) - theta;
163 
164  double px = sMom*cos(phi)*sin(theta);
165  double py = sMom*sin(phi)*sin(theta);
166  double pz = sMom*cos(theta) ; // the direction is already set by the theta angle
167  if (fVerbosity > 0)
168  edm::LogInfo("RandomXiGunProducer") << "-----------------------------------------------------------------------------------------------------\n"
169  << "Produced a proton with phi : " << phi << " theta: " << theta << " t: " << t << " Xi: " << Xi << "\n"
170  << " Px : " << px << " Py : " << py << " Pz : " << pz << "\n"
171  << " direction: " << direction << "\n"
172  << "-----------------------------------------------------------------------------------------------------"
173  << std::endl;
174 
175  return HepMC::FourVector(px,py,pz,sEnergy) ;
176 }
177 //#include "FWCore/Framework/interface/MakerMacros.h"
178 //DEFINE_FWK_MODULE(RandomtXiGunProducer);
HepMC::FourVector make_particle(double t, double Xi, double phi, int PartID, int direction)
T getParameter(std::string const &) const
EventNumber_t event() const
Definition: EventID.h:41
T getUntrackedParameter(std::string const &, T const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
RandomtXiGunProducer(const ParameterSet &)
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
Geom::Theta< T > theta() const
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
ESHandle< HepPDT::ParticleDataTable > fPDGTable
void produce(Event &e, const EventSetup &es) override
edm::EventID id() const
Definition: EventBase.h:59
HLT enums.
StreamID streamID() const
Definition: Event.h:95
const HepPDT::ParticleData * PData
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
def move(src, dest)
Definition: eostools.py:511