78#include "GaudiKernel/MsgStream.h"
82#include "CLHEP/Vector/TwoVector.h"
83#include "CLHEP/Vector/ThreeVector.h"
84#include "CLHEP/Geometry/Normal3D.h"
85#include "CLHEP/Geometry/Point3D.h"
89#include "CLHEP/Units/PhysicalConstants.h"
91#include "HepMC/GenEvent.h"
92#include "HepMC/GenVertex.h"
93#include "HepMC/GenParticle.h"
94#include "HepMC/Polarization.h"
96#include "GaudiKernel/Bootstrap.h"
97#include "GaudiKernel/ISvcLocator.h"
98#include "GaudiKernel/IMessageSvc.h"
99#include "GaudiKernel/GaudiException.h"
100#include "GaudiKernel/AlgFactory.h"
101#include "GaudiKernel/DataSvc.h"
102#include "GaudiKernel/SmartDataPtr.h"
103#include "GaudiKernel/MsgStream.h"
106#include "GaudiKernel/IDataProviderSvc.h"
107#include "GaudiKernel/PropertyMgr.h"
109#include "GaudiKernel/INTupleSvc.h"
110#include "GaudiKernel/NTuple.h"
111#include "GaudiKernel/Bootstrap.h"
112#include "GaudiKernel/IHistogramSvc.h"
123#include "CLHEP/Random/Ranlux64Engine.h"
124#include "CLHEP/Random/RandFlat.h"
132using namespace CLHEP;
134static inline int sign(
double x) {
return (x>0 ? 1: -1);}
149 return RandFlat::shoot(engine);
155 ISvcLocator* pSvcLocator): Algorithm(name,pSvcLocator)
170 declareProperty(
"eventfile", m_infile =
"NONE" );
171 declareProperty(
"emin",
m_emin =0.1 );
172 declareProperty(
"emax", m_emax =10. );
173 declareProperty(
"xvert_low", m_xlow =-110.7);
174 declareProperty(
"xvert_hig", m_xhig =110.7 );
175 declareProperty(
"zvert_low", m_zlow =-171.2);
176 declareProperty(
"zvert_hig", m_zhig = 171.2 );
177 declareProperty(
"yvert_val", m_yval = 0 );
178 declareProperty(
"tmin", m_tmin =0. );
179 declareProperty(
"tmax", m_tmax =24. );
180 declareProperty(
"tcor", m_tcor =0. );
181 declareProperty(
"IPx", m_IPx =0. );
182 declareProperty(
"IPy", m_IPy =0. );
183 declareProperty(
"IPz", m_IPz =0. );
184 declareProperty(
"Radius", m_radius =0. );
185 declareProperty(
"ExzCut", m_exzCut );
186 declareProperty(
"CubeProjection", m_cubeProj =
true );
187 declareProperty(
"OptimizeSphere", m_sphereOpt =
false );
190 declareProperty(
"SwapYZAxis", m_swapYZAxis =
false);
191 declareProperty(
"ctcut", m_ctcut =0.0 );
194 declareProperty(
"PrintEvent", m_printEvent=10);
195 declareProperty(
"PrintMod", m_printMod=100);
196 declareProperty(
"RMax", m_rmax = 10000000. );
197 declareProperty(
"ThetaMin", m_thetamin = 0.);
198 declareProperty(
"ThetaMax", m_thetamax = 1.);
199 declareProperty(
"PhiMin", m_phimin = -1*
PI);
200 declareProperty(
"PhiMax", m_phimax =
PI);
201 declareProperty(
"Xposition", m_xpos = 263.5-0.0001);
202 declareProperty(
"Yposition", m_ypos = 263.5-0.0001);
203 declareProperty(
"Zposition", m_zpos = 287.5-0.0001);
205 declareProperty(
"DoublePlaneTrigger", m_doublePlaneTrigger=
false);
206 declareProperty(
"xMinTop", m_xposMin_top =-16);
207 declareProperty(
"xMaxTop", m_xposMax_top = 16);
208 declareProperty(
"yTop", m_ypos_top = 52);
209 declareProperty(
"zMinTop", m_zposMin_top =-25);
210 declareProperty(
"zMaxTop", m_zposMax_top = 25);
211 declareProperty(
"xMinBottom", m_xposMin_bottom = -8);
212 declareProperty(
"xMaxBottom", m_xposMax_bottom = 8);
213 declareProperty(
"yBottom", m_ypos_bottom = -26);
214 declareProperty(
"zMinBottom", m_zposMin_bottom = -25);
215 declareProperty(
"zMaxBottom", m_zposMax_bottom = 25);
217 declareProperty(
"DumpMC", m_dumpMC = 0);
235 if(m_emin<0.1) {m_emin=0.1;std::cout<<
"WARNING: set emin to 0.1 GeV"<<std::endl;}
236 m_emin =m_emin *m_GeV;
237 m_emax =m_emax *m_GeV;
238 m_xlow =m_xlow *m_mm;
239 m_xhig =m_xhig *m_mm;
240 m_zlow =m_zlow *m_mm;
241 m_zhig =m_zhig *m_mm;
242 m_yval =m_yval *m_mm;
243 m_xpos =m_xpos *m_mm;
244 m_ypos =m_ypos *m_mm;
245 m_zpos =m_zpos *m_mm;
246 m_radius=m_radius*m_mm;
248 +sqrt((m_xpos-m_xlow)*(m_xpos-m_xlow)+(m_zpos-m_zlow)*(m_zpos-m_zlow)+(m_ypos-m_yval)*(m_ypos-m_yval))
249 /(m_emin/sqrt(m_emin*m_emin+
mass*
mass*m_GeV*m_GeV));
256 m_xposMin_bottom*=m_mm;
257 m_xposMax_bottom*=m_mm;
259 m_zposMin_bottom*=m_mm;
260 m_zposMax_bottom*=m_mm;
262 ISvcLocator* svcLocator = Gaudi::svcLocator();
264 StatusCode result = svcLocator->service(
"MessageSvc", p_msgSvc);
266 if ( !result.isSuccess() || p_msgSvc == 0 )
268 std::cerr <<
"VGenCommand(): could not initialize MessageSvc!" << std::endl;
269 throw GaudiException(
"Could not initalize MessageSvc",
"CosmicGenerator",StatusCode::FAILURE);
272 MsgStream log (p_msgSvc,
"CosmicGenerator");
278 m_tuple =
ntupleSvc()->book (
"FILE1/comp", CLID_ColumnWiseTuple,
"ks N-Tuple example");
280 status = m_tuple->addItem (
"cosmic_e", m_cosmicE);
281 status = m_tuple->addItem (
"cosmic_theta", m_cosmicTheta);
282 status = m_tuple->addItem (
"cosmic_phi", m_cosmicPhi);
283 status = m_tuple->addItem (
"cosmic_charge", m_cosmicCharge);
286 log << MSG::ERROR <<
" Cannot book N-tuple:" << long(m_tuple) << endmsg;
287 return StatusCode::FAILURE;
290 NTuplePtr nt1(
ntupleSvc(),
"FILE1/compp");
291 if(nt1) m_tuple1 = nt1;
293 m_tuple1 =
ntupleSvc()->book (
"FILE1/compp", CLID_ColumnWiseTuple,
"ks N-Tuple example");
295 status = m_tuple1->addItem (
"mc_theta", mc_theta);
296 status = m_tuple1->addItem (
"mc_phi", mc_phi);
297 status = m_tuple1->addItem (
"mc_px", mc_px);
298 status = m_tuple1->addItem (
"mc_py", mc_py);
299 status = m_tuple1->addItem (
"mc_pz", mc_pz);
302 log << MSG::ERROR <<
" Cannot book N-tuple:" << long(m_tuple1) << endmsg;
303 return StatusCode::FAILURE;
312 static const bool CREATEIFNOTTHERE(
true);
313 StatusCode RndmStatus = svcLocator->service(
"BesRndmGenSvc",
p_BesRndmGenSvc, CREATEIFNOTTHERE);
317 log << MSG::ERROR <<
" Could not initialize Random Number Service" << endreq;
328 log << MSG::INFO <<
"Initialisation cosmic gun done." << endreq;
329 log << MSG::INFO <<
"Accepted diff flux after E and cos(theta) cuts = " <<
330 flux_withCT <<
" /cm^2/s" << endreq;
331 log << MSG::INFO <<
"Accepted total flux after E and cos(theta) cuts = " <<
332 flux_withCT*(m_xhig-m_xlow)/m_mm*(m_zhig-m_zlow)/m_mm <<
" /s" << endreq;
334 std::cout<<
"Accepted diff flux after E and cos(theta) cuts = " <<
335 flux_withCT <<
" /cm^2/s" << std::endl;
336 std::cout <<
"Accepted total flux after E and cos(theta) cuts = " <<
337 flux_withCT*(m_xhig-m_xlow)/m_mm*(m_zhig-m_zlow)/m_mm <<
" /s" << std::endl;
342 log << MSG::INFO <<
"Cosmics are read from file " << m_infile << endreq;
343 m_ffile.open(m_infile.c_str());
346 log << MSG::FATAL <<
"Could not open input file - stop! " << endreq;
347 return StatusCode::FAILURE;
352 m_center=Hep3Vector(m_IPx, m_IPy, m_IPz);
353 log << MSG::INFO <<
"Initialisation done" << endreq;
354 return StatusCode::SUCCESS;
360 MsgStream log(messageService(), name());
368 float x_val = RandFlat::shoot(engine, m_xlow, m_xhig);
369 float z_val = RandFlat::shoot(engine, m_zlow, m_zhig);
374 t_val = RandFlat::shoot(engine, m_tmin, m_tmax);
376 else if(m_tmin == m_tmax){
379 else log << MSG::FATAL <<
" You specified m_tmin = " << m_tmin <<
" and m_tmax " << m_tmax << endreq;
380 HepLorentzVector p(x_val,m_yval,z_val, t_val*c_light);
389 MsgStream log(messageService(), name());
390 log << MSG::INFO <<
"CosmicGenerator executing" << endreq;
395 log << MSG::DEBUG <<
"Event #" << m_events << endreq;
401 m_polarization.clear();
412 std::cout << evt << std::endl;
417 Polarization thePolarization;
418 thePolarization.set_normal3d(
HepNormal3D(polx,poly,polz));
419 m_polarization.push_back(thePolarization);
424 m_fourPos.push_back(evt.
Vertex());
425 m_fourMom.push_back(evt.
Momentum());
426 m_pdgCode.push_back(evt.
pdgID());
431 log << MSG::FATAL <<
"End of file reached - stop " << endreq;
433 return StatusCode::FAILURE;
441 bool projected=
false;
444 HepLorentzVector vert;
445 HepLorentzVector vert_proj;
449 Hep3Vector vert3(vert.x(),vert.y(),vert.z());
453 m_cosmicE=pp.e()*m_GeV;
454 m_cosmicTheta=pp.theta();
455 m_cosmicPhi=pp.phi();
460 double phi1=pp.phi();
461 double mag1=pp.vect().mag();
466 Hep3Vector direction(pp_corr.x(),pp_corr.y(), pp_corr.z());
467 Hep3Vector center_dir=m_center-vert3;
475 beta=direction.angle(center_dir);
476 alpha=asin(m_radius/center_dir.mag());
477 if(fabs(beta)>
alpha){
478 log<<MSG::DEBUG<<
alpha<<
", "<<beta<<
" rejected muon due to sphere cut! "<<endreq;
484 double l_fight0,l_fight1, l_fight2;
485 double coor_x0, coor_y0, coor_z0;
486 double coor_x1, coor_y1, coor_z1;
487 double coor_x2, coor_y2, coor_z2;
497 coor_x0 = direction.x()*(coor_y0 - vert.y())/direction.y() +vert.x();
498 coor_z0 = direction.z()*(coor_y0 - vert.y())/direction.y() +vert.z();
502 if( fabs(coor_x0) <= m_xpos && fabs(coor_z0) <= m_zpos){
503 vert_pro0=
HepPoint3D (coor_x0, coor_y0, coor_z0);
504 l_fight0=vert_org.distance(vert_pro0);
509 coor_z1 = sign(coor_z0-vert.z())*m_zpos;
510 coor_x1 = direction.x()*(coor_z1 - vert.z())/direction.z() +vert.x();
511 coor_y1 = direction.y()*(coor_z1 - vert.z())/direction.z() +vert.y();
513 vert_pro1=
HepPoint3D (coor_x1, coor_y1, coor_z1);
514 l_fight1=vert_org.distance(vert_pro1);
517 coor_x2 = sign(coor_x0-vert.x())*m_xpos;
518 coor_z2 = direction.z()*(coor_x2 - vert.x())/direction.x() +vert.z();
519 coor_y2 = direction.y()*(coor_x2 - vert.x())/direction.x() +vert.y();
520 vert_pro2=
HepPoint3D (coor_x2, coor_y2, coor_z2);
521 l_fight2=vert_org.distance(vert_pro2);
522 if(l_fight1<l_fight2)
529 vert_proj=HepLorentzVector (vert_pro0.x(),vert_pro0.y(),vert_pro0.z() , l_fight0);
536 vert_proj=HepLorentzVector (vert_pro1.x(),vert_pro1.y(),vert_pro1.z() , l_fight1);
543 vert_proj=HepLorentzVector (vert_pro2.x(),vert_pro2.y(),vert_pro2.z() , l_fight2);
549 log<<MSG::DEBUG<<
" rejected muon due to cube projection request! "<<endreq;
557 else if(m_doublePlaneTrigger)
559 double coor_x0, coor_y0, coor_z0;
560 coor_y0 = m_ypos_top;
561 coor_x0 = direction.x()*(coor_y0 - vert.y())/direction.y() +vert.x();
562 coor_z0 = direction.z()*(coor_y0 - vert.y())/direction.y() +vert.z();
564 double coor_x1, coor_y1, coor_z1;
565 coor_y1 = m_ypos_bottom;
566 coor_x1 = direction.x()*(coor_y1 - vert.y())/direction.y() +vert.x();
567 coor_z1 = direction.z()*(coor_y1 - vert.y())/direction.y() +vert.z();
569 if(coor_x0>m_xposMin_top && coor_x0<m_xposMax_top && coor_z0>m_zposMin_top && coor_z0<m_zposMax_top
570 && coor_x1>m_xposMin_bottom && coor_x1<m_xposMax_bottom && coor_z1>m_zposMin_bottom && coor_z1<m_zposMax_bottom)
592 m_pdgCode.push_back(
charge*-13);
607 Polarization thePolarization;
617 thePolarization.set_normal3d(
HepNormal3D(polx,poly,polz));
620 thePolarization.set_normal3d(
HepNormal3D(polx,polz,-poly));
622 m_polarization.push_back(thePolarization);
628 double theta = pp.theta();
629 double phi = pp.phi();
638 <<
"Event #" << m_events
639 <<
" E=" << e <<
", mass=" <<
mass
640 <<
" -- you have generated a tachyon! Increase energy or change particle ID."
642 return StatusCode::FAILURE;
646 double px = p*
sin(theta)*
cos(phi);
647 double pz = p*
sin(theta)*
sin(phi);
648 double py = -p*
cos(theta);
661 m_fourMom.push_back(HepLorentzVector(px,py,pz,pp.e()));
664 m_fourMom.push_back(HepLorentzVector(px,pz,-py,pp.e()));
674 t = vert.t()-vert_proj.t()/HepLorentzVector(px,py,pz,pp.e()).beta()
692 m_fourPos.push_back(HepLorentzVector(
x,
y,z,
t));
694 m_fourPos.push_back(HepLorentzVector(
x,z,
y,
t));
698 << m_fourPos.back().x() <<
","
699 << m_fourPos.back().y() <<
","
700 << m_fourPos.back().z() <<
","
701 << m_fourPos.back().t() <<
"), (Px,Py,Pz,E) = ("
702 << m_fourMom.back().px() <<
","
703 << m_fourMom.back().py() <<
","
704 << m_fourMom.back().pz() <<
","
705 << m_fourMom.back().e() <<
")"
708 <<
" (theta,phi) = (" << theta <<
"," << phi <<
"), "
709 <<
"polarization(x,y,z) = ("
710 << m_polarization.back().normal3d().x() <<
","
711 << m_polarization.back().normal3d().y() <<
","
712 << m_polarization.back().normal3d().z() <<
")"
719 mc_theta=m_fourMom.back().theta();
720 mc_phi=m_fourMom.back().phi();
721 mc_px=m_fourMom.back().px();
722 mc_py=m_fourMom.back().py();
723 mc_pz=m_fourMom.back().pz();
731 GenEvent*
event =
new GenEvent(1,1);
733 if(m_fourMom.size()==m_fourPos.size()&&m_fourMom.size()==m_polarization.size()){
735 for(std::size_t
v=0;
v<m_fourMom.size();++
v){
742 GenParticle* particle =
new GenParticle( m_fourMom[
v], m_pdgCode[
v], 1);
743 particle->set_polarization( m_polarization[
v] );
746 GenVertex* vertex =
new GenVertex(m_fourPos[
v]);
747 vertex->add_particle_out( particle );
750 event->add_vertex( vertex );
754 event->set_event_number(m_events);
760 log<<MSG::ERROR<<
"Wrong different number of vertexes/momenta/polaritazions!"<<endreq;
761 return StatusCode::FAILURE;
764 SmartDataPtr<McGenEventCol> anMcCol(eventSvc(),
"/Event/Gen");
769 log << MSG::INFO <<
"Add McGenEvent to existing collection" << endreq;
771 anMcCol->push_back(mcEvent);
778 mcColl->push_back(mcEvent);
779 StatusCode sc = eventSvc()->registerObject(
"/Event/Gen",mcColl);
780 if (sc != StatusCode::SUCCESS)
782 log << MSG::ERROR <<
"Could not register McGenEvent" << endreq;
786 return StatusCode::FAILURE;
795 return StatusCode::SUCCESS;
807 MsgStream log(messageService(), name());
809 log << MSG::INFO<<
"***************************************************"<<endreq;
810 log << MSG::INFO <<
"** you have run CosmicGenerator with some " <<endreq;
811 log << MSG::INFO <<
"** filters for cosmic muon simulation" <<endreq;
812 log << MSG::INFO <<
"** "<<m_tried<<
" muons were tried" <<endreq;
813 log << MSG::INFO <<
"** "<<m_accepted<<
" muons were accepted" <<endreq;
814 log << MSG::INFO <<
"** "<<m_rejected<<
" muons were rejected" <<endreq;
815 log << MSG::INFO<<
"***************************************************"<<endreq;
816 std::cout<<
"***************************************************"<<std::endl;
817 std::cout <<
"** you have run CosmicGenerator with some " <<std::endl;
818 std::cout <<
"** filters for cosmic muon simulation" <<std::endl;
819 std::cout <<
"** "<<m_tried<<
" muons were tried" <<std::endl;
820 std::cout <<
"** "<<m_accepted<<
" muons were accepted" <<std::endl;
821 std::cout <<
"** "<<m_rejected<<
" muons were rejected" <<std::endl;
822 std::cout<<
"***************************************************"<<std::endl;
826 return StatusCode::SUCCESS;
833bool CosmicGenerator::exzCut(
const Hep3Vector& pos,
const HepLorentzVector& p)
840 r = sqrt((pow(pos.x(),2)+pow(pos.z()+28000,2))) ;
841 double e = 0.45238*r+5000 ;
846 r = sqrt((pow(pos.x(),2)+pow(pos.z()-20000,2))) ;
851 double e = 0.461538*(r-15000)+10000 ;
double sin(const BesAngle a)
double cos(const BesAngle a)
HepGeom::Point3D< double > HepPoint3D
HepGeom::Normal3D< float > HepNormal3D
*********Class see also m_nmax DOUBLE PRECISION m_emin
**********Class see also m_nmax DOUBLE PRECISION m_amel DOUBLE PRECISION m_x2 DOUBLE PRECISION m_alfinv DOUBLE PRECISION m_Xenph INTEGER m_KeyWtm INTEGER m_idyfs DOUBLE PRECISION m_zini DOUBLE PRECISION m_q2 DOUBLE PRECISION m_Wt_KF DOUBLE PRECISION m_WtCut INTEGER m_KFfin *COMMON c_KarLud $ !Input CMS energy[GeV] $ !CMS energy after beam spread beam strahlung[GeV] $ !Beam energy spread[GeV] $ !z boost due to beam spread $ !electron beam mass *ff pair spectrum $ !minimum v
ObjectVector< McGenEvent > McGenEventCol
const HepLorentzVector & Vertex(void)
const HepLorentzVector & Momentum(void)
HepLorentzVector generateVertex(void)
static IBesRndmGenSvc * p_BesRndmGenSvc
void PrintLevel(int printevt, int printmod)
void SetEnergyRange(float emin, float emax)
HepLorentzVector GenerateEvent(void)
float InitializeGenerator()
void SetCosCut(float ctcut)
static CosmicGun * GetCosmicGun(void)
manage multiple CLHEP random engines as named streams
virtual CLHEP::HepRandomEngine * GetEngine(const std::string &StreamName)=0
Interface to the CLHEP engine.