Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TGeoGlobalMagField.cxx
Go to the documentation of this file.
1// @(#)root/geom:$Id$
2
3/*************************************************************************
4 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
5 * All rights reserved. *
6 * *
7 * For the licensing terms see $ROOTSYS/LICENSE. *
8 * For the list of contributors see $ROOTSYS/README/CREDITS. *
9 *************************************************************************/
10
11#include "TGeoGlobalMagField.h"
12
13#include "TROOT.h"
14#include "TList.h"
15
16/** \class TGeoGlobalMagField
17\ingroup Geometry_classes
18
19Global magnetic field manager. Provides access to
20and owns the actual magnetic field set via `SetField()`. The field is deleted
21upon destruction of the field manager at the end of ROOT session or
22by calling:
23
24~~~ {.cpp}
25TGeoGlobalMagField::Instance()->SetField(0).
26~~~
27
28The previous global field is deleted upon replacement with notification.
29
30The global field manager provides access to the global field via:
31
32~~~ {.cpp}
33 TGeoGlobalMagField::Instance()->GetField()
34~~~
35
36One can directly call the Field() method of a field via the global field manager:
37
38~~~ {.cpp}
39 TGeoGlobalMagField::Instance()->Field(x,B)
40~~~
41*/
42
44
46
47////////////////////////////////////////////////////////////////////////////////
48/// Global field default constructor.
49
51{
52 fField = nullptr;
53 fLock = kFALSE;
54 if (fgInstance) {
56 if (field)
57 Fatal("TGeoGlobalMagField", "A global field manager already existing and containing a field. \
58 \n If you want a new global field please set it via: \
59 \n TGeoGlobalMagField::Instance()->SetField(myField).");
60 else
61 Warning("TGeoGlobalMagField", "A global field manager already existing. Please access via: \
62 \n TGeoGlobalMagField::Instance().");
63 delete fgInstance;
64 }
65 gROOT->GetListOfGeometries()->Add(this); // list of cleanups not deleted
66 fgInstance = this;
67}
68
69////////////////////////////////////////////////////////////////////////////////
70/// Global field destructor.
71
73{
74 gROOT->GetListOfGeometries()->Remove(this);
75 if (fField) {
76 TVirtualMagField *field = fField;
77 fField = nullptr;
78 delete field;
79 }
80 fgInstance = nullptr;
81}
82
83////////////////////////////////////////////////////////////////////////////////
84/// Field setter. Deletes previous field if any. Acts only if fLock=kFALSE.
85
87{
88 if (field == fField)
89 return;
90 // Check if we are allowed to change the old field.
91 if (fField) {
92 if (fLock) {
93 Error("SetField", "Global field is already set to <%s> and locked", fField->GetName());
94 return;
95 }
96 // We delete the old global field and notify user.
97 Info("SetField", "Previous magnetic field <%s> will be deleted", fField->GetName());
98 TVirtualMagField *oldfield = fField;
99 fField = nullptr;
100 delete oldfield;
101 }
102 fField = field;
103 if (fField)
104 Info("SetField", "Global magnetic field set to <%s>", fField->GetName());
105}
106
107////////////////////////////////////////////////////////////////////////////////
108/// Static getter that does not create the object.
109
111{
112 return fgInstance;
113}
114
115////////////////////////////////////////////////////////////////////////////////
116/// Returns always a valid static pointer to the field manager.
117
119{
120 if (fgInstance)
121 return fgInstance;
122 return new TGeoGlobalMagField();
123}
124
125////////////////////////////////////////////////////////////////////////////////
126/// Locks the global magnetic field if this is set. Cannot be unlocked.
127
129{
130 if (!fField) {
131 Warning("Lock", "Cannot lock global magnetic field since this was not set yet");
132 return;
133 }
134 fLock = kTRUE;
135 Info("Lock", "Global magnetic field <%s> is now locked", fField->GetName());
136}
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
#define ClassImp(name)
Definition Rtypes.h:377
#define gROOT
Definition TROOT.h:406
Global magnetic field manager.
TVirtualMagField * fField
void SetField(TVirtualMagField *field)
Field setter. Deletes previous field if any. Acts only if fLock=kFALSE.
TVirtualMagField * GetField() const
TGeoGlobalMagField()
Global field default constructor.
void Lock()
Locks the global magnetic field if this is set. Cannot be unlocked.
~TGeoGlobalMagField() override
Global field destructor.
static TGeoGlobalMagField * fgInstance
static TGeoGlobalMagField * Instance()
Returns always a valid static pointer to the field manager.
static TGeoGlobalMagField * GetInstance()
Static getter that does not create the object.
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:973
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:987
virtual void Fatal(const char *method, const char *msgfmt,...) const
Issue fatal error message.
Definition TObject.cxx:1015
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition TObject.cxx:961
Abstract class for magnetic field.