Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

MaGeGeneratorCosmicRayMuons.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * DISCLAIMER                                                       *
00004 // *                                                                  *
00005 // * Neither the authors of this software system, nor their employing *
00006 // * institutes,nor the agencies providing financial support for this *
00007 // * work  make  any representation or  warranty, express or implied, *
00008 // * regarding  this  software system or assume any liability for its *
00009 // * use.                                                             *
00010 // *                                                                  *
00011 // ********************************************************************
00012 //
00013 // This is supposed to be a generator for cosmic ray muons (with the
00014 // correct spectrum) in the tunnel. It is work in progress
00015 //
00016 // History:
00017 // --------
00018 // 28 Oct 2004   L.Pandola    First implementation (not working)
00019 
00020 #include "generators/MaGeGeneratorCosmicRayMuons.hh"
00021 #include "io/MJLogger.hh"
00022 
00023 #include "G4PrimaryParticle.hh"
00024 #include "G4Event.hh"
00025 #include "Randomize.hh"
00026 //#include <math.h>
00027 //#include <vector.h>
00028 #include "G4TransportationManager.hh"
00029 #include "G4ParticleTable.hh"
00030 #include "G4ParticleDefinition.hh"
00031 #include "G4MuonMinus.hh"
00032 #include "G4MuonPlus.hh"
00033 #include "G4DataVector.hh"
00034 
00035 MaGeGeneratorCosmicRayMuons::MaGeGeneratorCosmicRayMuons(G4String fileE,
00036                                                          G4String fileA) : 
00037   fFileEnergyName(fileE),fFileAngularName(fileA)
00038 {
00039   //particle initialization
00040   NumberOfParticlesToBeGenerated = 1;
00041   particle_definition = G4MuonMinus::MuonMinusDefinition();
00042   G4ThreeVector zero(0., 0., 0.);
00043   particle_momentum_direction = G4ParticleMomentum(0., 0., 1.);
00044   particle_energy = 500*GeV;
00045   particle_position = zero;
00046   particle_time = 0.0;
00047   particle_polarization = zero;
00048   particle_charge = particle_definition->GetPDGCharge();
00049 
00050   //geometry initialization
00051   halfz = 10.0*m;
00052   Radius = 5.0*m;
00053   verbosityLevel = 0;
00054 
00055   fEnergy = new G4DataVector();
00056   fEnergySpectrum = new G4DataVector();
00057 
00058   //open data files
00059   G4String path = "generators/data/";
00060   OpenEnergyFile(path+fFileEnergyName);
00061   OpenAngularFile(path+fFileAngularName);
00062   
00063 
00064   theMessenger = new MaGeGeneratorCosmicRayMuonsMessenger(this);
00065   gNavigator = G4TransportationManager::GetTransportationManager()
00066     ->GetNavigatorForTracking();
00067 }
00068 
00069 MaGeGeneratorCosmicRayMuons::~MaGeGeneratorCosmicRayMuons()
00070 {
00071   delete fEnergy;
00072   delete fEnergySpectrum;
00073   delete theMessenger;
00074 }
00075 
00076 void MaGeGeneratorCosmicRayMuons::SetHalfZ(G4double zhalf)
00077 {
00078   halfz = zhalf;
00079 }
00080 
00081 void MaGeGeneratorCosmicRayMuons::SetRadius(G4double rad)
00082 {
00083   Radius = rad;
00084 }
00085 
00086 void MaGeGeneratorCosmicRayMuons::SampleInitialPosition()
00087 {
00088   G4ThreeVector RandPos;
00089   G4double x=0., y=0., z=0.;
00090   
00091   //Bisogna generare le posizioni da un cerchio
00092 
00093   RandPos.setX(x);
00094   RandPos.setY(y);
00095   RandPos.setZ(z);
00096   G4ThreeVector CentreCoords(0.,0.,halfz);
00097   particle_position = CentreCoords + RandPos;
00098 }
00099 
00100 void MaGeGeneratorCosmicRayMuons::SetVerbosity(int vL)
00101 {
00102   verbosityLevel = vL;
00103   G4cout << "Verbosity Set to: " << verbosityLevel << G4endl;
00104 }
00105 
00106 void MaGeGeneratorCosmicRayMuons::GenerateAngularSpectrum()
00107 {;
00108 //bisogna leggere da un file 
00109 }
00110 
00111 void MaGeGeneratorCosmicRayMuons::GenerateEnergySpectrum()
00112 {
00113   G4double totIntegral = (*fEnergySpectrum)[fEnergySpectrum->size()-1]; 
00114   //final element = total area
00115   G4double random = G4UniformRand()*totIntegral;
00116   G4int j=0;
00117   for (size_t i=0;i<fEnergySpectrum->size()-2;i++)
00118     {
00119       //finds the right place
00120       if (random < (*fEnergySpectrum)[i+1] && random >= (*fEnergySpectrum)[i])
00121         j=i;
00122     }
00123  
00124   particle_energy=G4UniformRand()
00125     *((*fEnergy)[j+1]-(*fEnergy)[j])+(*fEnergy)[j];
00126   return;
00127 }
00128 
00129 void MaGeGeneratorCosmicRayMuons::SetParticleDefinition
00130   (G4ParticleDefinition* aParticleDefinition)
00131 {
00132   particle_definition = aParticleDefinition;
00133   particle_charge = particle_definition->GetPDGCharge();
00134 }
00135 
00136 
00137 void MaGeGeneratorCosmicRayMuons::GeneratePrimaryVertex(G4Event *evt)
00138 {
00139 
00140   if(particle_definition==NULL) {
00141     G4cout << "No particle has been defined!" << G4endl;
00142     return;
00143   }
00144   
00145   SampleInitialPosition();
00146 
00147   // Angular stuff
00148   GenerateAngularSpectrum();
00149 
00150   //Energy stuff
00151   GenerateEnergySpectrum();
00152   
00153   // create a new vertex
00154   G4PrimaryVertex* vertex = 
00155     new G4PrimaryVertex(particle_position,particle_time);
00156 
00157   if(verbosityLevel >= 2)
00158     G4cout << "Creating primaries and assigning to vertex" << G4endl;
00159 
00160   // create new primaries and set them to the vertex
00161   G4double mass =  particle_definition->GetPDGMass();
00162   G4double energy = particle_energy + mass;
00163   G4double pmom = sqrt(energy*energy-mass*mass);
00164   G4double px = pmom*particle_momentum_direction.x();
00165   G4double py = pmom*particle_momentum_direction.y();
00166   G4double pz = pmom*particle_momentum_direction.z();
00167 
00168   if(verbosityLevel >= 1){
00169     G4cout << "Particle name: " 
00170            << particle_definition->GetParticleName() << G4endl; 
00171     G4cout << "       Energy: "<<particle_energy << G4endl;
00172     G4cout << "     Position: "<<particle_position<< G4endl; 
00173     G4cout << "    Direction: "<<particle_momentum_direction << G4endl;
00174     G4cout << " NumberOfParticlesToBeGenerated: "
00175            << NumberOfParticlesToBeGenerated << G4endl;
00176   }
00177 
00178   if (G4UniformRand() > 0.5) 
00179     particle_definition = G4MuonMinus::MuonMinusDefinition();
00180   else
00181     particle_definition = G4MuonPlus::MuonPlusDefinition();
00182 
00183   for( G4int i=0; i<NumberOfParticlesToBeGenerated; i++ ) {
00184     G4PrimaryParticle* particle =
00185       new G4PrimaryParticle(particle_definition,px,py,pz);
00186     particle->SetMass( mass );
00187     particle->SetCharge( particle_charge );
00188     particle->SetPolarization(particle_polarization.x(),
00189                               particle_polarization.y(),
00190                               particle_polarization.z());
00191     vertex->SetPrimary( particle );
00192   }
00193 
00194   evt->AddPrimaryVertex( vertex );
00195   if(verbosityLevel > 1)
00196     G4cout << " Primary Vetex generated "<< G4endl;   
00197 }
00198 
00199 void MaGeGeneratorCosmicRayMuons::OpenEnergyFile(G4String filename)
00200 {
00201   G4DataVector* temporary = new G4DataVector();
00202 
00203   fEnergyInputFile.open(filename);
00204   if (!(fEnergyInputFile.is_open())) //not open correctly
00205     {
00206       MJLog(fatal) << "File could not be open" << endlog;
00207     }
00208   // The data format must be:
00209   // energy (GeV), spectrum
00210   // with evenly-spaced bins
00211   G4int nbin,i;
00212   G4double dat1, dat2;
00213   for (i=0; ;i++)
00214     {
00215       if (fEnergyInputFile.eof()) break;
00216       fEnergyInputFile >> dat1 >> dat2;
00217       fEnergy->push_back(dat1*GeV);
00218       temporary->push_back(dat2);
00219     }
00220   
00221   if (fEnergy->size() < 3) 
00222     MJLog(error) << "Invalid data file" << endlog;
00223   
00224   fEnergyInputFile.close();
00225   
00226   G4double binWidth = (*fEnergy)[1]-(*fEnergy)[0];
00227   fEnergy->push_back((*fEnergy)[fEnergy->size()-1]+0.5*binWidth); //
00228   //right edge of the last energy bin
00229 
00230   //Calculate cumulative distribution
00231   G4double cumul =0.0;
00232   for (i=0;i<temporary->size();i++) {
00233     fEnergySpectrum->push_back(cumul);
00234     cumul += (*temporary)[i];
00235   }
00236   fEnergySpectrum->push_back(cumul); //last one
00237   delete temporary;
00238 }
00239 
00240 void MaGeGeneratorCosmicRayMuons::OpenAngularFile(G4String filename)
00241 {;}

Generated on Mon Nov 29 16:58:50 2004 for Majorana Simulation by  doxygen 1.3.9.1