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$ 26 // 27 // 27 // Jane Tinslay August 2006 28 // Jane Tinslay August 2006 28 // 29 // 29 #include "G4TrajectoryDrawByAttribute.hh" 30 #include "G4TrajectoryDrawByAttribute.hh" 30 #include "G4AttDef.hh" 31 #include "G4AttDef.hh" 31 #include "G4AttFilterUtils.hh" 32 #include "G4AttFilterUtils.hh" 32 #include "G4AttUtils.hh" 33 #include "G4AttUtils.hh" 33 #include "G4AttValue.hh" 34 #include "G4AttValue.hh" 34 #include "G4TrajectoryDrawerUtils.hh" 35 #include "G4TrajectoryDrawerUtils.hh" 35 #include "G4VAttValueFilter.hh" 36 #include "G4VAttValueFilter.hh" 36 #include "G4VisTrajContext.hh" 37 #include "G4VisTrajContext.hh" 37 #include "G4VTrajectory.hh" 38 #include "G4VTrajectory.hh" 38 #include <assert.h> 39 #include <assert.h> 39 40 40 G4TrajectoryDrawByAttribute::G4TrajectoryDrawB 41 G4TrajectoryDrawByAttribute::G4TrajectoryDrawByAttribute(const G4String& name, G4VisTrajContext* context) 41 :G4VTrajectoryModel(name, context) 42 :G4VTrajectoryModel(name, context) 42 ,fAttName("") 43 ,fAttName("") 43 ,fFirst(true) 44 ,fFirst(true) 44 ,fWarnedMissingAttribute(false) 45 ,fWarnedMissingAttribute(false) 45 ,filter(0) 46 ,filter(0) 46 {} 47 {} 47 48 48 G4TrajectoryDrawByAttribute::~G4TrajectoryDraw 49 G4TrajectoryDrawByAttribute::~G4TrajectoryDrawByAttribute() 49 { 50 { 50 ContextMap::iterator iter = fContextMap.begi 51 ContextMap::iterator iter = fContextMap.begin(); 51 52 52 while (iter != fContextMap.end()) { 53 while (iter != fContextMap.end()) { 53 delete iter->second; 54 delete iter->second; 54 iter++; 55 iter++; 55 } 56 } 56 57 57 delete filter; 58 delete filter; 58 } 59 } 59 60 60 void 61 void >> 62 G4TrajectoryDrawByAttribute::Draw(const G4VTrajectory& object, >> 63 const G4int&, >> 64 const G4bool& visible) const >> 65 { >> 66 Draw(object, visible); >> 67 } >> 68 >> 69 void 61 G4TrajectoryDrawByAttribute::Draw(const G4VTra 70 G4TrajectoryDrawByAttribute::Draw(const G4VTrajectory& object, 62 const G4bool& /*visible*/) const << 71 const G4bool& visible) const 63 { 72 { 64 // Return if attribute name has not been set 73 // Return if attribute name has not been set. Just print one warning 65 if (fAttName.empty()) { << 74 if (fAttName.isNull()) { 66 75 67 if (!fWarnedMissingAttribute) { 76 if (!fWarnedMissingAttribute) { 68 G4ExceptionDescription ed; 77 G4ExceptionDescription ed; 69 ed<<"Null attribute name"; 78 ed<<"Null attribute name"; 70 G4Exception("G4TrajectoryDrawByAttribute 79 G4Exception("G4TrajectoryDrawByAttribute::Draw", 71 "modeling0116", 80 "modeling0116", 72 JustWarning, ed); 81 JustWarning, ed); 73 fWarnedMissingAttribute = true; 82 fWarnedMissingAttribute = true; 74 } 83 } 75 84 76 return; 85 return; 77 } 86 } 78 87 79 // Basically cache data loaded filter for ef 88 // Basically cache data loaded filter for efficiency 80 if (fFirst) { 89 if (fFirst) { 81 90 82 fFirst = false; 91 fFirst = false; 83 92 84 // Get attribute definition 93 // Get attribute definition 85 G4AttDef attDef; 94 G4AttDef attDef; 86 95 87 // Expect definition to exist 96 // Expect definition to exist 88 if (!G4AttUtils::ExtractAttDef(object, fAt 97 if (!G4AttUtils::ExtractAttDef(object, fAttName, attDef)) { 89 static G4bool warnedUnableToExtract = fa 98 static G4bool warnedUnableToExtract = false; 90 if (!warnedUnableToExtract) { 99 if (!warnedUnableToExtract) { 91 G4ExceptionDescription ed; 100 G4ExceptionDescription ed; 92 ed <<"Unable to extract attribute definition << 101 ed <<"Unable to extract attribute definition named "<<fAttName; 93 << "Available attributes:\n" << 94 << *object.GetAttDefs(); << 95 G4Exception 102 G4Exception 96 ("G4TrajectoryDrawByAttribute::Draw", 103 ("G4TrajectoryDrawByAttribute::Draw", 97 "modeling0117", JustWarning, ed, ". Inval << 104 "modeling0117", JustWarning, ed, "Invalid attribute name"); >> 105 G4cout << "Available attributes:\n" >> 106 << object.GetAttDefs(); 98 warnedUnableToExtract = true; 107 warnedUnableToExtract = true; 99 } 108 } 100 return; 109 return; 101 } 110 } 102 111 103 // Get new G4AttValue filter 112 // Get new G4AttValue filter 104 filter = G4AttFilterUtils::GetNewFilter(at 113 filter = G4AttFilterUtils::GetNewFilter(attDef); 105 assert (0 != filter); 114 assert (0 != filter); 106 115 107 // Load both interval and single valued da 116 // Load both interval and single valued data. Single valued data should 108 // override interval data. 117 // override interval data. 109 ContextMap::const_iterator iter = fContext 118 ContextMap::const_iterator iter = fContextMap.begin(); 110 119 111 while (iter != fContextMap.end()) { 120 while (iter != fContextMap.end()) { 112 if (iter->first.second == G4TrajectoryDr 121 if (iter->first.second == G4TrajectoryDrawByAttribute::Interval) { 113 filter->LoadIntervalElement(iter->first.firs 122 filter->LoadIntervalElement(iter->first.first); 114 } 123 } 115 else if (iter->first.second == G4Traject 124 else if (iter->first.second == G4TrajectoryDrawByAttribute::SingleValue) { 116 filter->LoadSingleValueElement(iter->first.f 125 filter->LoadSingleValueElement(iter->first.first); 117 } 126 } 118 iter++; 127 iter++; 119 } 128 } 120 } 129 } 121 130 122 // Get attribute value 131 // Get attribute value 123 G4AttValue attVal; 132 G4AttValue attVal; 124 133 125 // Expect value to exist 134 // Expect value to exist 126 if (!G4AttUtils::ExtractAttValue(object, fAt 135 if (!G4AttUtils::ExtractAttValue(object, fAttName, attVal)) { 127 static G4bool warnedUnableToExtract = fals 136 static G4bool warnedUnableToExtract = false; 128 if (!warnedUnableToExtract) { 137 if (!warnedUnableToExtract) { 129 G4ExceptionDescription ed; 138 G4ExceptionDescription ed; 130 ed <<"Unable to extract attribute defini << 139 ed <<"Unable to extract attribute value named "<<fAttName; 131 << "Available attributes:\n" << 132 << *object.GetAttDefs(); << 133 G4Exception 140 G4Exception 134 ("G4TrajectoryDrawByAttribute::Draw", 141 ("G4TrajectoryDrawByAttribute::Draw", 135 "modeling0118", JustWarning, ed, ". Invalid << 142 "modeling0118", JustWarning, ed, "Invalid attribute name"); >> 143 G4cout << "Available attributes:\n" >> 144 << object.GetAttDefs(); 136 warnedUnableToExtract = true; 145 warnedUnableToExtract = true; 137 } 146 } 138 return; 147 return; 139 } 148 } 140 149 141 G4VisTrajContext myContext(GetContext()); 150 G4VisTrajContext myContext(GetContext()); 142 G4String key; 151 G4String key; 143 152 144 // If attribute value passes filter, get cor 153 // If attribute value passes filter, get corresponding interval/single value 145 // key loaded into G4AttValue filter. 154 // key loaded into G4AttValue filter. 146 if (filter->GetValidElement(attVal, key)) { 155 if (filter->GetValidElement(attVal, key)) { 147 156 148 // Extract context corresponding to valid 157 // Extract context corresponding to valid key. 149 // Single value match should have override 158 // Single value match should have overriden interval match. 150 ContextMap::const_iterator iter = fContext 159 ContextMap::const_iterator iter = fContextMap.begin(); 151 160 152 G4bool gotContext(false); 161 G4bool gotContext(false); 153 162 154 while (!gotContext && (iter != fContextMap 163 while (!gotContext && (iter != fContextMap.end())) { 155 if (iter->first.first == key) { 164 if (iter->first.first == key) { 156 myContext = *(iter->second); 165 myContext = *(iter->second); 157 gotContext = true; 166 gotContext = true; 158 } 167 } 159 iter++; 168 iter++; 160 } 169 } 161 170 162 assert (gotContext); 171 assert (gotContext); 163 } 172 } >> 173 >> 174 myContext.SetVisible(visible); 164 175 165 if (GetVerbose()) { 176 if (GetVerbose()) { 166 G4cout<<"G4TrajectoryDrawByAttribute drawe 177 G4cout<<"G4TrajectoryDrawByAttribute drawer named "<<Name(); 167 G4cout<<", drawing style selected accordin 178 G4cout<<", drawing style selected according to value of attribute "<<fAttName; 168 G4cout<<" : "<<attVal.GetValue()<<". Sele 179 G4cout<<" : "<<attVal.GetValue()<<". Selected context:"<<G4endl; 169 myContext.Print(G4cout); 180 myContext.Print(G4cout); 170 } 181 } 171 182 172 // Draw the trajectory 183 // Draw the trajectory 173 G4TrajectoryDrawerUtils::DrawLineAndPoints(o 184 G4TrajectoryDrawerUtils::DrawLineAndPoints(object, myContext); 174 } 185 } 175 186 176 void 187 void 177 G4TrajectoryDrawByAttribute::Print(std::ostrea 188 G4TrajectoryDrawByAttribute::Print(std::ostream& ostr) const 178 { 189 { 179 ostr<<"G4TrajectoryDrawByAttribute, dumping 190 ostr<<"G4TrajectoryDrawByAttribute, dumping configuration for model named "<< Name() <<":"<<std::endl;; 180 191 181 ostr<<"Default configuration:"<<G4endl; 192 ostr<<"Default configuration:"<<G4endl; 182 GetContext().Print(ostr); 193 GetContext().Print(ostr); 183 194 184 ostr<<"\nAttribute name "<<fAttName<<std::en 195 ostr<<"\nAttribute name "<<fAttName<<std::endl; 185 ostr<<"\nKey<->Context map dump:"<<std::endl 196 ostr<<"\nKey<->Context map dump:"<<std::endl; 186 197 187 ContextMap::const_iterator iter = fContextMa 198 ContextMap::const_iterator iter = fContextMap.begin(); 188 199 189 while (iter != fContextMap.end()) { 200 while (iter != fContextMap.end()) { 190 ostr<<"Context for key "<<iter->first.firs 201 ostr<<"Context for key "<<iter->first.first<<":"<<std::endl; 191 iter->second->Print(ostr); 202 iter->second->Print(ostr); 192 203 193 iter++; 204 iter++; 194 } 205 } 195 } 206 } 196 207 197 void 208 void 198 G4TrajectoryDrawByAttribute::Set(const G4Strin 209 G4TrajectoryDrawByAttribute::Set(const G4String& name) 199 { 210 { 200 fAttName = name; 211 fAttName = name; 201 } 212 } 202 213 203 void 214 void 204 G4TrajectoryDrawByAttribute::AddIntervalContex 215 G4TrajectoryDrawByAttribute::AddIntervalContext(const G4String& name, G4VisTrajContext* context) 205 { 216 { 206 // Takes ownership of context 217 // Takes ownership of context 207 std::pair<G4String, Config> myPair(name, G4T 218 std::pair<G4String, Config> myPair(name, G4TrajectoryDrawByAttribute::Interval); 208 219 209 ContextMap::iterator iter = fContextMap.find 220 ContextMap::iterator iter = fContextMap.find(myPair); 210 221 211 if (iter != fContextMap.end()) { 222 if (iter != fContextMap.end()) { 212 G4ExceptionDescription ed; 223 G4ExceptionDescription ed; 213 ed <<"Interval "<< name <<" already exists 224 ed <<"Interval "<< name <<" already exists"; 214 G4Exception 225 G4Exception 215 ("G4TrajectoryDrawByAttribute::AddInterv 226 ("G4TrajectoryDrawByAttribute::AddIntervalContext", 216 "modeling0119", FatalErrorInArgument, e << 227 "modeling0119", FatalErrorInArgument, ed, "Invalid interval"); 217 } 228 } 218 229 219 fContextMap[myPair] = context; 230 fContextMap[myPair] = context; 220 } 231 } 221 232 222 void 233 void 223 G4TrajectoryDrawByAttribute::AddValueContext(c 234 G4TrajectoryDrawByAttribute::AddValueContext(const G4String& name, G4VisTrajContext* context) 224 { 235 { 225 // Takes ownership of context 236 // Takes ownership of context 226 std::pair<G4String, Config> myPair(name, G4T 237 std::pair<G4String, Config> myPair(name, G4TrajectoryDrawByAttribute::SingleValue); 227 238 228 ContextMap::iterator iter = fContextMap.find 239 ContextMap::iterator iter = fContextMap.find(myPair); 229 240 230 if (iter != fContextMap.end()) { 241 if (iter != fContextMap.end()) { 231 G4ExceptionDescription ed; 242 G4ExceptionDescription ed; 232 ed <<"Single value "<< name <<" already ex 243 ed <<"Single value "<< name <<" already exists"; 233 G4Exception 244 G4Exception 234 ("G4TrajectoryDrawByAttribute::AddSingle 245 ("G4TrajectoryDrawByAttribute::AddSingleValueContext", 235 "modeling0120", FatalErrorInArgument, e << 246 "modeling0120", FatalErrorInArgument, ed, "Invalid value"); 236 } 247 } 237 248 238 fContextMap[myPair] = context; 249 fContextMap[myPair] = context; 239 } 250 } 240 251