SToGS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
toROOTGPSPrimaryGeneratorAction.cc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 
29 
30 #include "G4Event.hh"
31 #include "G4ParticleGun.hh"
32 #include "G4ParticleTable.hh"
33 #include "G4ParticleDefinition.hh"
34 #include "Randomize.hh"
35 
36 #include "TChain.h"
37 
38 // std includes
39 #include <iostream>
40 #include <sstream>
41 #include <fstream>
42 
43 #if G4MULTITHREADED
44 #include "G4Threading.hh"
45 #endif
46 #ifdef G4MULTITHREADED
47 #include "G4AutoLock.hh"
48 namespace { G4Mutex buildMutex = G4MUTEX_INITIALIZER; G4Mutex runMutex = G4MUTEX_INITIALIZER; }
49 #endif
50 
51 using namespace std;
52 
53 
56 
58  G4VUserPrimaryGeneratorAction(),
59  theChainOfPrimaryEvents(0x0),
60  fPr(0x0),
61  fCurrentEntry(0L),
62  fEntries(0L)
63 {
64  fPr = new SBRPEvent(); fEntries = fCurrentEntry = 0L;
65  /*
66 
67  NumberOfParticlesToBeGenerated = 1;
68  particle_definition = G4Geantino::GeantinoDefinition();
69  G4ThreeVector zero;
70  particle_momentum_direction = G4ParticleMomentum(1, 0, 0);
71  particle_energy = 1.0 * MeV;
72  particle_position = zero;
73  particle_time = 0.0;
74  particle_polarization = zero;
75  particle_charge = 0.0;
76  particle_weight = 1.0;
77 
78  biasRndm = new G4SPSRandomGenerator();
79  posGenerator = new G4SPSPosDistribution();
80  posGenerator->SetBiasRndm(biasRndm);
81  angGenerator = new G4SPSAngDistribution();
82  angGenerator->SetPosDistribution(posGenerator);
83  angGenerator->SetBiasRndm(biasRndm);
84  eneGenerator = new G4SPSEneDistribution();
85  eneGenerator->SetBiasRndm(biasRndm);
86 
87  */
88  GetInputFiles();
89  theMessanger = new toROOTGPSPrimaryGeneratorActionMessanger(this);
90 }
91 
93  G4VUserPrimaryGeneratorAction(),
94  theChainOfPrimaryEvents(0x0),
95  fPr(0x0),
96  fCurrentEntry(0L),
97  fEntries(0L)
98 {
99  fPr = new SBRPEvent(); fEntries = fCurrentEntry = 0L;
100 
101  GetInputFiles(filename);
102  theMessanger = new toROOTGPSPrimaryGeneratorActionMessanger(this);
103 }
104 
106 {
107  delete theMessanger;
108 }
109 
111 {
112  G4ThreeVector particle_position(0,0,0), particle_momentum_direction;
113 
114  G4PrimaryParticle* particle = 0x0; G4PrimaryVertex* vertex = new G4PrimaryVertex(particle_position, 0);
115 
116  {
117 #if G4MULTITHREADED
118  G4AutoLock lock(&runMutex); // better to be protected since root is doing some stuff in global
119 #endif
120  if ( !(fCurrentEntry < fEntries) )
121  return;
122  theChainOfPrimaryEvents->GetEntry(fCurrentEntry++);
123  }
124 
125 
126  // create a new vertex
127  for (G4int i = 0; i < fPr->GetNbHits(); i++) {
128 
129  // get next Pr Particle
130  SBRPHit *aprimary = fPr->GetHit(i);
131  particle =
132  new G4PrimaryParticle(aprimary->fPDG);
133 
134  // particle->SetKineticEnergy( aprimary->fE*CLHEP::MeV );
135  // particle_momentum_direction.set(aprimary->fPX,aprimary->fPY,aprimary->fPZ);
136  // particle->SetMomentumDirection( particle_momentum_direction.unit() );
137  // particle->SetMomentumDirection( particle_momentum_direction.unit() );
138  particle->Set4Momentum(aprimary->fPX*aprimary->fE*CLHEP::MeV,
139  aprimary->fPY*aprimary->fE*CLHEP::MeV,
140  aprimary->fPZ*aprimary->fE*CLHEP::MeV,
141  aprimary->fE*CLHEP::MeV);
142 
143  //particle->SetPolarization(0,0,0);
144  /*if (verbosityLevel > 1) {
145 
146  G4cout << "Particle name: " << particle_definition->GetParticleName() << G4endl;
147  G4cout << " Energy: " << particle_energy << G4endl;
148  G4cout << " Position: " << particle_position << G4endl;
149  G4cout << " Direction: " << particle_momentum_direction
150  << G4endl;
151  } */
152  // Set bweight equal to the multiple of all non-zero weights
153  // particle_weight = eneGenerator->GetWeight()*biasRndm->GetBiasWeight();
154  // pass it to primary particle
155  particle->SetWeight(1);
156 
157  vertex->SetPrimary(particle);
158  }
159  anEvent->AddPrimaryVertex(vertex);
160 
161 
162 
163  /*
164  G4AutoLock l(&mutex);
165  if (particle_definition == NULL)
166  return;
167 
168  if (verbosityLevel > 1)
169  G4cout << " NumberOfParticlesToBeGenerated: "
170  << NumberOfParticlesToBeGenerated << G4endl;
171 
172  // Position stuff
173  particle_position = posGenerator->GenerateOne();
174 
175  // create a new vertex
176  G4PrimaryVertex* vertex = new G4PrimaryVertex(particle_position,
177  131 particle_time);
178 
179  for (G4int i = 0; i < NumberOfParticlesToBeGenerated; i++) {
180  // Angular stuff
181  particle_momentum_direction = angGenerator->GenerateOne();
182  // Energy stuff
183  particle_energy = eneGenerator->GenerateOne(particle_definition);
184 
185  if (verbosityLevel >= 2)
186  G4cout << "Creating primaries and assigning to vertex" << G4endl;
187  // create new primaries and set them to the vertex
188  G4double mass = particle_definition->GetPDGMass();
189  G4PrimaryParticle* particle =
190  new G4PrimaryParticle(particle_definition);
191  particle->SetKineticEnergy( particle_energy );
192  particle->SetMass( mass );
193  particle->SetMomentumDirection( particle_momentum_direction );
194  particle->SetCharge( particle_charge );
195  particle->SetPolarization(particle_polarization.x(), particle_polarization.y(),particle_polarization.z());
196  if (verbosityLevel > 1) {
197  G4cout << "Particle name: "
198  << particle_definition->GetParticleName() << G4endl;
199  G4cout << " Energy: " << particle_energy << G4endl;
200  G4cout << " Position: " << particle_position << G4endl;
201  G4cout << " Direction: " << particle_momentum_direction
202  << G4endl;
203  }
204  // Set bweight equal to the multiple of all non-zero weights
205  particle_weight = eneGenerator->GetWeight()*biasRndm->GetBiasWeight();
206  // pass it to primary particle
207  particle->SetWeight(particle_weight);
208 
209  vertex->SetPrimary(particle);
210 
211  }
212  // now pass the weight to the primary vertex. CANNOT be used here!
213  // vertex->SetWeight(particle_weight);
214  evt->AddPrimaryVertex(vertex);
215  if (verbosityLevel > 1)
216  G4cout << " Primary Vetex generated !" << G4endl;
217  */
218 
219 }
220 
221 
223 {
224  G4cout << G4endl << " ------ INFO ------ from toROOTGPSPrimaryGeneratorAction::GetInputFiles " << (void*)(this) << G4endl;
225 
226  // for MT, thread id used in the name of the file to avoid pb
227  std::ostringstream fullfilename;
228  fullfilename.clear();
229  //
230 #if G4MULTITHREADED
231  G4AutoLock lock(&buildMutex); // better to be protected since root is doing some stuff in global
232  G4int thread_id = 0;
233  thread_id = G4Threading::G4GetThreadId();
234  fullfilename << filename << "_Thread" << std::setfill('0') << std::setw(2) << thread_id << ".gene";
235 #else
236  fullfilename << filename << ".gene";
237 #endif
238  //
239  // open the ascii file
240  ifstream file; file.open(fullfilename.str().data());
241  if ( file.is_open() == false ) {
242  G4cout << " ** WARNING ** cannot open file " << fullfilename << " (Default parameters are used) "<< G4endl;
243  return;
244  }
245 
246  // read the file and get the cascade
247  std::string aline, key, rfile; std::getline (file,aline);
248  while ( file.good() ) {
249 
250  if ( aline[0] == '#' ) {
251  std::getline (file,aline);
252  continue;
253  } // this line is a comment
254 
255  // file to be added to the chain
256  std::istringstream decode(aline);
257  decode >> key >> rfile;
258 
259  if ( key == "tree_name" ) {
260  theChainOfPrimaryEvents = new TChain(rfile.data());
261  std::getline (file,aline);
262 
263  G4cout << " ==> TTree expected name is " << rfile.data() << G4endl;
264  continue;
265  }
266  // default is ROOTGPS
267  if ( theChainOfPrimaryEvents == 0x0 ) {
268  theChainOfPrimaryEvents = new TChain("ROOTGPS");
269  G4cout << " ==> TTree expected name is ROOTGPS " << G4endl;
270  }
271  Int_t added = theChainOfPrimaryEvents->AddFile(rfile.data());
272  if ( added ) {
273  G4cout << " ==> add the file to TChain " << rfile.data() << G4endl;
274  }
275  else
276  G4cout << " !!! Cannot add the file to TChain " << rfile.data() << G4endl;
277 
278  std::getline (file,aline);
279 
280  }
281  // theChainOfPrimaryEvents->Print();
282  theChainOfPrimaryEvents->SetBranchAddress("Pr.",&fPr);
283  fEntries = theChainOfPrimaryEvents->GetEntries();
284 
285  G4cout << " ------ END ------ from toROOTGPSPrimaryGeneratorAction::GetInputFiles " << G4endl << G4endl;
286 }
287 
288 #include "G4UIdirectory.hh"
289 #include "G4UIcmdWithAString.hh"
290 
292 {
293  theDirectory = new G4UIdirectory("/toROOTGPS/generator/");
294  theDirectory->SetGuidance("To modify generator's parameters");
295 
296  resetParametersCmd = new G4UIcmdWithAString("/toROOTGPS/generator/file", this);
297  resetParametersCmd->SetGuidance("To load new parameters of toROOTGPSPrimaryGeneratorAction from a file");
298  resetParametersCmd->SetGuidance("Required parameters: one valid filename (string)");
299  resetParametersCmd->AvailableForStates(G4State_PreInit,G4State_Idle);
300 }
301 
303 {
304  delete theDirectory;
305  delete resetParametersCmd;
306 }
307 
308 void toROOTGPSPrimaryGeneratorActionMessanger::SetNewValue(G4UIcommand* command, G4String newValue)
309 {
310  if( command == resetParametersCmd )
311  theGenerator->GetInputFiles( newValue );
312 }
313 
314 
virtual void GeneratePrimaries(G4Event *anEvent)
toROOTGPSPrimaryGeneratorActionMessanger(toROOTGPSPrimaryGeneratorAction *)
SToGS Base Root Polarized Hit.
Double32_t fPY
toROOTGPSPrimaryGeneratorActionMessanger * theMessanger
Double32_t fPZ
void GetInputFiles(G4String filename="setup/toROOTGPS")
from a given file, it reads the characteristics of the gamma cascade
first step to a general file (ROOT) GPS generator in which events are read from files and/or generata...
Int_t GetNbHits() const
helper function
Messanger class for ParisBasicPrimaryGenerator.
Double32_t fPX
SBRPHit * GetHit(Int_t)
to get a Hit
Double32_t fE