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