/home/bes3soft/bes3soft/Boss/7.0.2/dist/7.0.2/Generator/GeneratorModule/GeneratorModule-00-01-05/src/GenModule.cxx

Go to the documentation of this file.
00001 // -------------------------------------------------------------
00002 // File: GeneratorModule/GenModule.cxx
00003 // Description:
00004 //    This class is the base class used to specify the behavior of all
00005 //    event generator modules and is meant to capture the common behavior
00006 //    of these modules. 
00007 //
00008 // Header for this module:-
00009 
00010 #include <fstream>
00011 
00012 #include "GeneratorModule/GenModule.h"
00013 
00014 // Framework Related Headers:-
00015 
00016 #include "GaudiKernel/MsgStream.h"
00017 #include "GaudiKernel/ISvcLocator.h"
00018 #include "GaudiKernel/DataSvc.h"
00019 #include "GaudiKernel/SmartDataPtr.h"
00020 
00021 
00022 #include "GaudiKernel/IIncidentSvc.h"
00023 #include "GaudiKernel/Incident.h"
00024 
00025 #include "GaudiKernel/IPartPropSvc.h"
00026 
00027 // Other Packages used by this class:-
00028 #include "CLHEP/Random/Ranlux64Engine.h"
00029 #include "CLHEP/Random/RandPoisson.h"
00030 
00031 //#include "BesHepMC/GenVertex.h"
00032 #include "HepMC/GenVertex.h"
00033 
00034 #include "GeneratorObject/McGenEvent.h"
00035 
00036 
00037 //---------------------------------------------------------------------------
00038 GenModule::GenModule(const std::string& name, ISvcLocator* pSvcLocator)
00039   :     Algorithm(name, pSvcLocator),
00040         m_pRandomEngine(0),
00041         m_pPoissonGenerator(0)
00042   //---------------------------------------------------------------------------
00043 {
00044   declareProperty("FixedMode",m_fixedMode=true);
00045   declareProperty("MeanInt",m_meanInteractions=1.0);
00046   declareProperty("RandomSeed",m_randomSeed=1234567);
00047   declareProperty("StripPartons",m_StripVector);
00048   m_StripVector.push_back(0);
00049 
00050 }
00051 
00052 //---------------------------------------------------------------------------
00053 GenModule::~GenModule(){
00054   // Delete random number objects if they were created
00055 
00056   if(m_pPoissonGenerator) delete m_pPoissonGenerator;
00057   if(m_pRandomEngine) delete m_pRandomEngine;
00058 
00059 } 
00060 //---------------------------------------------------------------------------
00061 
00062 //---------------------------------------------------------------------------
00063 StatusCode GenModule::initialize(){
00064   //---------------------------------------------------------------------------
00065 
00066   // Inform the user what the mode and conditions are:
00067   MsgStream log(messageService(), name());
00068 
00069   // Initialize StripPartons variables
00070   if (m_StripVector[0] > 0) {
00071     StripPartonsInit();
00072   } else {
00073     m_StripPartonSwitch =       false;
00074     m_StripVector.clear();
00075   }
00076 
00077    
00078   // Get the Particle Properties Service
00079   IPartPropSvc* p_PartPropSvc;
00080   //static const bool CREATEIFNOTTHERE(true);
00081   //StatusCode PartPropStatus = service("PartPropSvc", p_PartPropSvc, CREATEIFNOTTHERE);
00082   StatusCode PartPropStatus = service("PartPropSvc", p_PartPropSvc);
00083   if (!PartPropStatus.isSuccess() || 0 == p_PartPropSvc) {
00084     log << MSG::ERROR << " Could not initialize Particle Properties Service" << endreq;
00085     return PartPropStatus;
00086   }      
00087 
00088   m_particleTable = p_PartPropSvc->PDT();
00089   
00090   m_pRandomEngine = new CLHEP::Ranlux64Engine(m_randomSeed);
00091 
00092   if(m_fixedMode) {
00093     if(m_meanInteractions == 1.0) {
00094       log << MSG::INFO << "Standard Initialization:  Single Interaction Mode "
00095           << endreq;
00096     }
00097     else {
00098       log << MSG::INFO << "Fixed Number of Interactions per Event is: "
00099           << m_meanInteractions << endreq;
00100     }
00101   }
00102   else {
00103     m_pPoissonGenerator = new CLHEP::RandPoisson(*m_pRandomEngine, 
00104                                           m_meanInteractions);
00105 
00106     log << MSG::INFO << 
00107       "Poisson Distribution of Interactions per Event with Mean: "
00108         << m_meanInteractions << endreq;
00109   }
00110   // Initialize the generator itself
00111   StatusCode status = this->genInitialize();
00112   if (status.isFailure()) {
00113     log << MSG::ERROR << "Could not initialize Generator properly" << endreq;
00114     return status;
00115   }  
00116   StatusCode  status1 = this->genuserInitialize();
00117   if (status1.isFailure()) {
00118     log << MSG::ERROR << "Could not initialize user part properly" << endreq;
00119     return status1;
00120   } 
00121   return status;
00122 }
00123 
00124 //---------------------------------------------------------------------------
00125 StatusCode GenModule::execute() {
00126   //---------------------------------------------------------------------------
00127     
00128   int numToGenerate;
00129   StatusCode status;
00130 
00131   MsgStream log(messageService(), name());
00132 
00133   log << MSG::DEBUG << "GenModule::execute()" << endreq;
00134 
00135   // Decide how many interactions to generate
00136   if(m_fixedMode)  {
00137     numToGenerate = (int) m_meanInteractions;
00138   }
00139   else {
00140     numToGenerate = m_pPoissonGenerator->fire();
00141   }
00142 
00143   // Generate as many interactions as requested
00144   for(int i=0; i<numToGenerate; i++) {
00145 
00146     // Call the code that generates an event
00147     status = this->callGenerator();
00148 
00149     // Create the MC event and send the GeneratorEvent stored in it to fillEvt
00150     GenEvent* evt = new GenEvent(1,1);
00151     status = this->fillEvt(evt);
00152 
00153     // Strip out the requested partons here.
00154     if (m_StripPartonSwitch)    StripPartons(evt);
00155     
00156     // Check if the McCollection already exists
00157     SmartDataPtr<McGenEventCol> anMcCol(eventSvc(), "/Event/Gen");
00158     if (anMcCol!=0) {
00159       // Add event to existing collection
00160       MsgStream log(messageService(), name());
00161       log << MSG::INFO << "Add McGenEvent to existing collection" << endreq;
00162       McGenEvent* mcEvent = new McGenEvent(evt);
00163       anMcCol->push_back(mcEvent);
00164     }
00165     else {
00166       // Create Collection and add  to the transient store
00167       McGenEventCol *mcColl = new McGenEventCol;
00168       McGenEvent* mcEvent = new McGenEvent(evt);
00169       mcColl->push_back(mcEvent);
00170 
00171       StatusCode sc = eventSvc()->registerObject("/Event/Gen",mcColl);
00172 
00173       if (sc != StatusCode::SUCCESS) {
00174         MsgStream log(messageService(), name());
00175         log << MSG::ERROR << "Could not register McGenEvent" << endreq;
00176         delete mcColl;
00177         delete evt;
00178         delete mcEvent;
00179         return StatusCode::FAILURE;
00180       }
00181       else {
00182 //        std::cout << " McGenEventCol made and stored" << std::endl;
00183       }
00184       
00185     }
00186   }
00187 
00188   // now call the incident service and set the signal.
00189   IIncidentSvc *incSvc;
00190   service("IncidentSvc",incSvc);
00191   incSvc->fireIncident( Incident( name(), "McGenEvent Generated") );
00192 
00193   return status;
00194 }
00195 
00196 //---------------------------------------------------------------------------
00197 StatusCode GenModule::finalize() {
00198   //---------------------------------------------------------------------------
00199 
00200   StatusCode status = this->genFinalize();
00201 
00202   return status;
00203 }
00204 
00205 //---------------------------------------------------------------------------
00206 StatusCode GenModule::genInitialize() { 
00207   //---------------------------------------------------------------------------
00208   return StatusCode::SUCCESS;
00209 }
00210 
00211 //---------------------------------------------------------------------------
00212 StatusCode GenModule::genuserInitialize() { 
00213   //---------------------------------------------------------------------------
00214   return StatusCode::SUCCESS;
00215 }
00216 
00217 //---------------------------------------------------------------------------
00218 StatusCode GenModule::callGenerator() { 
00219   //---------------------------------------------------------------------------
00220   return StatusCode::SUCCESS;
00221 }
00222 
00223 //---------------------------------------------------------------------------
00224 StatusCode GenModule::genFinalize() { 
00225   //---------------------------------------------------------------------------
00226   return StatusCode::SUCCESS;
00227 }
00228 
00229 //---------------------------------------------------------------------------
00230 StatusCode GenModule::fillEvt(GenEvent* /* evt */) {
00231   //---------------------------------------------------------------------------
00232   return StatusCode::SUCCESS;
00233 }
00234 
00235 void
00236 GenModule::StripPartonsInit     (void)
00237 {
00238   MsgStream log(messageService(), name());
00239 
00240   for (int i = 1; i < 9; ++i) { m_AllPartons.push_back(i); m_AllPartons.push_back(-i); } // Quarks
00241   m_AllPartons.push_back(21); // gluon
00242 
00243   m_StripPartonSwitch = true;
00244   m_StripVector.erase(m_StripVector.begin(), m_StripVector.begin()+1);
00245   // When the user specifies only the StripPartonSwitch or gives a Particle Code
00246   // which is NOT a parton then strip ALL partons
00247   if (m_StripVector.size() == 0) {
00248     log << MSG::INFO << " !!!! NO SPECIFIC PARTON FOR STRIPOUT WAS REQUESTED => STRIPOUT ALL "
00249         << endreq;
00250     m_StripVector = m_AllPartons;
00251   } else {
00252     bool        value_ok        =       true;
00253     std::vector<int>::const_iterator    i =     m_StripVector.begin();
00254     do {
00255       if ( std::find(m_AllPartons.begin(),
00256                      m_AllPartons.end(),
00257                      *i) == m_AllPartons.end() ) value_ok = false;
00258       ++i;
00259     } while (i != m_StripVector.end() && value_ok);
00260     if (!value_ok) {
00261       log << MSG::INFO << " !!!! ILEGAL PDG-ID FOR STRIPOUT WAS REQUESTED => STRIPOUT ALL "
00262           << endreq;
00263       m_StripVector = m_AllPartons;
00264     }
00265   }
00266   log << MSG::INFO << " THE FOLLOWING PARTON(S) WILL BE STRIPED OUT ";
00267   for (std::vector<int>::const_iterator ip = m_StripVector.begin(); ip != m_StripVector.end(); ++ip)
00268     log << *ip << "  ";
00269   log << endreq;
00270   
00271 }
00272 
00273 void
00274 GenModule::StripPartons (GenEvent* evt)
00275 {
00276   //    for ( HepMC::GenEvent::particle_iterator p = evt->particles_begin();
00277   //              p != evt->particles_end(); ++p )
00278   //    {
00279   //  //            std::cout << " PDG, BAR " << (*p)->pdg_id() << " " << (*p)->barcode() << std::endl;
00280   //        if ( std::find(m_StripVector.begin(),
00281   //                       m_StripVector.end(),
00282   //                       (*p)->pdg_id()) != m_StripVector.end() )
00283   //        {
00284   //  //                std::cout << " REMOVING " << (*p)->pdg_id() << " " << (*p)->barcode() << std::endl;
00285   //            HepMC::GenVertex*       pvtx    =       (*p)->production_vertex();
00286   //            HepMC::GenVertex*       evtx    =       (*p)->end_vertex();
00287   //            if (pvtx) pvtx->remove_particle(*p);
00288   //            if (evtx) evtx->remove_particle(*p);
00289   //            delete  *p;
00290   //        }
00291             
00292   //    }
00293         
00294   // Loop on all vertices of the event and remove the particle from the vertex
00295   for ( HepMC::GenEvent::vertex_iterator vtx = evt->vertices_begin();
00296         vtx != evt->vertices_end(); ++vtx ) {
00297     // Loop on all children particles and remove the ones that should be stripped out
00298     for ( HepMC::GenVertex::particle_iterator p = (*vtx)-> particles_begin(HepMC::children);
00299           p != (*vtx)->particles_end(HepMC::children);
00300           ++p ){
00301       if ( std::find(m_StripVector.begin(),
00302                      m_StripVector.end(),
00303                      (*p)->pdg_id()) != m_StripVector.end() ) {
00304         if ( (*p)->end_vertex() ) (*p)->end_vertex()->remove_particle(*p);
00305         if ( (*p)->production_vertex() ) (*p)->production_vertex()->remove_particle(*p);
00306         delete *p;
00307       }
00308     }
00309     // Loop on all parents particles and remove the ones that should be stripped out
00310     for ( HepMC::GenVertex::particle_iterator p = (*vtx)-> particles_begin(HepMC::parents);
00311           p != (*vtx)->particles_end(HepMC::parents);
00312           ++p ) {
00313       if ( std::find(m_StripVector.begin(),
00314                      m_StripVector.end(),
00315                      (*p)->pdg_id()) != m_StripVector.end() ) {
00316         if ( (*p)->end_vertex() ) (*p)->end_vertex()->remove_particle(*p);
00317         if ( (*p)->production_vertex() ) (*p)->production_vertex()->remove_particle(*p);
00318         delete *p;
00319       }
00320     }
00321   }
00322 }
00323 

Generated on Tue Nov 29 23:12:39 2016 for BOSS_7.0.2 by  doxygen 1.4.7