// // ******************************************************************** // * License and Disclaimer * // * * // * The Geant4 software is copyright of the Copyright Holders of * // * the Geant4 Collaboration. It is provided under the terms and * // * conditions of the Geant4 Software License, included in the file * // * LICENSE and available at http://cern.ch/geant4/license . These * // * include a list of copyright holders. * // * * // * Neither the authors of this software system, nor their employing * // * institutes,nor the agencies providing financial support for this * // * work make any representation or warranty, express or implied, * // * regarding this software system or assume any liability for its * // * use. Please see the license in the file LICENSE and URL above * // * for the full disclaimer and the limitation of liability. * // * * // * This code implementation is the result of the scientific and * // * technical work of the GEANT4 collaboration. * // * By using, copying, modifying or distributing the software (or * // * any work based on the software) you agree to acknowledge its * // * use in resulting scientific publications, and indicate your * // * acceptance of all terms of the Geant4 Software license. * // ******************************************************************** // // // #include #include #include "IFieldManager.hxx" #include #include #include namespace COMET { IFieldManager* IFieldManager::fObject = 0; IFieldManager::IFieldManager(){ fFields = new FieldList(); // set object fObject = this; updateField(); } IFieldManager::~IFieldManager() { clear(); delete fFields; } void IFieldManager::updateField() { fFirst = true; fNumFieldPointers = 0; fFieldPointers = 0; clear(); } IFieldManager* IFieldManager::getObject() { if (!fObject) new IFieldManager(); return fObject; } void IFieldManager::GetFieldValue(const Double_t* point, Double_t* field) const { // NOTE: this routine dominates the CPU time for tracking. // Using the simple array fieldPointers[] instead of fields[] // directly sped it up field[0] = field[1] = field[2] = field[3] = field[4] = field[5] = 0.0; // protect against Geant4 bug that calls us with point[] NaN. if(point[0] != point[0]) return; // (can't use nFieldPointers or fieldPointers, as they may change) if (fFirst) ((IFieldManager*)this)->setupArray(); // (cast away const) for (int i=0; iisInBoundingBox(point)) { p->addFieldValue(point,field); //} } if(0) { COMETLog( "Point = [" << point[0]/unit::cm << ", " << point[1]/unit::cm << ", " << point[2]/unit::cm << "] cm"); COMETLog(" Field = [" << field[0]/unit::tesla << ", " << field[1]/unit::tesla << ", " << field[2]/unit::tesla << "]"); COMETLog(" [" << field[3] << ", " << field[4] << ", " << field[5]<< "] "); } } void IFieldManager::clear() { if (fFields) { if (fFields->size()>0) { FieldList::iterator i; for (i=fFields->begin(); i!=fFields->end(); ++i) delete *i; fFields->clear(); } } if (fFieldPointers) delete[] fFieldPointers; fFirst = true; fNumFieldPointers = 0; fFieldPointers = NULL; } void IFieldManager::setupArray() { fFirst = false; fNumFieldPointers = fFields->size(); fFieldPointers = new const IElementField* [fNumFieldPointers+1]; // add 1 so it's never 0 for (int i=0; i IFieldManager::MakeDescription()const{ IHandle full_description(new IFieldDescription()); if (fFields && fFields->size()>0) { for ( FieldList::const_iterator i_element=fFields->begin(); i_element!=fFields->end(); ++i_element) { full_description->AddElement((*i_element)->MakeDescription()); } } return full_description; } IFieldManager* IFieldManager::Import(const char* filename){ TDirectory* original_directory=TDirectory::CurrentDirectory(); IFieldManager* mgr=NULL; TFile* infile = TFile::Open(filename,"READ"); if(!infile || infile->IsZombie()){ COMETError("Cannot open file "<Close(); } if(original_directory) original_directory->cd(); return mgr; } IFieldManager* IFieldManager::Import(TDirectory* directory){ if(!directory) return NULL; IFieldDescription* description=NULL; TIter next(directory->GetListOfKeys()); TKey *key; while ((key = (TKey*)next())) { if (strcmp(key->GetClassName(),"COMET::IFieldDescription") != 0) continue; description = (COMET::IFieldDescription*)key->ReadObj(); break; } if(!description){ COMETError("TDirectory, '"<GetName()<<"' doesn't contain an instance of IFieldDescription"); return NULL; } IFieldManager* mgr=getObject(); bool success=description->Recreate(); if(!success) { COMETError("Problem recreating field described in '"<GetName()<<"'. See previous messages."); return NULL; } return mgr; } }