TGeoGlobalMagField.cxx

Go to the documentation of this file.
00001 // @(#)root/geom:$Id: TGeoGlobalMagField.cxx 27191 2009-01-20 08:09:20Z brun $
00002 
00003 /*************************************************************************
00004  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00005  * All rights reserved.                                                  *
00006  *                                                                       *
00007  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00008  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00009  *************************************************************************/
00010 
00011 #include "TROOT.h"
00012 #include "TGeoGlobalMagField.h"
00013 
00014 //______________________________________________________________________________
00015 //                                                                        
00016 //    TGeoGlobalMagField - Global magnetic field manager. Provides access to 
00017 // and owns the actual magnetic field set via SetField(). The field is deleted
00018 // upon destruction of the field manager at the end of ROOT session or
00019 // by calling: TGeoGlobalMagField::Instance()->SetField(0). The previous
00020 // global field is deleted upon replacement with notification.
00021 //
00022 // The global field manager provides access to the global field via:
00023 //   TGeoGlobalMagField::Instance()->GetField()
00024 // One can directly call the Field() method of a field via the global field manager:
00025 //
00026 //   TGeoGlobalMagField::Instance()->Field(x,B)
00027 //                                                                     
00028 //______________________________________________________________________________
00029 
00030 ClassImp(TGeoGlobalMagField)
00031 
00032 TGeoGlobalMagField *TGeoGlobalMagField::fgInstance = NULL;
00033 
00034 //______________________________________________________________________________
00035 TGeoGlobalMagField::TGeoGlobalMagField()
00036 {
00037 // Global field default constructor.
00038    fField = NULL;
00039    fLock = kFALSE;
00040    if (fgInstance) {
00041       TVirtualMagField *field = fgInstance->GetField();
00042       if (field)
00043          Fatal("TGeoGlobalMagField", "A global field manager already existing and containing a field. \
00044          \n If you want a new global field please set it via: \
00045          \n   TGeoGlobalMagField::Instance()->SetField(myField).");
00046       else
00047          Warning("TGeoGlobalMagField", "A global field manager already existing. Please access via: \
00048          \n   TGeoGlobalMagField::Instance().");
00049       delete fgInstance;
00050    }   
00051    gROOT->GetListOfGeometries()->Add(this); // list of cleanups not deleted
00052    fgInstance = this;
00053 }
00054 
00055 //______________________________________________________________________________
00056 TGeoGlobalMagField::~TGeoGlobalMagField()
00057 {
00058 // Global field destructor.
00059    gROOT->GetListOfGeometries()->Remove(this);
00060    if (fField) {
00061       TVirtualMagField *field = fField;
00062       fField = NULL;
00063       delete field;
00064    }   
00065    fgInstance = NULL;
00066 }
00067    
00068 //______________________________________________________________________________
00069 void TGeoGlobalMagField::SetField(TVirtualMagField *field)
00070 {
00071 // Field setter. Deletes previous field if any. Acts only if fLock=kFALSE.
00072    if (field==fField) return;
00073    // Check if we are allowed to change the old field.
00074    if (fField) {
00075       if (fLock) {
00076          Error("SetField", "Global field is already set to <%s> and locked", fField->GetName());
00077          return;
00078       }
00079       // We delete the old global field and notify user.   
00080       Info("SetField", "Previous magnetic field <%s> will be deleted", fField->GetName());
00081       TVirtualMagField *oldfield = fField;
00082       fField = NULL;
00083       delete oldfield;
00084    }   
00085    fField = field;
00086    if (fField) Info("SetField", "Global magnetic field set to <%s>", fField->GetName());
00087 }
00088 
00089 //______________________________________________________________________________
00090 TGeoGlobalMagField *TGeoGlobalMagField::Instance()
00091 {
00092 // Returns always a valid static pointer to the field manager.
00093    if (fgInstance) return fgInstance;
00094    return new TGeoGlobalMagField();
00095 }
00096 
00097 //______________________________________________________________________________
00098 void TGeoGlobalMagField::Lock()
00099 {
00100 // Locks the global magnetic field if this is set. Cannot be unlocked.
00101    if (!fField) {
00102       Warning("Lock", "Cannot lock global magnetic field since this was not set yet");
00103       return;
00104    }
00105    fLock = kTRUE;
00106    Info("Lock", "Global magnetic field <%s> is now locked", fField->GetName());
00107 }   

Generated on Tue Jul 5 14:12:55 2011 for ROOT_528-00b_version by  doxygen 1.5.1