<commit_before>//____________________________________________________________________________
/*!

\class    GasTPCGun: Not the classical way to run a particle gun.
                     The way the simulation is setup is much easier to
                     generate each particle's kinematics and then propagate
                     into geant

\brief    Program to fire single particles using particle gun.
	  Only tracking, no flux or event generation

\author	  <NAME> <tstainer \at liv.ac.uk>
          University of Liverpool

\author	  <NAME> <georgios at hep.ph.liv.ac.uk>
          University of Liverpool

\created  Oct 2014
\last update Jun 2015
*/
//____________________________________________________________________________
#include "GasTPCProcessorManager.hh"
#include "GeometryLoader.hh"
//#include "ParticleGunProcessor.hh"
#include "Geant4TrackingAlgorithm.hh"
#include "GasTPCParticleGun.hh"

#include "TXMLEngine.h"
#include "Messenger.hh"

using namespace util;

int main(int argc, char ** argv) {

  // Load the xml file
  std::string datacard   = "";
  std::string outputfile = "PGData.root";
  int nentries = 100;
  for(int iArg = 1; iArg < argc; iArg++) {
    if((!strcasecmp(argv[iArg],"-d"))||(!strcasecmp(argv[iArg],"-datacard"))) {
      datacard = argv[++iArg] ;
    }
    if((!strcasecmp(argv[iArg],"-n"))) {
      nentries = atoi(argv[++iArg]);
      std::cout << "INFO::Generating " << nentries << " events." << std::endl;
    }
    if((!strcasecmp(argv[iArg],"-o"))||(!strcasecmp(argv[iArg],"-output"))) {
      outputfile = argv[++iArg] ;
    }
  }

  // Load the xml file
  const char* gastpc_dir = getenv("GasTPC");
  if(datacard == "")
    datacard = "GeantSimulation.xml";
  std::string fdatacard = string(gastpc_dir) + "/src/config/" + datacard;

  ReadDatacard(fdatacard);

  // Define the input tree
  TTree *pg = new TTree("pg","pg");

  double Px,Py,Pz,E,X,Y,Z,M;
  int pdg;

  pg->Branch("Px",    &Px,  "Px/D");
  pg->Branch("Py",    &Py,  "Py/D");
  pg->Branch("Pz",    &Pz,  "Pz/D");
  pg->Branch("E",     &E,   "E/D");
  pg->Branch("X",     &X,   "X/D");
  pg->Branch("Y",     &Y,   "Y/D");
  pg->Branch("Z",     &Z,   "Z/D");
  pg->Branch("M",     &M,   "M/D");
  pg->Branch("pdg",   &pdg, "pdg/I");

  // Fill the tree
  for(int i=0; i< nentries; i++){
    // Determine the mass
    double fMass = 105.66; // Muon as default
    pdg = fPDG;
    if(pdg == 22)
      fMass = 0;
    else if(abs(pdg) == 11)
      fMass = 0.511;
    else if(abs(pdg) == 13)
      fMass = 105.66;
    else if(abs(pdg) == 211)
      fMass = 139.57;
    else if(abs(pdg) == 321)
      fMass = 493.67;
    else if(abs(pdg) == 2212)
      fMass = 938.27;
    else if(abs(pdg) == 2112)
      fMass = 939.57;
    else{
      cout << "Choose the correct pdg to simulate. 11,-11,13,-13,22,211,-211,321,-321,2212,2112" << endl;
      break;
    }


    //determine monoenergetic or energy spectrum 
    if(fMinEnergy == fMaxEnergy){
      E = fMaxEnergy;
    }
    else{
      TRandom rand;
      rand.SetSeed(i+999);
      if(fgauss == 0)
	E = rand.Uniform(fMinEnergy,fMaxEnergy);
      else
	E = rand.Gaus(fMaxEnergy);
    }
    
    //if all 3 momentum are 0 then indicates to use Gaussian
    Px = fMomentumX;
    Py = fMomentumY;
    Pz = fMomentumZ;
    while( (Px == 0) && (Py == 0 ) && ( Pz == 0) ){
      TRandom3 rand;
      rand.SetSeed(i+999);
      Px = rand.Gaus();
      Py = rand.Gaus();
      Pz = rand.Gaus(); 
    }

    //normalise momentum to unit vectors and multiply by energy
    double Mom = TMath::Sqrt( Px*Px + Py*Py + Pz*Pz);
    //turn them into unit vectors and multiply by true momentum
    //M = fMass;
    Px *= (TMath::Sqrt( E*E - M*M ))/Mom;
    Py *= (TMath::Sqrt( E*E - M*M ))/Mom;
    Pz *= (TMath::Sqrt( E*E - M*M ))/Mom;
    
    X = fPositionX;
    Y = fPositionY;
    Z = fPositionZ;
    //pdg = fPDG;

    pg->Fill();
  }

  //try {
  // Create Processor Manage.
  GasTPCProcessorManager manager;
  manager.initialize(outputfile,fTrackingVerbose);

  //Initiate geometry loader
  //GeometryLoader * geomLoader = new GeometryLoader();
  
  // Initialize your processors here. The processors will define
  // default values for all runtime parameters.
  //ParticleGunProcessor   *g4tracking = new ParticleGunProcessor(geomLoader);
  
  // Get command line arguments if any. 
  // This will modify the default values for the runtime parameters.
  //manager.getRunTimeArguments(argc, argv);
  //manager.loadDataCards();

  // Initialize your processors here. The processors will define
  // default values for all runtime parameters.
  LoadNuEventProcessor     *eventLoader  = new LoadNuEventProcessor();
  eventLoader->initialize("particlegun",pg,fGeometryFile,1);
  if(nentries > 0)
    eventLoader->setNEvents(nentries);
  
  Geant4TrackingAlgorithm *g4trackingalg = new Geant4TrackingAlgorithm(fGeometryFile,fSimTargetVolume);
  //g4trackingalg->setGeometryFile(fGeometryFile);
  //g4trackingalg->setTargetVolumeName(fSimTargetVolume);
  g4trackingalg->setPhysicsList(fEMPhysicsList,fHadPhysicsList);
  g4trackingalg->setMagneticField(fMagneticFieldStrength);
  g4trackingalg->setEnergyCut(fMinEnergyCut);
  g4trackingalg->setDefaultCut(fGammaCut);
  g4trackingalg->setElectronCut(fElectronCut);
  g4trackingalg->setPositronCut(fPositronCut);
  g4trackingalg->setGammaCut(fGammaCut);
  g4trackingalg->setNeutronCut(fNeutronCut);
  g4trackingalg->setVerbose(fTrackingVerbose);
  //g4trackingalg->doVis(false);
  //g4trackingalg->doParticleGun(false);<commit_msg>Necessary changes to run the particle gun.<commit_after>//____________________________________________________________________________
/*!

\class    GasTPCGun: Not the classical way to run a particle gun.
                     The way the simulation is setup is much easier to
                     generate each particle's kinematics and then propagate
                     into geant

\brief    Program to fire single particles using particle gun.
	  Only tracking, no flux or event generation

\author	  <NAME> <tstainer \at liv.ac.uk>
          University of Liverpool

\author	  <NAME> <georgios at hep.ph.liv.ac.uk>
          University of Liverpool

\created  Oct 2014
\last update Jun 2015
*/
//____________________________________________________________________________
#include "GasTPCProcessorManager.hh"
#include "GeometryLoader.hh"
//#include "ParticleGunProcessor.hh"
#include "Geant4TrackingAlgorithm.hh"
#include "GasTPCParticleGun.hh"

#include "TXMLEngine.h"
#include "Messenger.hh"

using namespace util;

int main(int argc, char ** argv) {

  // Load the xml file
  std::string datacard   = "";
  std::string outputfile = "PGData.root";
  int nentries = 100;
  for(int iArg = 1; iArg < argc; iArg++) {
    if((!strcasecmp(argv[iArg],"-d"))||(!strcasecmp(argv[iArg],"-datacard"))) {
      datacard = argv[++iArg] ;
    }
    if((!strcasecmp(argv[iArg],"-n"))) {
      nentries = atoi(argv[++iArg]);
      std::cout << "INFO::Generating " << nentries << " events." << std::endl;
    }
    if((!strcasecmp(argv[iArg],"-o"))||(!strcasecmp(argv[iArg],"-output"))) {
      outputfile = argv[++iArg] ;
    }
  }

  // Load the xml file
  const char* gastpc_dir = getenv("GasTPC");
  if(datacard == "")
    datacard = "GeantSimulationPG.xml";
  std::string fdatacard = string(gastpc_dir) + "/src/config/" + datacard;

  ReadDatacard(fdatacard);

  if(fGeometryFile == "")
    fGeometryFile = "site.gdml";
  std::string fgeofile = string(gastpc_dir) + "/src/config/" + fGeometryFile;
  std::cout << "INFO::Geometry file :: " << fgeofile << std::endl;

  // Define the input tree
  TTree *pg = new TTree("pg","pg");

  double Px,Py,Pz,E,X,Y,Z,M;
  int pdg;

  pg->Branch("Px",    &Px,  "Px/D");
  pg->Branch("Py",    &Py,  "Py/D");
  pg->Branch("Pz",    &Pz,  "Pz/D");
  pg->Branch("E",     &E,   "E/D");
  pg->Branch("X",     &X,   "X/D");
  pg->Branch("Y",     &Y,   "Y/D");
  pg->Branch("Z",     &Z,   "Z/D");
  pg->Branch("M",     &M,   "M/D");
  pg->Branch("pdg",   &pdg, "pdg/I");

  // Fill the tree
  for(int i=0; i< nentries; i++){
    // Determine the mass
    double fMass = 105.66; // Muon as default
    pdg = fPDG;
    if(pdg == 22)
      fMass = 0;
    else if(abs(pdg) == 11)
      fMass = 0.511;
    else if(abs(pdg) == 13)
      fMass = 105.66;
    else if(abs(pdg) == 211)
      fMass = 139.57;
    else if(abs(pdg) == 321)
      fMass = 493.67;
    else if(abs(pdg) == 2212)
      fMass = 938.27;
    else if(abs(pdg) == 2112)
      fMass = 939.57;
    else{
      cout << "ERROR::Choose the correct pdg to simulate. 11,-11,13,-13,22,211,-211,321,-321,2212,2112" << endl;
      break;
    }


    //determine monoenergetic or energy spectrum 
    if(fMinEnergy == fMaxEnergy){
      E = fMaxEnergy;
    }
    else{
      TRandom rand;
      rand.SetSeed(i+999);
      if(fgauss == 0)
	E = rand.Uniform(fMinEnergy,fMaxEnergy);
      else
	E = rand.Gaus(fMaxEnergy);
    }
    
    //if all 3 momentum are 0 then indicates to use Gaussian
    Px = fMomentumX;
    Py = fMomentumY;
    Pz = fMomentumZ;
    while( (Px == 0) && (Py == 0 ) && ( Pz == 0) ){
      TRandom3 rand;
      rand.SetSeed(i+999);
      Px = rand.Gaus();
      Py = rand.Gaus();
      Pz = rand.Gaus(); 
    }

    //normalise momentum to unit vectors and multiply by energy
    double Mom = TMath::Sqrt( Px*Px + Py*Py + Pz*Pz);
    //turn them into unit vectors and multiply by true momentum
    //M = fMass;
    Px *= (TMath::Sqrt( E*E - M*M ))/Mom;
    Py *= (TMath::Sqrt( E*E - M*M ))/Mom;
    Pz *= (TMath::Sqrt( E*E - M*M ))/Mom;
    
    X = fPositionX;
    Y = fPositionY;
    Z = fPositionZ;
    //pdg = fPDG;

    pg->Fill();
  }

  //try {
  // Create Processor Manage.
  GasTPCProcessorManager manager;
  manager.initialize(outputfile,fTrackingVerbose);

  //Initiate geometry loader
  //GeometryLoader * geomLoader = new GeometryLoader();
  
  // Initialize your processors here. The processors will define
  // default values for all runtime parameters.
  //ParticleGunProcessor   *g4tracking = new ParticleGunProcessor(geomLoader);
  
  // Get command line arguments if any. 
  // This will modify the default values for the runtime parameters.
  //manager.getRunTimeArguments(argc, argv);
  //manager.loadDataCards();

  // Initialize your processors here. The processors will define
  // default values for all runtime parameters.
  LoadNuEventProcessor     *eventLoader  = new LoadNuEventProcessor();
  eventLoader->initialize("particlegun",pg,fgeofile,1);
  if(nentries > 0)
    eventLoader->setNEvents(nentries);
  
  Geant4TrackingAlgorithm *g4trackingalg = new Geant4TrackingAlgorithm(fgeofile,fSimTargetVolume);
  //g4trackingalg->setGeometryFile(fGeometryFile);
  //g4trackingalg->setTargetVolumeName(fSimTargetVolume);
  g4trackingalg->setPhysicsList(fEMPhysicsList,fHadPhysicsList);
  g4trackingalg->setMagneticField(fMagneticFieldStrength);
  g4trackingalg->setEnergyCut(fMinEnergyCut);
  g4trackingalg->setDefaultCut(fGammaCut);
  g4trackingalg->setElectronCut(fElectronCut);
  g4trackingalg->setPositronCut(fPositronCut);
  g4trackingalg->setGammaCut(fGammaCut);
  g4trackingalg->setNeutronCut(fNeutronCut);
  g4trackingalg->setVerbose(fTrackingVerbose);
  //g4trackingalg->doVis(false);
  //g4trackingalg->doParticleGun(false);
{'fraction_non_alphanumeric': 0.14753080946892455, 'fraction_numerical': 0.023229009663977302, 'mean_word_length': 3.127332601536773, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5698324', 'n_tokens_mistral': 4305, 'n_tokens_neox': 3980, 'n_words': 894}