1 #include "StarPrimaryMaker.h"
3 #include "g2t/St_g2t_particle_Module.h"
4 #include "tables/St_g2t_event_Table.h"
5 #include "tables/St_g2t_gepart_Table.h"
6 #include "tables/St_g2t_vertex_Table.h"
7 #include "tables/St_g2t_event_Table.h"
9 #include "StarGenerator.h"
10 #include "StarCallf77.h"
16 #include "StBFChain.h"
21 #include "StarGenerator/BASE/AgStarReader.h"
22 #include "StarGenerator/EVENT/StarGenEvent.h"
23 #include "StarGenerator/EVENT/StarGenParticle.h"
24 #include "StarGenerator/UTIL/StarRandom.h"
25 #include "StarGenerator/FILT/StarFilterMaker.h"
27 #include "TMCProcess.h"
29 #include "tables/St_vertexSeed_Table.h"
36 const double mmOverC = 1.0E-3 / TMath::C();
50 mVx(0), mVy(0), mVz(0), mSx(0.1), mSy(0.1), mSz(30.0), mRho(0), mVdxdz(0), mVdydz(0),
52 mPtMin(0), mPtMax(-1), mRapidityMin(0), mRapidityMax(-1), mPhiMin(0), mPhiMax(-1), mZMin(-999), mZMax(+999),
54 mPrimaryVertex(0,0,0,0),
60 assert(fgPrimary == 0);
64 SetAttr(
"usestarsim",
int(1) );
67 AddObj( mStack,
".const" );
74 SetAttr(
"FilterKeepHeader",
int(1) );
78 mVertexFunctionMap[
""] = std::bind( &StarPrimaryMaker::vertexGaussXYZ,
this );
79 mVertexFunctionMap[
"gaussXYZ"] = std::bind( &StarPrimaryMaker::vertexGaussXYZ,
this );
80 mVertexFunctionMap[
"flatZ"] = std::bind( &StarPrimaryMaker::vertexFlatZ,
this );
81 mVertexFunctionMap[
"flatABZ"] = std::bind( &StarPrimaryMaker::vertexFlatABZ,
this );
82 mVertexFunctionMap[
"flatRZ"] = std::bind( &StarPrimaryMaker::vertexFlatRZ,
this );
83 mVertexFunctionMap[
"flatXYZ"] = std::bind( &StarPrimaryMaker::vertexFlatXYZ,
this );
85 SetAttr(
"vertexDistribution",
"gaussXYZ" );
92 mVertexFunction = GetVertexFunction( SAttr(
"vertexDistribution") );
96 StarPrimaryMaker::~StarPrimaryMaker()
99 if ( mStack )
delete mStack;
100 if ( mFile )
delete mFile;
110 Int_t StarPrimaryMaker::Init()
113 if ( 1 == IAttr(
"usestarsim") ) {
114 LOG_INFO <<
"Registered starsim callbacks" << endm;
122 mDoBeamline = IAttr(
"beamline");
125 mVertexFunction = GetVertexFunction( SAttr(
"vertexDistribution") );
130 Int_t result = StMaker::Init();
138 mAccepted=
new TEventList(mFilter->
GetName(),
"Accepted events");
145 if ( mFileName ==
"" ) {
146 mFileName = ((
StBFChain*)StMaker::GetTopChain())->GetFileOut();
147 mFileName.ReplaceAll(
".root",
".gener.root");
150 mFile = TFile::Open( mFileName,
"recreate" );
153 mTree =
new TTree(
"genevents",
"TTree containing event generator information" );
155 mPrimaryEvent =
new StarGenEvent(
"primaryEvent",
"Primary Event... particle-wise information from all event generators");
156 mTree->Branch(
"primaryEvent",
"StarGenEvent",&mPrimaryEvent,64000,99);
158 if (mFilter) mFilter->SetEvent(mPrimaryEvent);
160 TIter
Next( GetMakeList() );
179 generator -> SetId(
id++ );
186 if ( generator->
IOmode()==0 )
188 generator -> SetOutputTree(
mTree );
208 mAccepted->Print(
"all");
209 mTree->SetEventList( mAccepted );
218 mTree->GetUserInfo()->Add( &particles );
223 TIter
Next( GetMakeList() );
237 if ( mFilter ) stats.nFilterSeen = mFilter->numberOfEvents();
238 if ( mFilter ) stats.nFilterAccept = mFilter->acceptedEvents();
248 Warning(
GetName(),
"Could not write to unopened file");
284 if ( IAttr(
"Debug")==1 )
event()->
Print(
"head");
289 if (
event()->GetFilterResult() & ( StarGenEvent::kAccept | StarGenEvent::kFlag ) )
305 if ( mAccepted ) mAccepted->Enter(
mTree->GetEntries() );
316 if ( IAttr(
"FilterKeepAll" ) == 0 ) mPrimaryEvent->
Clear(
"part");
317 if ( IAttr(
"FilterKeepAll" ) || IAttr(
"FilterKeepHeader" ) )
mTree->Fill();
323 if ( IAttr(
"FilterSkipRejects") )
return kStSKIP;
332 Int_t StarPrimaryMaker::InitRun( Int_t runnumber )
336 mRunNumber = runnumber;
338 mVertexFunction = GetVertexFunction( SAttr(
"vertexDistribution") );
340 return StMaker::InitRun( runnumber );
344 void StarPrimaryMaker::Clear(
const Option_t *opts )
350 if ( mPrimaryEvent ) {
351 mPrimaryEvent->
Clear();
354 TIter
Next( GetMakeList() );
374 mFilter ->
Shunt( GetDataSet(
".filter" ) );
377 Int_t StarPrimaryMaker::PreGenerate()
383 TDataSet* dbDataSet = GetChain()->GetDataBase(
"Calibrations/rhic/vertexSeed");
384 vertexSeed_st* vSeed = 0;
387 vSeed = ((St_vertexSeed*) (dbDataSet->FindObject(
"vertexSeed")))->GetTable();
393 mVdxdz = vSeed->dxdz;
394 mVdydz = vSeed->dydz;
397 LOG_WARN <<
"-- Beamline constraint requested but none seen in DB --" << endm;
403 LOG_INFO << Form(
"[%i]Beamline Parameters run=%i vx=%6.4f vy=%6.4f vz=%6.3f dxdz=%6.4f dydz=%6.4f",mDoBeamline,mRunNumber,mVx,mVy,mVz,mVdxdz,mVdydz) << endm;
406 TIter
Next( GetMakeList() );
410 generator -> PreGenerateHook();
416 Int_t StarPrimaryMaker::PostGenerate()
419 TIter
Next( GetMakeList() );
423 generator -> PostGenerateHook();
429 Int_t StarPrimaryMaker::Generate()
431 TIter
Next( GetMakeList() );
438 Double_t prob = generator -> GetPileup();
440 generator -> Generate();
452 Double_t ymin, Double_t ymax,
453 Double_t phimin, Double_t phimax,
454 Double_t zmin, Double_t zmax )
457 LOG_INFO <<
"-- StarPrimaryMaker::SetCuts --" <<
" ptmin=" << ptmin <<
" ptmax=" << ptmax << endm;
458 LOG_INFO <<
"-- StarPrimaryMaker::SetCuts --" <<
" ymin=" << ymin <<
" ymax=" << ymax << endm;
459 LOG_INFO <<
"-- StarPrimaryMaker::SetCuts --" <<
" phimn=" << phimin <<
" phimx=" << phimax << endm;
460 LOG_INFO <<
"-- StarPrimaryMaker::SetCuts --" <<
" zmin=" << zmin <<
" zmax=" << zmax << endm;
462 mPtMin = ptmin; mPtMax = ptmax;
463 mRapidityMin = ymin; mRapidityMax = ymax;
464 mPhiMin = phimin; mPhiMax = phimax;
465 mZMin = zmin; mZMax = zmax;
473 Bool_t go = particle->
Simulate();
if (!go)
return go;
475 Double_t px = particle -> GetPx();
476 Double_t py = particle -> GetPy();
477 Double_t pz = particle -> GetPz();
478 Double_t pt = TMath::Sqrt(px*px + py*py);
479 TVector3 p( px, py, pz );
482 if ( pt < mPtMin )
return false;
483 if ( mPtMin < mPtMax )
485 if ( pt > mPtMax )
return false;
489 if ( mRapidityMin < mRapidityMax )
491 if ( p.Eta() < mRapidityMin )
return false;
492 if ( p.Eta() > mRapidityMax )
return false;
504 Int_t StarPrimaryMaker::Finalize()
507 TIter
Next( GetMakeList() );
516 generator -> Finalize();
523 St_particle *table = (St_particle*) GetDataSet(
"particle");
527 Error(
GetName(),
"No particle table found, but we have particles.");
534 TLorentzVector primary = mVertexFunction();
while ( primary.Z() < mZMin || primary.Z() > mZMax ) primary = mVertexFunction();
536 mPrimaryVertex = primary;
549 TLorentzVector
vertex = (generator->
IsPileup())? mVertexFunction() : primary;
555 event -> SetOffset( offset );
563 for ( Int_t i=0; i<npart; i++ )
571 Double_t px = particle->
GetPx();
572 Double_t py = particle->
GetPy();
573 Double_t pz = particle->
GetPz();
575 Double_t M = particle->
GetMass();
576 Double_t vx = particle->
GetVx() / 10;
577 Double_t vy = particle->
GetVy() / 10;
578 Double_t vz = particle->
GetVz() / 10;
579 Double_t vt = particle->
GetTof() * mmOverC;
581 Double_t polx=0, poly=0, polz=0;
587 Int_t
id = particle->
GetId();
590 Double_t weight = 1.0;
631 mStack -> PushTrack( toDo, parent,
id, px, py, pz, E, vx, vy, vz, vt, polx, poly, polz, kPNoProcess, ntrack, weight, status );
635 if ( toDo ) table ->
AddAt( particle->get_hepevt_address(), nstack );
638 StarGenParticle *p = mPrimaryEvent->
AddParticle( status,
id, parent, parent2, kid1, kid2, px, py, pz, E, M, vx, vy, vz, vt );
641 p->
SetIndex( particle -> GetIndex() );
643 p->SetPrimaryKey( i + offset );
646 if ( toDo ) p->SetStack( 1 + nstack );
647 else p->SetStack( -1 );
649 if ( toDo ) { nstack++; ndone++; }
664 event->Clear(
"part");
672 void StarPrimaryMaker::BuildTables()
676 TIter
Next( GetMakeList() );
682 sum += generator ->
Event() -> GetNumberOfParticles();
687 St_g2t_event *g2t_event =
new St_g2t_event(
"event", 1 );
690 St_particle *g2t_particle =
new St_particle(
"particle",
mNumParticles );
701 static const TVector3 xhat(1,0,0), yhat(0,1,0), zhat(0,0,1);
703 if ( mVdxdz == 0.0 && mVdydz == 0.0 )
return;
706 TLorentzVector vertex(vx,vy,vz,vt);
707 TLorentzVector moment(px,py,pz,E);
709 Double_t thetaX = TMath::ATan( mVdxdz );
710 Double_t thetaY = TMath::ATan( mVdydz );
712 vertex.Rotate( -thetaY, xhat );
713 vertex.Rotate( +thetaX, yhat );
714 moment.Rotate( -thetaY, xhat );
715 moment.Rotate( +thetaX, yhat );
728 TLorentzVector StarPrimaryMaker::vertexGaussXYZ() {
729 double x=0,y=0,z=0,t=0;
734 double dist = TMath::Sqrt(x*x+y*y+z*z);
735 t = dist / TMath::Ccgs();
736 return TLorentzVector(x,y,z,t);
739 TLorentzVector StarPrimaryMaker::vertexFlatZ() {
740 double x=0,y=0,z=0,t=0;
745 double dist = TMath::Sqrt(x*x+y*y+z*z);
746 t = dist / TMath::Ccgs();
747 return TLorentzVector(x,y,z,t);
750 TLorentzVector StarPrimaryMaker::vertexFlatABZ() {
751 double x=0,y=0,z=0,t=0;
761 if ( (x/a)*(x/a) + (y/b)*(y/b) <= 1.0 )
break;
763 double dist = TMath::Sqrt(x*x+y*y+z*z);
764 t = dist / TMath::Ccgs();
765 TLorentzVector result(x,y,z,t);
766 result.RotateZ( mRho );
770 TLorentzVector StarPrimaryMaker::vertexFlatRZ() {
771 double x=0,y=0,z=0,t=0;
777 x = r * TMath::Cos(phi);
778 y = r * TMath::Sin(phi);
779 double dist = TMath::Sqrt(x*x+y*y+z*z);
780 t = dist / TMath::Ccgs();
781 TLorentzVector result(x,y,z,t);
785 TLorentzVector StarPrimaryMaker::vertexFlatXYZ() {
786 double x=0,y=0,z=0,t=0;
791 double dist = TMath::Sqrt(x*x+y*y+z*z);
792 t = dist / TMath::Ccgs();
793 return TLorentzVector(x,y,z,t);
796 std::function<TLorentzVector()> StarPrimaryMaker::GetVertexFunction(
const char* name ){
797 auto result = mVertexFunctionMap[
"gaussXYZ" ];
798 if ( mVertexFunctionMap.count(name) == 1 )
800 result = mVertexFunctionMap[ name ];
803 LOG_WARN <<
"Vertex function " << name <<
" not defined. Defaulting to gaussian." << endm;
static TParticlePDG * pdg(Int_t pdgid)
Double_t gauss(const Double_t sigma) const
Return a random number distributed according to a gaussian with specified sigma.
TTree * mTree
The output tree.
Float_t GetVz()
Get the z-component of the start vertex.
Bool_t Simulate(StarGenParticle *p)
Tests to see whether the particle passes all appropriate cuts to be passed to the simulator...
static StarRandom & Instance()
Obtain the single instance of the random number generator.
Bool_t IsPileup()
Returns true if this is a pileup generator.
Int_t IOmode()
Returns the ID of this event generator.
virtual void AddData(TDataSet *data, const char *dir=".data")
User methods.
Int_t GetNumberOfParticles()
Obtain the number of particles in the event record.
StarGenEvent * Event()
Retrieves the event record.
void Print(const Option_t *opts="head") const
Yet another particle class.
Int_t GetStatus()
Get the status code of the particle according to the HEPEVT standard.
Float_t GetTof()
Get the tof.
ABC for defining event generator interfaces.
Float_t GetPz()
Get the z-component of the momentum.
void Clear(const Option_t *opts="")
Clear the event.
virtual void Clear(Option_t *option="")
User defined functions.
Float_t GetEnergy()
Get the energy.
void SetIndex(Int_t i)
Set the line number in the event record.
void SetCuts(Double_t ptmin, Double_t ptmax=-1, Double_t ymin=0, Double_t ymax=-1, Double_t phimin=0, Double_t phimax=-1, Double_t zmin=-999, Double_t zmax=+999)
Set particle cuts.
virtual void Clear(const Option_t *opts="part,data")
Clear the event.
Int_t mNumParticles
Total number of particles.
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
static StarParticleData & instance()
Returns a reference to the single instance of this class.
TParticlePDG * GetParticle(const Char_t *name)
Get a particle by name.
Int_t GetId()
Get the id code of the particle according to the PDG standard.
static AgStarReader & Instance()
Return the single instance of this class.
Main filter class. Goes anywhere in the chain, filters StarGenEvent objects.
Interface to PDG information.
const TObjArray & GetParticles() const
Returns a reference to the list of particles.
void AddGenerator(StarGenerator *gener)
Int_t GetNumberOfParticles()
Returns the number of particles in the event.
Implementation of the VMC particle stack for use in STAR.
Double_t flat() const
Return a random number uniformly distributed between 0 and 1.
Float_t GetPx()
Get the x-component of the momentum.
End of run statistics for event generators.
virtual TDataSet * Next() const
Int_t GetLastMother()
Get the last mother particle.
Float_t GetVy()
Get the y-component of the start vertex.
Float_t GetPy()
Get the y-component of the momentum.
Int_t GetFirstMother()
Get the first mother particle.
Base class for event records.
virtual const char * GetName() const
special overload
Float_t GetVx()
Get the x-component of the start vertex.
StarGenEvent * event()
Return a pointer to the event.
Int_t GetLastDaughter()
Get the last daughter particle.
virtual StarGenStats Stats()
Create and retrieve end-of-run statistics.
virtual void AddAt(TDataSet *dataset, Int_t idx=0)
Main steering class for event generation.
virtual void Shunt(TDataSet *newParent=0)
TVector2 gauss2d(const Double_t sx, const Double_t sy, const Double_t rho) const
Returns a pair of random numbers generated according to a 2D gaussian.
void RotateBeamline(Double_t &px, Double_t &py, Double_t &pz, Double_t &E, Double_t &M, Double_t &vx, Double_t &vy, Double_t &vz, Double_t &vt)
Float_t GetMass()
Get the mass.
Int_t GetFirstDaughter()
Get the first daughter particle.
void SetStack(StarParticleStack *stack)
StarGenParticle * AddParticle()
virtual void Clear(const Option_t *opts="")
Clear the stack.
void SetRunNumber(Int_t run)
Set the run number for this event.
void AddFilter(StarFilterMaker *filt)