ROOT  6.06/09
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 "TROOT.h"
12 #include "TGeoGlobalMagField.h"
13 
14 //______________________________________________________________________________
15 //
16 // TGeoGlobalMagField - Global magnetic field manager. Provides access to
17 // and owns the actual magnetic field set via SetField(). The field is deleted
18 // upon destruction of the field manager at the end of ROOT session or
19 // by calling: TGeoGlobalMagField::Instance()->SetField(0). The previous
20 // global field is deleted upon replacement with notification.
21 //
22 // The global field manager provides access to the global field via:
23 // TGeoGlobalMagField::Instance()->GetField()
24 // One can directly call the Field() method of a field via the global field manager:
25 //
26 // TGeoGlobalMagField::Instance()->Field(x,B)
27 //
28 //______________________________________________________________________________
29 
30 ClassImp(TGeoGlobalMagField)
31 
32 TGeoGlobalMagField *TGeoGlobalMagField::fgInstance = NULL;
33 
34 ////////////////////////////////////////////////////////////////////////////////
35 /// Global field default constructor.
36 
37 TGeoGlobalMagField::TGeoGlobalMagField()
38 {
39  fField = NULL;
40  fLock = kFALSE;
41  if (fgInstance) {
42  TVirtualMagField *field = fgInstance->GetField();
43  if (field)
44  Fatal("TGeoGlobalMagField", "A global field manager already existing and containing a field. \
45  \n If you want a new global field please set it via: \
46  \n TGeoGlobalMagField::Instance()->SetField(myField).");
47  else
48  Warning("TGeoGlobalMagField", "A global field manager already existing. Please access via: \
49  \n TGeoGlobalMagField::Instance().");
50  delete fgInstance;
51  }
52  gROOT->GetListOfGeometries()->Add(this); // list of cleanups not deleted
53  fgInstance = this;
54 }
55 
56 ////////////////////////////////////////////////////////////////////////////////
57 /// Global field destructor.
58 
60 {
61  gROOT->GetListOfGeometries()->Remove(this);
62  if (fField) {
63  TVirtualMagField *field = fField;
64  fField = NULL;
65  delete field;
66  }
67  fgInstance = NULL;
68 }
69 
70 ////////////////////////////////////////////////////////////////////////////////
71 /// Field setter. Deletes previous field if any. Acts only if fLock=kFALSE.
72 
74 {
75  if (field==fField) return;
76  // Check if we are allowed to change the old field.
77  if (fField) {
78  if (fLock) {
79  Error("SetField", "Global field is already set to <%s> and locked", fField->GetName());
80  return;
81  }
82  // We delete the old global field and notify user.
83  Info("SetField", "Previous magnetic field <%s> will be deleted", fField->GetName());
84  TVirtualMagField *oldfield = fField;
85  fField = NULL;
86  delete oldfield;
87  }
88  fField = field;
89  if (fField) Info("SetField", "Global magnetic field set to <%s>", fField->GetName());
90 }
91 
92 ////////////////////////////////////////////////////////////////////////////////
93 /// Static getter that does not create the object.
94 
95 TGeoGlobalMagField *TGeoGlobalMagField::GetInstance()
96 {
97  return fgInstance;
98 }
99 
100 ////////////////////////////////////////////////////////////////////////////////
101 /// Returns always a valid static pointer to the field manager.
102 
103 TGeoGlobalMagField *TGeoGlobalMagField::Instance()
104 {
105  if (fgInstance) return fgInstance;
106  return new TGeoGlobalMagField();
107 }
108 
109 ////////////////////////////////////////////////////////////////////////////////
110 /// Locks the global magnetic field if this is set. Cannot be unlocked.
111 
113 {
114  if (!fField) {
115  Warning("Lock", "Cannot lock global magnetic field since this was not set yet");
116  return;
117  }
118  fLock = kTRUE;
119  Info("Lock", "Global magnetic field <%s> is now locked", fField->GetName());
120 }
void Fatal(const char *location, const char *msgfmt,...)
static TGeoGlobalMagField * Instance()
Returns always a valid static pointer to the field manager.
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition: TObject.cxx:892
#define gROOT
Definition: TROOT.h:340
const Bool_t kFALSE
Definition: Rtypes.h:92
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
virtual ~TGeoGlobalMagField()
Global field destructor.
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
static TGeoGlobalMagField * fgInstance
void Warning(const char *location, const char *msgfmt,...)
static TGeoGlobalMagField * GetInstance()
Static getter that does not create the object.
#define ClassImp(name)
Definition: Rtypes.h:279
TVirtualMagField * fField
void Lock()
Locks the global magnetic field if this is set. Cannot be unlocked.
#define NULL
Definition: Rtypes.h:82
const Bool_t kTRUE
Definition: Rtypes.h:91
void SetField(TVirtualMagField *field)
Field setter. Deletes previous field if any. Acts only if fLock=kFALSE.
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:904