I want To shoot muons from top of this kind of detector

I want to shoot muons from top of this type of detector , but only in the x-y plane . So I have the following doubts

  1. Firstly it I put the particle gun outside on top of the detector and press /run/BeamOn , the window just closes. Means the dimensions of my detector is (10., 6., 8.,) and if I place the gun at lets say (0., 8.,0.) the window just closes but if I put it at (0., 7., 0.,) it works.
  2. Secondly, here is my code I don’t understand how to inject muons from top only in x-y plane in random angle in each event.

I have seen some related questions here on the forum but unfortunately I am naïve and need some help. Here is my code:

#include “generator.hh”
#include “Randomize.hh”
#include “G4RandomDirection.hh”
fParticleGun = new G4ParticleGun();

delete fParticleGun;


void MyPrimaryGenerator::GeneratePrimaries(G4Event *anEvent)
G4ParticleTable *particleTable = G4ParticleTable::GetParticleTable();
G4String particleName = “muon”;
G4ParticleDefinition *particle = particleTable->FindParticle(“mu-”);
G4ThreeVector pos(0.m, .8m, 0.*m);
G4double cosTheta = .5;
G4ThreeVector v = G4RandomDirection(cosTheta);
v.set(v.y(), v.z(), v.x());

Any sort of help would be highly appreciated. Thank you in advance !

Is that detector block your whole world volume? Or is it a volume smaller than your world placed within it? For obvious reasons, if you try to specify a starting point outside the world volume, G4 is simply going to skip it.

Second, what do you mean by “top” vs. “(x,y) plane”? In your drawing, it seems like you have Y pointing upward, so the top face of the detector would be the (x,z) plane.

For cosmic ray muons, I don’t think you want to generate everything from a point with random directions. Cosmic rays are essentially vertical from any reference location, with an angular dependence that falls of like 1/cos^2(theta).

To generate just downward going, the most direct method is to integrate that function (and normalize is) to get the CDF, then invert the CDF so you can throw a G4UniformRand() and get theta as output (this is just C++ math, not Geant4 specific). Once you have theta, make sure it’s downward by reassigning it as pi-theta (theta==0 is along +z, upward). Then pick a uniformly random phi = twopi*G4UniformRand(). Now you can create the three vector direction using polar coordinates:

   G4ThreeVector v; v.setRPhiTheta(1., phi, theta);

Thank you for your reply! Oh okay so that’s the mistake, the block is my world volume and sorry for my mistake yes I want to shoot on the (x,z) plane. So if I want to shoot muons on the x,z plane how can I implement that into my code?
Sorry, I am actually a beginner at this, could you please tell me what is CDF and what does inverting the CDF mean ?
Thanks again!

First, I would recommend changing your orientation. In Geant4, the Z axis is upward, and the Z axis is also the reference axis for polar (theta) angles; if you do things that way you won’t have to apply extra rotations

Second, you cannot shoot from outside the world volume. Typically, in Geant4 you make the world volume somewhat larger than your detector, and then place your detector within that.

Third, CDF stands for cumulative distribution function. It’s the integral of the PDF (probability density function).

okay sir ! Sure, I made a world volume larger than the detector and also I have changed the orientation(z axis upwards), now if I want to shoot muons from top, how do I do it?

What I described earlier, but with this orientation, it will be easier. For simple on-surface or near-surface detectors, you can assume the muon angular distribution goes like 1/cos^2(zenith), which is the angle with respect to straight upward. That is your PDF (probability density function).

If you want to generate a random value from the PDF, then one method is to integrate it to get a CDF, which you normalize so the PDF total integral is 1. Invert the function, and throw a _uniform random value. When you plug that uniform random into the inverted CDF, you get out a random zenith angle with the desired distribution.

Since that zenith angle is oriented upward, and you wan your muons pointed downward (toward your detector), you use theta = -zenith, then generate a direction around that as I described previously.

Okay please correct me if I am wrong like after integrating and normalizing we would get something like (1/(tan(x) + C)) * (1/cos^2(x)) where x is the angle and C is the proportionality constant and if we invert it we would get f^(-1)(x) = arctan(1/(x * cos^2(y)) - C)