CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
39  produces<HepMCProduct>("unsmeared");
40  produces<GenEventInfoProduct>();
41 }
42 
44 {
45  // no need to cleanup GenEvent memory - done in HepMCProduct
46 }
47 
49 {
51  CLHEP::HepRandomEngine* engine = &rng->getEngine(e.streamID());
52 
53  if ( fVerbosity > 0 )
54  {
55  cout << " RandomtXiGunProducer : Begin New Event Generation" << endl ;
56  }
57  // event loop (well, another step in it...)
58 
59  // no need to clean up GenEvent memory - done in HepMCProduct
60  //
61 
62  // here re-create fEvt (memory)
63  //
64  fEvt = new HepMC::GenEvent() ;
65 
66  // now actualy, cook up the event from PDGTable and gun parameters
67  //
68  // 1st, primary vertex
69  //
70  HepMC::GenVertex* Vtx = new HepMC::GenVertex(HepMC::FourVector(0.,0.,0.));
71 
72  // loop over particles
73  //
74  int barcode = 1 ;
75  for (unsigned int ip=0; ip<fPartIDs.size(); ++ip)
76  {
77  int PartID = fPartIDs[ip];
78 // t = -2*P*P'*(1-cos(theta)) -> t/(2*P*P')+1=cos(theta)
79 // xi = 1 - P'/P --> P'= (1-xi)*P
80 //
81  PData = fPDGTable->particle(HepPDT::ParticleID(abs(PartID)));
82  if (!PData) exit(1);
83  double t = 0;
84  double Xi = 0;
85  double phi=0;
86  if (fFireForward) {
87  while(1) {
88  Xi = CLHEP::RandFlat::shoot(engine,fMinXi,fMaxXi);
89  double min_t = std::max(fMint,Minimum_t(Xi));
90  double max_t = fMaxt;
91  if (min_t>max_t) {
92  std::cout << "WARNING: t limits redefined (unphysical values for given xi)." << endl;
93  max_t = min_t;
94  }
95  t = CLHEP::RandFlat::shoot(engine,min_t,max_t);
96  break;
97  }
98  phi = CLHEP::RandFlat::shoot(engine,fMinPhi, fMaxPhi) ;
99  HepMC::GenParticle* Part =
100  new HepMC::GenParticle(make_particle(t,Xi,phi,PartID,1),PartID,1);
101  Part->suggest_barcode( barcode ) ;
102  barcode++ ;
103  Vtx->add_particle_out(Part);
104  }
105  if ( fFireBackward) {
106  while(1) {
107  Xi = CLHEP::RandFlat::shoot(engine,fMinXi,fMaxXi);
108  double min_t = std::max(fMint,Minimum_t(Xi));
109  double max_t = fMaxt;
110  if (min_t>max_t) {
111  std::cout << "WARNING: t limits redefined (unphysical values for given xi)." << endl;
112  max_t = min_t;
113  }
114  t = CLHEP::RandFlat::shoot(engine,min_t,max_t);
115  break;
116  }
117  phi = CLHEP::RandFlat::shoot(engine,fMinPhi, fMaxPhi) ;
118  HepMC::GenParticle* Part2 =
119  new HepMC::GenParticle(make_particle(t,Xi,phi,PartID,-1),PartID,1);
120  Part2->suggest_barcode( barcode ) ;
121  barcode++ ;
122  Vtx->add_particle_out(Part2) ;
123  }
124  }
125 
126  fEvt->add_vertex(Vtx) ;
127  fEvt->set_event_number(e.id().event()) ;
128  fEvt->set_signal_process_id(20) ;
129 
130  if ( fVerbosity > 0 )
131  {
132  fEvt->print() ;
133  }
134 
135  std::unique_ptr<HepMCProduct> BProduct(new HepMCProduct()) ;
136  BProduct->addHepMCData( fEvt );
137  e.put(std::move(BProduct),"unsmeared");
138 
139  std::unique_ptr<GenEventInfoProduct> genEventInfo(new GenEventInfoProduct(fEvt));
140  e.put(std::move(genEventInfo));
141 
142  if ( fVerbosity > 0 )
143  {
144  // for testing purpose only
145  // fEvt->print() ; // prints empty info after it's made into edm::Event
146  cout << " RandomtXiGunProducer : Event Generation Done " << endl;
147  }
148 }
149 HepMC::FourVector RandomtXiGunProducer::make_particle(double t,double Xi,double phi,int PartID, int direction)
150 {
151  double mass = PData->mass().value() ;
152  double fpMom = sqrt(fpEnergy*fpEnergy-mass*mass); // momentum of beam proton
153  double sEnergy = (1.0-Xi)*fpEnergy; // energy of scattered particle
154  double sMom = sqrt(sEnergy*sEnergy-mass*mass); // momentum of scattered particle
155  double min_t = -2.*(fpMom*sMom-fpEnergy*sEnergy+mass*mass);
156  if (t<min_t) t=min_t; // protect against kinemactically forbiden region
157  long double theta = acos((-t/2.- mass*mass + fpEnergy*sEnergy)/(sMom*fpMom)); // use t = -t
158 
159  if (direction<1) theta = acos(-1.) - theta;
160 
161  double px = sMom*cos(phi)*sin(theta)*direction;
162  double py = sMom*sin(phi)*sin(theta);
163  double pz = sMom*cos(theta) ; // the direction is already set by the theta angle
164  if (fVerbosity > 0)
165  edm::LogInfo("RandomXiGunProducer") << "-----------------------------------------------------------------------------------------------------\n"
166  << "Produced a proton with phi : " << phi << " theta: " << theta << " t: " << t << " Xi: " << Xi << "\n"
167  << " Px : " << px << " Py : " << py << " Pz : " << pz << "\n"
168  << " direction: " << direction << "\n"
169  << "-----------------------------------------------------------------------------------------------------"
170  << std::endl;
171 
172  return HepMC::FourVector(px,py,pz,sEnergy) ;
173 }
174 //#include "FWCore/Framework/interface/MakerMacros.h"
175 //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
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:122
edm::Service< edm::RandomNumberGenerator > rng
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
def move
Definition: eostools.py:510
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
ESHandle< HepPDT::ParticleDataTable > fPDGTable
virtual void produce(Event &e, const EventSetup &es) override
edm::EventID id() const
Definition: EventBase.h:58
StreamID streamID() const
Definition: Event.h:81
tuple cout
Definition: gather_cfg.py:145
const HepPDT::ParticleData * PData