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) return;
89 // Check if we are allowed to change the old field.
90 if (fField) {
91 if (fLock) {
92 Error("SetField", "Global field is already set to <%s> and locked", fField->GetName());
93 return;
94 }
95 // We delete the old global field and notify user.
96 Info("SetField", "Previous magnetic field <%s> will be deleted", fField->GetName());
97 TVirtualMagField *oldfield = fField;
98 fField = nullptr;
99 delete oldfield;
100 }
101 fField = field;
102 if (fField) Info("SetField", "Global magnetic field set to <%s>", fField->GetName());
103}
104
105////////////////////////////////////////////////////////////////////////////////
106/// Static getter that does not create the object.
107
109{
110 return fgInstance;
111}
112
113////////////////////////////////////////////////////////////////////////////////
114/// Returns always a valid static pointer to the field manager.
115
117{
118 if (fgInstance) return fgInstance;
119 return new TGeoGlobalMagField();
120}
121
122////////////////////////////////////////////////////////////////////////////////
123/// Locks the global magnetic field if this is set. Cannot be unlocked.
124
126{
127 if (!fField) {
128 Warning("Lock", "Cannot lock global magnetic field since this was not set yet");
129 return;
130 }
131 fLock = kTRUE;
132 Info("Lock", "Global magnetic field <%s> is now locked", fField->GetName());
133}
const Bool_t kFALSE
Definition RtypesCore.h:101
const Bool_t kTRUE
Definition RtypesCore.h:100
#define ClassImp(name)
Definition Rtypes.h:364
#define gROOT
Definition TROOT.h:404
Global magnetic field manager.
virtual ~TGeoGlobalMagField()
Global field destructor.
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.
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.
virtual const char * GetName() const
Returns name of object.
Definition TNamed.h:47
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:949
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:963
virtual void Fatal(const char *method, const char *msgfmt,...) const
Issue fatal error message.
Definition TObject.cxx:991
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition TObject.cxx:937
Abstract class for magnetic field.