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