Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // >> 26 // $Id: G4TrajectoryDrawByAttribute.cc,v 1.5 2010/05/28 02:00:59 allison Exp $ >> 27 // GEANT4 tag $Name: geant4-09-04-beta-01 $ 26 // 28 // 27 // Jane Tinslay August 2006 29 // Jane Tinslay August 2006 28 // 30 // 29 #include "G4TrajectoryDrawByAttribute.hh" 31 #include "G4TrajectoryDrawByAttribute.hh" 30 #include "G4AttDef.hh" 32 #include "G4AttDef.hh" 31 #include "G4AttFilterUtils.hh" 33 #include "G4AttFilterUtils.hh" 32 #include "G4AttUtils.hh" 34 #include "G4AttUtils.hh" 33 #include "G4AttValue.hh" 35 #include "G4AttValue.hh" 34 #include "G4TrajectoryDrawerUtils.hh" 36 #include "G4TrajectoryDrawerUtils.hh" 35 #include "G4VAttValueFilter.hh" 37 #include "G4VAttValueFilter.hh" 36 #include "G4VisTrajContext.hh" 38 #include "G4VisTrajContext.hh" 37 #include "G4VTrajectory.hh" 39 #include "G4VTrajectory.hh" 38 #include <assert.h> 40 #include <assert.h> >> 41 #include <sstream> 39 42 40 G4TrajectoryDrawByAttribute::G4TrajectoryDrawB 43 G4TrajectoryDrawByAttribute::G4TrajectoryDrawByAttribute(const G4String& name, G4VisTrajContext* context) 41 :G4VTrajectoryModel(name, context) 44 :G4VTrajectoryModel(name, context) 42 ,fAttName("") 45 ,fAttName("") 43 ,fFirst(true) 46 ,fFirst(true) 44 ,fWarnedMissingAttribute(false) 47 ,fWarnedMissingAttribute(false) 45 ,filter(0) 48 ,filter(0) 46 {} 49 {} 47 50 48 G4TrajectoryDrawByAttribute::~G4TrajectoryDraw 51 G4TrajectoryDrawByAttribute::~G4TrajectoryDrawByAttribute() 49 { 52 { 50 ContextMap::iterator iter = fContextMap.begi 53 ContextMap::iterator iter = fContextMap.begin(); 51 54 52 while (iter != fContextMap.end()) { 55 while (iter != fContextMap.end()) { 53 delete iter->second; 56 delete iter->second; 54 iter++; 57 iter++; 55 } 58 } 56 59 57 delete filter; 60 delete filter; 58 } 61 } 59 62 60 void 63 void >> 64 G4TrajectoryDrawByAttribute::Draw(const G4VTrajectory& object, >> 65 const G4int&, >> 66 const G4bool& visible) const >> 67 { >> 68 Draw(object, visible); >> 69 } >> 70 >> 71 void 61 G4TrajectoryDrawByAttribute::Draw(const G4VTra 72 G4TrajectoryDrawByAttribute::Draw(const G4VTrajectory& object, 62 const G4bool& /*visible*/) const << 73 const G4bool& visible) const 63 { 74 { 64 // Return if attribute name has not been set 75 // Return if attribute name has not been set. Just print one warning 65 if (fAttName.empty()) { << 76 if (fAttName.isNull()) { 66 77 67 if (!fWarnedMissingAttribute) { 78 if (!fWarnedMissingAttribute) { 68 G4ExceptionDescription ed; << 79 std::ostringstream o; 69 ed<<"Null attribute name"; << 80 o<<"Null attribute name"; 70 G4Exception("G4TrajectoryDrawByAttribute << 81 G4Exception("G4TrajectoryDrawByAttribute::Draw", "NullAttributeName", JustWarning, o.str().c_str()); 71 "modeling0116", << 82 72 JustWarning, ed); << 73 fWarnedMissingAttribute = true; 83 fWarnedMissingAttribute = true; 74 } 84 } 75 85 76 return; 86 return; 77 } 87 } 78 88 79 // Basically cache data loaded filter for ef 89 // Basically cache data loaded filter for efficiency 80 if (fFirst) { 90 if (fFirst) { 81 91 82 fFirst = false; 92 fFirst = false; 83 93 84 // Get attribute definition 94 // Get attribute definition 85 G4AttDef attDef; 95 G4AttDef attDef; 86 96 87 // Expect definition to exist 97 // Expect definition to exist 88 if (!G4AttUtils::ExtractAttDef(object, fAt 98 if (!G4AttUtils::ExtractAttDef(object, fAttName, attDef)) { 89 static G4bool warnedUnableToExtract = fa << 99 std::ostringstream o; 90 if (!warnedUnableToExtract) { << 100 o <<"Unable to extract attribute definition named "<<fAttName; 91 G4ExceptionDescription ed; << 101 G4Exception 92 ed <<"Unable to extract attribute definition << 102 ("G4TrajectoryDrawByAttribute::Draw", "InvalidAttributeDefinition", FatalErrorInArgument, o.str().c_str()); 93 << "Available attributes:\n" << 94 << *object.GetAttDefs(); << 95 G4Exception << 96 ("G4TrajectoryDrawByAttribute::Draw", << 97 "modeling0117", JustWarning, ed, ". Inval << 98 warnedUnableToExtract = true; << 99 } << 100 return; << 101 } 103 } 102 104 103 // Get new G4AttValue filter 105 // Get new G4AttValue filter 104 filter = G4AttFilterUtils::GetNewFilter(at 106 filter = G4AttFilterUtils::GetNewFilter(attDef); 105 assert (0 != filter); 107 assert (0 != filter); 106 108 107 // Load both interval and single valued da 109 // Load both interval and single valued data. Single valued data should 108 // override interval data. 110 // override interval data. 109 ContextMap::const_iterator iter = fContext 111 ContextMap::const_iterator iter = fContextMap.begin(); 110 112 111 while (iter != fContextMap.end()) { 113 while (iter != fContextMap.end()) { 112 if (iter->first.second == G4TrajectoryDr 114 if (iter->first.second == G4TrajectoryDrawByAttribute::Interval) { 113 filter->LoadIntervalElement(iter->first.firs 115 filter->LoadIntervalElement(iter->first.first); 114 } 116 } 115 else if (iter->first.second == G4Traject 117 else if (iter->first.second == G4TrajectoryDrawByAttribute::SingleValue) { 116 filter->LoadSingleValueElement(iter->first.f 118 filter->LoadSingleValueElement(iter->first.first); 117 } 119 } 118 iter++; 120 iter++; 119 } 121 } 120 } 122 } 121 123 122 // Get attribute value 124 // Get attribute value 123 G4AttValue attVal; 125 G4AttValue attVal; 124 126 125 // Expect value to exist 127 // Expect value to exist 126 if (!G4AttUtils::ExtractAttValue(object, fAt 128 if (!G4AttUtils::ExtractAttValue(object, fAttName, attVal)) { 127 static G4bool warnedUnableToExtract = fals << 129 std::ostringstream o; 128 if (!warnedUnableToExtract) { << 130 o <<"Unable to extract attribute value named "<<fAttName; 129 G4ExceptionDescription ed; << 130 ed <<"Unable to extract attribute defini << 131 << "Available attributes:\n" << 132 << *object.GetAttDefs(); << 133 G4Exception 131 G4Exception 134 ("G4TrajectoryDrawByAttribute::Draw", << 132 ("G4TrajectoryDrawByAttribute::Draw", "InvalidAttributeValue", FatalErrorInArgument, o.str().c_str()); 135 "modeling0118", JustWarning, ed, ". Invalid << 136 warnedUnableToExtract = true; << 137 } << 138 return; << 139 } 133 } 140 134 141 G4VisTrajContext myContext(GetContext()); 135 G4VisTrajContext myContext(GetContext()); 142 G4String key; 136 G4String key; 143 137 144 // If attribute value passes filter, get cor 138 // If attribute value passes filter, get corresponding interval/single value 145 // key loaded into G4AttValue filter. 139 // key loaded into G4AttValue filter. 146 if (filter->GetValidElement(attVal, key)) { 140 if (filter->GetValidElement(attVal, key)) { 147 141 148 // Extract context corresponding to valid 142 // Extract context corresponding to valid key. 149 // Single value match should have override 143 // Single value match should have overriden interval match. 150 ContextMap::const_iterator iter = fContext 144 ContextMap::const_iterator iter = fContextMap.begin(); 151 145 152 G4bool gotContext(false); 146 G4bool gotContext(false); 153 147 154 while (!gotContext && (iter != fContextMap 148 while (!gotContext && (iter != fContextMap.end())) { 155 if (iter->first.first == key) { 149 if (iter->first.first == key) { 156 myContext = *(iter->second); 150 myContext = *(iter->second); 157 gotContext = true; 151 gotContext = true; 158 } 152 } 159 iter++; 153 iter++; 160 } 154 } 161 155 162 assert (gotContext); 156 assert (gotContext); 163 } 157 } >> 158 >> 159 myContext.SetVisible(visible); 164 160 165 if (GetVerbose()) { 161 if (GetVerbose()) { 166 G4cout<<"G4TrajectoryDrawByAttribute drawe 162 G4cout<<"G4TrajectoryDrawByAttribute drawer named "<<Name(); 167 G4cout<<", drawing style selected accordin 163 G4cout<<", drawing style selected according to value of attribute "<<fAttName; 168 G4cout<<" : "<<attVal.GetValue()<<". Sele 164 G4cout<<" : "<<attVal.GetValue()<<". Selected context:"<<G4endl; 169 myContext.Print(G4cout); 165 myContext.Print(G4cout); 170 } 166 } 171 167 172 // Draw the trajectory 168 // Draw the trajectory 173 G4TrajectoryDrawerUtils::DrawLineAndPoints(o 169 G4TrajectoryDrawerUtils::DrawLineAndPoints(object, myContext); 174 } 170 } 175 171 176 void 172 void 177 G4TrajectoryDrawByAttribute::Print(std::ostrea 173 G4TrajectoryDrawByAttribute::Print(std::ostream& ostr) const 178 { 174 { 179 ostr<<"G4TrajectoryDrawByAttribute, dumping 175 ostr<<"G4TrajectoryDrawByAttribute, dumping configuration for model named "<< Name() <<":"<<std::endl;; 180 176 181 ostr<<"Default configuration:"<<G4endl; 177 ostr<<"Default configuration:"<<G4endl; 182 GetContext().Print(ostr); 178 GetContext().Print(ostr); 183 179 184 ostr<<"\nAttribute name "<<fAttName<<std::en 180 ostr<<"\nAttribute name "<<fAttName<<std::endl; 185 ostr<<"\nKey<->Context map dump:"<<std::endl 181 ostr<<"\nKey<->Context map dump:"<<std::endl; 186 182 187 ContextMap::const_iterator iter = fContextMa 183 ContextMap::const_iterator iter = fContextMap.begin(); 188 184 189 while (iter != fContextMap.end()) { 185 while (iter != fContextMap.end()) { 190 ostr<<"Context for key "<<iter->first.firs 186 ostr<<"Context for key "<<iter->first.first<<":"<<std::endl; 191 iter->second->Print(ostr); 187 iter->second->Print(ostr); 192 188 193 iter++; 189 iter++; 194 } 190 } 195 } 191 } 196 192 197 void 193 void 198 G4TrajectoryDrawByAttribute::Set(const G4Strin 194 G4TrajectoryDrawByAttribute::Set(const G4String& name) 199 { 195 { 200 fAttName = name; 196 fAttName = name; 201 } 197 } 202 198 203 void 199 void 204 G4TrajectoryDrawByAttribute::AddIntervalContex 200 G4TrajectoryDrawByAttribute::AddIntervalContext(const G4String& name, G4VisTrajContext* context) 205 { 201 { 206 // Takes ownership of context 202 // Takes ownership of context 207 std::pair<G4String, Config> myPair(name, G4T 203 std::pair<G4String, Config> myPair(name, G4TrajectoryDrawByAttribute::Interval); 208 204 209 ContextMap::iterator iter = fContextMap.find 205 ContextMap::iterator iter = fContextMap.find(myPair); 210 206 211 if (iter != fContextMap.end()) { 207 if (iter != fContextMap.end()) { 212 G4ExceptionDescription ed; << 208 std::ostringstream o; 213 ed <<"Interval "<< name <<" already exists << 209 o <<"Interval "<< name <<" already exists"<<std::endl; 214 G4Exception 210 G4Exception 215 ("G4TrajectoryDrawByAttribute::AddInterv << 211 ("G4TrajectoryDrawByAttribute::AddIntervalContext", "InvalidInterval", FatalErrorInArgument, o.str().c_str()); 216 "modeling0119", FatalErrorInArgument, e << 217 } 212 } 218 213 219 fContextMap[myPair] = context; 214 fContextMap[myPair] = context; 220 } 215 } 221 216 222 void 217 void 223 G4TrajectoryDrawByAttribute::AddValueContext(c 218 G4TrajectoryDrawByAttribute::AddValueContext(const G4String& name, G4VisTrajContext* context) 224 { 219 { 225 // Takes ownership of context 220 // Takes ownership of context 226 std::pair<G4String, Config> myPair(name, G4T 221 std::pair<G4String, Config> myPair(name, G4TrajectoryDrawByAttribute::SingleValue); 227 222 228 ContextMap::iterator iter = fContextMap.find 223 ContextMap::iterator iter = fContextMap.find(myPair); 229 224 230 if (iter != fContextMap.end()) { 225 if (iter != fContextMap.end()) { 231 G4ExceptionDescription ed; << 226 std::ostringstream o; 232 ed <<"Single value "<< name <<" already ex << 227 o <<"Single value "<< name <<" already exists"<<std::endl; 233 G4Exception 228 G4Exception 234 ("G4TrajectoryDrawByAttribute::AddSingle << 229 ("G4TrajectoryDrawByAttribute::AddSingleValueContext", "InvalidInterval", FatalErrorInArgument, o.str().c_str()); 235 "modeling0120", FatalErrorInArgument, e << 236 } 230 } 237 231 238 fContextMap[myPair] = context; 232 fContextMap[myPair] = context; 239 } 233 } 240 234