Add a magnetic field

Hello, I’m trying to add a magnetic field to the simulation following the example B5

  1. In the detectorconstruction
    a. in B1DetectorConstruction::B1DetectorConstruction()

I added fMagneticLogical(nullptr)
b. in G4VPhysicalVolume* B1DetectorConstruction::Construct()
I built the volume

 //magnet
	G4LogicalVolume* MagnetlogicEnv;   
	G4Box* MagnetEnv;
	
		MagnetEnv = new G4Box("MagnetEnv", 1.0*m, 1.0*m, 1.0*m);
        MagnetlogicEnv= new G4LogicalVolume(MagnetEnv, vacuum, "Magnet");    
    	new G4PVPlacement(0,    //rotation
       	G4ThreeVector(0,0,5*cm),         //at (0,0,fzFirDummyPos)
       	MagnetlogicEnv,                //its logical volume
       	"Magnet",              //its name                              
       	logicWorld,              //its mother  volume
       	false,                   //no boolean operation
       	0,                       //copy number
       	checkOverlaps);          //overlaps checking

c. I added

void B1DetectorConstruction::ConstructSDandField()
{
  // magnetic field ----------------------------------------------------------
  fMagneticField = new B5MagneticField();
  fFieldMgr = new G4FieldManager();
  fFieldMgr->SetDetectorField(fMagneticField);
  fFieldMgr->CreateChordFinder(fMagneticField);
  G4bool forceToAllDaughters = true;
  fMagneticLogical->SetFieldManager(fFieldMgr, forceToAllDaughters);
}
  1. In DetectorConstruction.hh
    a. In class
 B1DetectorConstruction : public G4VUserDetectorConstruction
{
private

I added

 static G4ThreadLocal B5MagneticField* fMagneticField;
    	static G4ThreadLocal G4FieldManager* fFieldMgr;
      	G4LogicalVolume* fMagneticLogical;

Moreover, I copied in the src directory the B5MagneticField.cc and in the include directory B5MagneticField.hh

but when I compile I get errors

C:\B1\include\B1DetectorConstruction.hh(95,40): error C2143: errore di sintassi: ';' mancante prima di '*' (compilazion
e del file di origine C:\B1\src\B1SteppingAction.cc) [C:\B1\B1-build\exampleB1.vcxproj]
C:\B1\include\B1DetectorConstruction.hh(95,40): error C2143: errore di sintassi: ';' mancante prima di '*' (compilazion
e del file di origine C:\B1\exampleB1.cc) [C:\B1\B1-build\exampleB1.vcxproj]
C:\B1\include\B1DetectorConstruction.hh(95,40): error C4430: identificatore di tipo mancante, verrà utilizzato int. Not
a: default-int non è più supportato in C++ (compilazione del file di origine C:\B1\src\B1SteppingAction.cc) [C:\B1\B1-b
uild\exampleB1.vcxproj]
C:\B1\include\B1DetectorConstruction.hh(95,40): error C4430: identificatore di tipo mancante, verrà utilizzato int. Not
a: default-int non è più supportato in C++ (compilazione del file di origine C:\B1\exampleB1.cc) [C:\B1\B1-build\exampl
eB1.vcxproj]
C:\B1\include\B1DetectorConstruction.hh(95,56): error C2238: token imprevisti prima di ';' (compilazione del file di or
igine C:\B1\src\B1SteppingAction.cc) [C:\B1\B1-build\exampleB1.vcxproj]
C:\B1\include\B1DetectorConstruction.hh(95,56): error C2238: token imprevisti prima di ';' (compilazione del file di or
igine C:\B1\exampleB1.cc) [C:\B1\B1-build\exampleB1.vcxproj]
C:\B1\include\B1DetectorConstruction.hh(96,41): error C2143: errore di sintassi: ';' mancante prima di '*' (compilazion
e del file di origine C:\B1\src\B1SteppingAction.cc) [C:\B1\B1-build\exampleB1.vcxproj]
C:\B1\include\B1DetectorConstruction.hh(96,41): error C2143: errore di sintassi: ';' mancante prima di '*' (compilazion
e del file di origine C:\B1\exampleB1.cc) [C:\B1\B1-build\exampleB1.vcxproj]
C:\B1\include\B1DetectorConstruction.hh(96,41): error C4430: identificatore di tipo mancante, verrà utilizzato int. Not
a: default-int non è più supportato in C++ (compilazione del file di origine C:\B1\src\B1SteppingAction.cc) [C:\B1\B1-b
uild\exampleB1.vcxproj]
C:\B1\include\B1DetectorConstruction.hh(96,41): error C4430: identificatore di tipo mancante, verrà utilizzato int. Not
a: default-int non è più supportato in C++ (compilazione del file di origine C:\B1\exampleB1.cc) [C:\B1\B1-build\exampl
eB1.vcxproj]
C:\B1\include\B1DetectorConstruction.hh(96,52): error C2238: token imprevisti prima di ';' (compilazione del file di or
igine C:\B1\src\B1SteppingAction.cc) [C:\B1\B1-build\exampleB1.vcxproj]
C:\B1\include\B1DetectorConstruction.hh(96,52): error C2238: token imprevisti prima di ';' (compilazione del file di or
igine C:\B1\exampleB1.cc) [C:\B1\B1-build\exampleB1.vcxproj]
C:\B1\include\B1DetectorConstruction.hh(95,40): error C2143: errore di sintassi: ';' mancante prima di '*' (compilazion
e del file di origine C:\B1\src\B1DetectorConstruction.cc) [C:\B1\B1-build\exampleB1.vcxproj]
C:\B1\include\B1DetectorConstruction.hh(95,40): error C4430: identificatore di tipo mancante, verrà utilizzato int. Not
a: default-int non è più supportato in C++ (compilazione del file di origine C:\B1\src\B1DetectorConstruction.cc) [C:\B
1\B1-build\exampleB1.vcxproj]
C:\B1\include\B1DetectorConstruction.hh(95,56): error C2238: token imprevisti prima di ';' (compilazione del file di or
igine C:\B1\src\B1DetectorConstruction.cc) [C:\B1\B1-build\exampleB1.vcxproj]
C:\B1\include\B1DetectorConstruction.hh(96,41): error C2143: errore di sintassi: ';' mancante prima di '*' (compilazion
e del file di origine C:\B1\src\B1DetectorConstruction.cc) [C:\B1\B1-build\exampleB1.vcxproj]
C:\B1\include\B1DetectorConstruction.hh(96,41): error C4430: identificatore di tipo mancante, verrà utilizzato int. Not
a: default-int non è più supportato in C++ (compilazione del file di origine C:\B1\src\B1DetectorConstruction.cc) [C:\B
1\B1-build\exampleB1.vcxproj]
C:\B1\include\B1DetectorConstruction.hh(96,52): error C2238: token imprevisti prima di ';' (compilazione del file di or
igine C:\B1\src\B1DetectorConstruction.cc) [C:\B1\B1-build\exampleB1.vcxproj]
C:\B1\include\B1DetectorConstruction.hh(95,40): error C2143: errore di sintassi: ';' mancante prima di '*' (compilazion
e del file di origine C:\B1\src\B1RunAction.cc) [C:\B1\B1-build\exampleB1.vcxproj]
C:\B1\include\B1DetectorConstruction.hh(95,40): error C4430: identificatore di tipo mancante, verrà utilizzato int. Not
a: default-int non è più supportato in C++ (compilazione del file di origine C:\B1\src\B1RunAction.cc) [C:\B1\B1-build\
exampleB1.vcxproj]
C:\B1\include\B1DetectorConstruction.hh(95,56): error C2238: token imprevisti prima di ';' (compilazione del file di or
igine C:\B1\src\B1RunAction.cc) [C:\B1\B1-build\exampleB1.vcxproj]
C:\B1\include\B1DetectorConstruction.hh(96,41): error C2143: errore di sintassi: ';' mancante prima di '*' (compilazion
e del file di origine C:\B1\src\B1RunAction.cc) [C:\B1\B1-build\exampleB1.vcxproj]
C:\B1\include\B1DetectorConstruction.hh(96,41): error C4430: identificatore di tipo mancante, verrà utilizzato int. Not
a: default-int non è più supportato in C++ (compilazione del file di origine C:\B1\src\B1RunAction.cc) [C:\B1\B1-build\
exampleB1.vcxproj]
C:\B1\include\B1DetectorConstruction.hh(96,52): error C2238: token imprevisti prima di ';' (compilazione del file di or
igine C:\B1\src\B1RunAction.cc) [C:\B1\B1-build\exampleB1.vcxproj]
C:\B1\src\B1DetectorConstruction.cc(57,56): error C2039: 'fMagneticField': non è un membro di 'B1DetectorConstruction'
[C:\B1\B1-build\exampleB1.vcxproj]
C:\B1\include\B1DetectorConstruction.hh(41): message : vedere la dichiarazione di 'B1DetectorConstruction' [C:\B1\B1-bu
ild\exampleB1.vcxproj]
C:\B1\src\B1DetectorConstruction.cc(58,55): error C2039: 'fFieldMgr': non è un membro di 'B1DetectorConstruction' [C:\B
1\B1-build\exampleB1.vcxproj]
C:\B1\include\B1DetectorConstruction.hh(41): message : vedere la dichiarazione di 'B1DetectorConstruction' [C:\B1\B1-bu
ild\exampleB1.vcxproj]
C:\B1\src\B1DetectorConstruction.cc(363,1): error C2509: 'ConstructSDandField': funzione membro non dichiarata in 'B1De
tectorConstruction' [C:\B1\B1-build\exampleB1.vcxproj]
C:\B1\include\B1DetectorConstruction.hh(41): message : vedere la dichiarazione di 'B1DetectorConstruction' [C:\B1\B1-bu
ild\exampleB1.vcxproj]

the line B1DetectorConstruction.hh(95,40): is
static G4ThreadLocal B5MagneticField* fMagneticField;

then I don’t understand what is the problem

B1DetectorConstruction.hh (4.3 KB)
B1DetectorConstruction.cc (15.1 KB)

At the point of the statement

static G4ThreadLocal B5MagneticField* fMagneticField;

the compiler doesn’t know the type B5MagneticField - it hasn’t been declared/defined yet. See for example https://arne-mertz.de/2018/03/forward-declarations/ and Class declaration - cppreference.com for details on the origin of the problem and how to fix.

Hello @bmorgan thank you.
I fixed it and now the simulation compiles…but it dies without runnning

**************************************************************
 Geant4 version Name: geant4-10-07-patch-01 [MT]   (5-February-2021)
  << in Multi-threaded mode >>
                       Copyright : Geant4 Collaboration
                      References : NIM A 506 (2003), 250-303
                                 : IEEE-TNS 53 (2006), 270-278
                                 : NIM A 835 (2016), 186-225
                             WWW : http://geant4.org/
**************************************************************

<<< Reference Physics List QBBC
Using Root
Visualization Manager instantiating with verbosity "warnings (3)"...
Visualization Manager initialising...
Registering graphics systems...

You have successfully registered the following graphics systems.
Registered graphics systems are:
  ASCIITree (ATree)
  DAWNFILE (DAWNFILE)
  G4HepRep (HepRepXML)
  G4HepRepFile (HepRepFile)
  RayTracer (RayTracer)
  VRML1FILE (VRML1FILE)
  VRML2FILE (VRML2FILE)
  gMocrenFile (gMocrenFile)
  OpenGLImmediateQt (OGLIQt, OGLI)
  OpenGLStoredQt (OGLSQt, OGL, OGLS)
  Qt3D (Qt3D)

Registering model factories...

You have successfully registered the following model factories.
Registered model factories:
  generic
  drawByAttribute
  drawByCharge
  drawByOriginVolume
  drawByParticleID
  drawByEncounteredVolume

Registered models:
  None

Registered filter factories:
  attributeFilter
  chargeFilter
  originVolumeFilter
  particleFilter
  encounteredVolumeFilter

Registered filters:
  None

You have successfully registered the following user vis actions.
Run Duration User Vis Actions: none
End of Event User Vis Actions: none
End of Run User Vis Actions: none

Some /vis commands (optionally) take a string to specify colour.
"/vis/list" to see available colours.
Checking overlaps for volume Magnet (G4Box) ... OK!
Checking overlaps for volume Regenerator (G4Tubs) ... OK!
Checking overlaps for volume Target1 (G4Tubs) ... OK!
Checking overlaps for volume Target2 (G4Tubs) ... OK!

C:\B1\B1-build\Release>






There’s very little to go on from that output - try debugging step by step either with breakpoints or good old fashioned print to stdout to find where it gets to before exiting. That will provide hints on where to look in the code to identify the problem.

Hello @bmorgan…I found the problem…but I dont’ understand the reason!

If I use my solid (I built a G4box)

//magnet
/*	G4LogicalVolume* MagnetlogicEnv;   
	G4Box* MagnetEnv;
	
    	MagnetEnv = new G4Box("MagnetEnv", 5*cm, 5*cm, 5*cm);
        MagnetlogicEnv= new G4LogicalVolume(MagnetEnv, vacuum, "Magnet");    
    	new G4PVPlacement(0,    //rotation
       	G4ThreeVector(0,0,20*cm),         //at (0,0,fzFirDummyPos)
       	MagnetlogicEnv,                //its logical volume
       	"Magnet",              //its name                              
       	logicWorld,              //its mother  volume
       	false,                   //no boolean operation
       	0,                       //copy number
       	checkOverlaps);          //overlaps checking*/
       	
 //   G4UserLimits* userLimits = new G4UserLimits(10*m);
  //	fMagneticLogical->SetUserLimits(userLimits);*/

it doesn’t work…if I use the solid of the B5 example (a G4tube)

auto magneticSolid 
    = new G4Tubs("magneticTubs",0.,10.*cm,10.*cm,0.,360.*deg);

  fMagneticLogical
    = new G4LogicalVolume(magneticSolid, vacuum, "magneticLogical");

  // placement of Tube
  
  G4RotationMatrix* fieldRot = new G4RotationMatrix();
  fieldRot->rotateX(90.*deg);
  new G4PVPlacement(fieldRot,G4ThreeVector(0.,0.,1*m),fMagneticLogical,
                    "magneticPhysical",logicWorld,
                    false,0,checkOverlaps);
  
  // set step limit in tube with magnetic field  
  G4UserLimits* userLimits = new G4UserLimits(1*m);
  fMagneticLogical->SetUserLimits(userLimits);

it works!!
But I can’t understand the reason…Moreover…I prefer a G4box…