G4TrajectoryDrawByAttribute.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 // $Id$
00027 //
00028 // Jane Tinslay August 2006
00029 //
00030 #include "G4TrajectoryDrawByAttribute.hh"
00031 #include "G4AttDef.hh"
00032 #include "G4AttFilterUtils.hh"
00033 #include "G4AttUtils.hh"
00034 #include "G4AttValue.hh"
00035 #include "G4TrajectoryDrawerUtils.hh"
00036 #include "G4VAttValueFilter.hh"
00037 #include "G4VisTrajContext.hh"
00038 #include "G4VTrajectory.hh"
00039 #include <assert.h>
00040 
00041 G4TrajectoryDrawByAttribute::G4TrajectoryDrawByAttribute(const G4String& name, G4VisTrajContext* context)
00042   :G4VTrajectoryModel(name, context)
00043   ,fAttName("")
00044   ,fFirst(true)
00045   ,fWarnedMissingAttribute(false)
00046   ,filter(0)
00047 {}
00048 
00049 G4TrajectoryDrawByAttribute::~G4TrajectoryDrawByAttribute() 
00050 {
00051   ContextMap::iterator iter = fContextMap.begin();
00052   
00053   while (iter != fContextMap.end()) {
00054     delete iter->second;
00055     iter++;
00056   }
00057   
00058   delete filter;
00059 }
00060 
00061 void
00062 G4TrajectoryDrawByAttribute::Draw(const G4VTrajectory& object,
00063                                   const G4int&,
00064                                   const G4bool& visible) const
00065 {
00066   Draw(object, visible);
00067 }
00068 
00069 void
00070 G4TrajectoryDrawByAttribute::Draw(const G4VTrajectory& object, 
00071                                   const G4bool& visible) const
00072 {
00073   // Return if attribute name has not been set. Just print one warning
00074   if (fAttName.isNull()) {
00075 
00076     if (!fWarnedMissingAttribute) {
00077       G4ExceptionDescription ed;
00078       ed<<"Null attribute name";
00079       G4Exception("G4TrajectoryDrawByAttribute::Draw",
00080                   "modeling0116",
00081                   JustWarning, ed);
00082       fWarnedMissingAttribute = true;
00083     }
00084     
00085     return;
00086   }
00087   
00088   // Basically cache data loaded filter for efficiency
00089   if (fFirst) {
00090     
00091     fFirst = false;
00092     
00093     // Get attribute definition
00094     G4AttDef attDef;
00095     
00096     // Expect definition to exist    
00097     if (!G4AttUtils::ExtractAttDef(object, fAttName, attDef)) {
00098       static G4bool warnedUnableToExtract = false;
00099       if (!warnedUnableToExtract) {
00100         G4ExceptionDescription ed;
00101         ed <<"Unable to extract attribute definition named "<<fAttName;
00102         G4Exception
00103           ("G4TrajectoryDrawByAttribute::Draw",
00104            "modeling0117", JustWarning, ed, "Invalid attribute name");
00105         G4cout << "Available attributes:\n"
00106                << object.GetAttDefs();
00107         warnedUnableToExtract = true;
00108       }
00109       return;
00110     }
00111     
00112     // Get new G4AttValue filter
00113     filter = G4AttFilterUtils::GetNewFilter(attDef);
00114     assert (0 != filter);
00115     
00116     // Load both interval and single valued data. Single valued data should
00117     // override interval data.
00118     ContextMap::const_iterator iter = fContextMap.begin();
00119     
00120     while (iter != fContextMap.end()) {
00121       if (iter->first.second == G4TrajectoryDrawByAttribute::Interval) {
00122         filter->LoadIntervalElement(iter->first.first);
00123       }
00124       else if (iter->first.second == G4TrajectoryDrawByAttribute::SingleValue) {
00125         filter->LoadSingleValueElement(iter->first.first);
00126       }
00127       iter++;
00128     }
00129   }
00130  
00131   // Get attribute value
00132   G4AttValue attVal;
00133 
00134   // Expect value to exist
00135   if (!G4AttUtils::ExtractAttValue(object, fAttName, attVal)) {
00136     static G4bool warnedUnableToExtract = false;
00137     if (!warnedUnableToExtract) {
00138       G4ExceptionDescription ed;
00139       ed <<"Unable to extract attribute value named "<<fAttName;
00140       G4Exception
00141         ("G4TrajectoryDrawByAttribute::Draw",
00142          "modeling0118", JustWarning, ed, "Invalid attribute name");
00143         G4cout << "Available attributes:\n"
00144                << object.GetAttDefs();
00145       warnedUnableToExtract = true;
00146     }
00147       return;
00148   }
00149   
00150   G4VisTrajContext myContext(GetContext());
00151   G4String key;
00152 
00153   // If attribute value passes filter, get corresponding interval/single value 
00154   // key loaded into G4AttValue filter.
00155   if (filter->GetValidElement(attVal, key)) {
00156 
00157     // Extract context corresponding to valid key.
00158     // Single value match should have overriden interval match.
00159     ContextMap::const_iterator iter = fContextMap.begin();
00160 
00161     G4bool gotContext(false);
00162 
00163     while (!gotContext && (iter != fContextMap.end())) {
00164       if (iter->first.first == key) {
00165         myContext = *(iter->second);
00166         gotContext = true;
00167       }
00168       iter++;
00169     }
00170 
00171     assert (gotContext);
00172   }
00173 
00174   myContext.SetVisible(visible);
00175   
00176   if (GetVerbose()) {
00177     G4cout<<"G4TrajectoryDrawByAttribute drawer named "<<Name();
00178     G4cout<<", drawing style selected according to value of attribute "<<fAttName;
00179     G4cout<<" : "<<attVal.GetValue()<<".  Selected context:"<<G4endl;
00180     myContext.Print(G4cout);
00181   }
00182 
00183   // Draw the trajectory
00184   G4TrajectoryDrawerUtils::DrawLineAndPoints(object, myContext);
00185 }
00186 
00187 void
00188 G4TrajectoryDrawByAttribute::Print(std::ostream& ostr) const
00189 {
00190   ostr<<"G4TrajectoryDrawByAttribute, dumping configuration for model named "<< Name() <<":"<<std::endl;;
00191 
00192   ostr<<"Default configuration:"<<G4endl;
00193   GetContext().Print(ostr);
00194 
00195   ostr<<"\nAttribute name "<<fAttName<<std::endl;
00196   ostr<<"\nKey<->Context map dump:"<<std::endl;
00197 
00198   ContextMap::const_iterator iter = fContextMap.begin();
00199 
00200   while (iter != fContextMap.end()) {
00201     ostr<<"Context for key "<<iter->first.first<<":"<<std::endl;
00202     iter->second->Print(ostr);
00203     
00204     iter++;
00205   }
00206 }
00207 
00208 void
00209 G4TrajectoryDrawByAttribute::Set(const G4String& name)
00210 {
00211   fAttName = name;
00212 }
00213 
00214 void
00215 G4TrajectoryDrawByAttribute::AddIntervalContext(const G4String& name, G4VisTrajContext* context)
00216 {
00217   // Takes ownership of context
00218   std::pair<G4String, Config> myPair(name, G4TrajectoryDrawByAttribute::Interval);
00219 
00220   ContextMap::iterator iter = fContextMap.find(myPair);
00221   
00222   if (iter != fContextMap.end()) {
00223     G4ExceptionDescription ed;
00224     ed <<"Interval "<< name <<" already exists";
00225     G4Exception
00226       ("G4TrajectoryDrawByAttribute::AddIntervalContext",
00227        "modeling0119", FatalErrorInArgument, ed, "Invalid interval");
00228   }
00229 
00230   fContextMap[myPair] = context;
00231 }
00232 
00233 void
00234 G4TrajectoryDrawByAttribute::AddValueContext(const G4String& name, G4VisTrajContext* context)
00235 {
00236   // Takes ownership of context
00237   std::pair<G4String, Config> myPair(name, G4TrajectoryDrawByAttribute::SingleValue);
00238 
00239   ContextMap::iterator iter = fContextMap.find(myPair);
00240   
00241   if (iter != fContextMap.end()) {
00242     G4ExceptionDescription ed;
00243     ed <<"Single value "<< name <<" already exists";
00244     G4Exception
00245       ("G4TrajectoryDrawByAttribute::AddSingleValueContext",
00246        "modeling0120", FatalErrorInArgument, ed, "Invalid value");
00247   }
00248 
00249   fContextMap[myPair] = context;
00250 }

Generated on Mon May 27 17:50:01 2013 for Geant4 by  doxygen 1.4.7