Logo ROOT  
Reference Guide
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 
19 Global magnetic field manager. Provides access to
20 and owns the actual magnetic field set via `SetField()`. The field is deleted
21 upon destruction of the field manager at the end of ROOT session or
22 by calling:
23 
24 ~~~ {.cpp}
25 TGeoGlobalMagField::Instance()->SetField(0).
26 ~~~
27 
28 The previous global field is deleted upon replacement with notification.
29 
30 The global field manager provides access to the global field via:
31 
32 ~~~ {.cpp}
33  TGeoGlobalMagField::Instance()->GetField()
34 ~~~
35 
36 One 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 }
TGeoGlobalMagField::Instance
static TGeoGlobalMagField * Instance()
Returns always a valid static pointer to the field manager.
Definition: TGeoGlobalMagField.cxx:116
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TGeoGlobalMagField::Lock
void Lock()
Locks the global magnetic field if this is set. Cannot be unlocked.
Definition: TGeoGlobalMagField.cxx:125
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TGeoGlobalMagField::TGeoGlobalMagField
TGeoGlobalMagField()
Global field default constructor.
Definition: TGeoGlobalMagField.cxx:50
TObject::Info
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition: TObject.cxx:864
TObject::Error
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:890
TObject::Fatal
virtual void Fatal(const char *method, const char *msgfmt,...) const
Issue fatal error message.
Definition: TObject.cxx:918
TList.h
TVirtualMagField
Abstract class for magnetic field.
Definition: TVirtualMagField.h:17
TROOT.h
TGeoGlobalMagField.h
TGeoGlobalMagField::GetInstance
static TGeoGlobalMagField * GetInstance()
Static getter that does not create the object.
Definition: TGeoGlobalMagField.cxx:108
TGeoGlobalMagField::fLock
Bool_t fLock
Definition: TGeoGlobalMagField.h:23
TGeoGlobalMagField
Global magnetic field manager.
Definition: TGeoGlobalMagField.h:19
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TObject::Warning
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:876
TGeoGlobalMagField::~TGeoGlobalMagField
virtual ~TGeoGlobalMagField()
Global field destructor.
Definition: TGeoGlobalMagField.cxx:72
TGeoGlobalMagField::fgInstance
static TGeoGlobalMagField * fgInstance
Definition: TGeoGlobalMagField.h:21
TGeoGlobalMagField::SetField
void SetField(TVirtualMagField *field)
Field setter. Deletes previous field if any. Acts only if fLock=kFALSE.
Definition: TGeoGlobalMagField.cxx:86
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
TGeoGlobalMagField::GetField
TVirtualMagField * GetField() const
Definition: TGeoGlobalMagField.h:35
gROOT
#define gROOT
Definition: TROOT.h:406
TGeoGlobalMagField::fField
TVirtualMagField * fField
Definition: TGeoGlobalMagField.h:22