| 1 | <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>8.5. Controlling Visualization from Compiled Code</title><link rel="stylesheet" href="../xml/XSLCustomizationLayer/G4HTMLStylesheet.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.71.1"><link rel="start" href="index.html" title="Geant4 User's Guide for Application Developers"><link rel="up" href="ch08.html" title="Chapter 8. Visualization"><link rel="prev" href="ch08s04.html" title="8.4. Controlling Visualization from Commands"><link rel="next" href="ch08s06.html" title="8.6. Visualization Attributes"><script language="JavaScript">
|
|---|
| 2 | function remote_win(fName)
|
|---|
| 3 | {
|
|---|
| 4 | var url = "AllResources/Detector/geometry.src/" + fName;
|
|---|
| 5 | RemoteWin=window.open(url,"","resizable=no,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,copyhistory=0,width=520,height=520")
|
|---|
| 6 | RemoteWin.creator=self
|
|---|
| 7 | }
|
|---|
| 8 | </script></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">8.5.
|
|---|
| 9 | Controlling Visualization from Compiled Code
|
|---|
| 10 | </th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch08s04.html"><img src="AllResources/IconsGIF/prev.gif" alt="Prev"></a> </td><th width="60%" align="center">Chapter 8.
|
|---|
| 11 | Visualization
|
|---|
| 12 | </th><td width="20%" align="right"> <a accesskey="n" href="ch08s06.html"><img src="AllResources/IconsGIF/next.gif" alt="Next"></a></td></tr></table><hr></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="sect.VisCntCmpl"></a>8.5.
|
|---|
| 13 | Controlling Visualization from Compiled Code
|
|---|
| 14 | </h2></div></div></div><p>
|
|---|
| 15 | While a Geant4 simulation is running, visualization can be
|
|---|
| 16 | performed without user intervention. This is accomplished by
|
|---|
| 17 | calling methods of the Visualization Manager from methods of the
|
|---|
| 18 | user action classes (<span class="emphasis"><em>G4UserRunAction</em></span> and
|
|---|
| 19 | <span class="emphasis"><em>G4UserEventAction</em></span>, for example). In this section methods of
|
|---|
| 20 | the class <span class="emphasis"><em>G4VVisManager</em></span>, which is part of the
|
|---|
| 21 | <code class="literal">graphics_reps</code> category, are described and examples of
|
|---|
| 22 | their use are given.
|
|---|
| 23 | </p><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.VisCntCmpl.VsMng"></a>8.5.1.
|
|---|
| 24 | G4VVisManager
|
|---|
| 25 | </h3></div></div></div><p>
|
|---|
| 26 | The Visualization Manager is implemented by classes
|
|---|
| 27 | <span class="emphasis"><em>G4VisManager</em></span> and <span class="emphasis"><em>G4VisExecutive</em></span>.
|
|---|
| 28 | See <a href="ch08s02.html" title="8.2.
|
|---|
| 29 | Adding Visualization to Your Executable
|
|---|
| 30 | ">Section 8.2</a>
|
|---|
| 31 | "<span class="bold"><strong>Making a Visualization Executable</strong></span>". In order
|
|---|
| 32 | that your Geant4 be compilable either with or without the visualization
|
|---|
| 33 | category, you should not use these classes directly in your C++
|
|---|
| 34 | source code, other than in the <code class="literal">main()</code> function. Instead,
|
|---|
| 35 | you should use their abstract base class <span class="emphasis"><em>G4VVisManager</em></span>,
|
|---|
| 36 | defined in the <code class="literal">intercoms</code> category.
|
|---|
| 37 | </p><p>
|
|---|
| 38 | The pointer to the concrete instance of the real Visualization
|
|---|
| 39 | Manager can be obtained as follows:
|
|---|
| 40 |
|
|---|
| 41 | </p><div class="informalexample"><pre class="programlisting">
|
|---|
| 42 | //----- Getting a pointer to the concrete Visualization Manager instance
|
|---|
| 43 | G4VVisManager* pVVisManager = G4VVisManager::GetConcreteInstance();
|
|---|
| 44 | </pre></div><p>
|
|---|
| 45 | </p><p>
|
|---|
| 46 | The method <code class="literal">G4VVisManager::GetConcreteInstance()</code> returns
|
|---|
| 47 | <code class="literal">NULL</code> if Geant4 is not ready for visualization. Thus your
|
|---|
| 48 | C++ source code should be protected as follows:
|
|---|
| 49 |
|
|---|
| 50 | </p><div class="informalexample"><pre class="programlisting">
|
|---|
| 51 | //----- How to protect your C++ source codes in visualization
|
|---|
| 52 | if (pVVisManager) {
|
|---|
| 53 | ....
|
|---|
| 54 | pVVisManager ->Draw (...);
|
|---|
| 55 | ....
|
|---|
| 56 | }
|
|---|
| 57 | </pre></div><p>
|
|---|
| 58 | </p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.VisCntCmpl.DtcCmp"></a>8.5.2.
|
|---|
| 59 | Visualization of detector components
|
|---|
| 60 | </h3></div></div></div><p>
|
|---|
| 61 | If you have already constructed detector components with logical
|
|---|
| 62 | volumes to which visualization attributes are properly assigned,
|
|---|
| 63 | you are almost ready for visualizing detector components. All you
|
|---|
| 64 | have to do is to describe proper visualization commands within your
|
|---|
| 65 | C++ codes, using the <code class="literal">ApplyCommand()</code> method.
|
|---|
| 66 | </p><p>
|
|---|
| 67 | For example, the following is sample C++ source codes to
|
|---|
| 68 | visualize the detector components:
|
|---|
| 69 |
|
|---|
| 70 | </p><div class="informalexample"><pre class="programlisting">
|
|---|
| 71 | //----- C++ source code: How to visualize detector components (2)
|
|---|
| 72 | // ... using visualization commands in source codes
|
|---|
| 73 |
|
|---|
| 74 | G4VVisManager* pVVisManager = G4VVisManager::GetConcreteInstance() ;
|
|---|
| 75 |
|
|---|
| 76 | if(pVVisManager)
|
|---|
| 77 | {
|
|---|
| 78 | ... (camera setting etc) ...
|
|---|
| 79 | G4UImanager::GetUIpointer()->ApplyCommand("/vis/drawVolume");
|
|---|
| 80 | G4UImanager::GetUIpointer()->ApplyCommand("/vis/viewer/flush");
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | //----- end of C++ source code
|
|---|
| 84 |
|
|---|
| 85 | </pre></div><p>
|
|---|
| 86 | </p><p>
|
|---|
| 87 | In the above, you should also describe <code class="literal">/vis/open</code> command
|
|---|
| 88 | somewhere in your C++ codes or execute the command from (G)UI at
|
|---|
| 89 | the executing stage.
|
|---|
| 90 | </p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.VisCntCmpl.Trjct"></a>8.5.3.
|
|---|
| 91 | Visualization of trajectories
|
|---|
| 92 | </h3></div></div></div><p>
|
|---|
| 93 | In order to visualize trajectories, you can use the method <code class="literal">void
|
|---|
| 94 | G4Trajectory::DrawTrajectory()</code> defined in the tracking
|
|---|
| 95 | category. In the implementation of this method, the following
|
|---|
| 96 | drawing method of <span class="emphasis"><em>G4VVisManager</em></span> is used:
|
|---|
| 97 |
|
|---|
| 98 | </p><div class="informalexample"><pre class="programlisting">
|
|---|
| 99 | //----- A drawing method of G4Polyline
|
|---|
| 100 | virtual void G4VVisManager::Draw (const G4Polyline&, ...) ;
|
|---|
| 101 | </pre></div><p>
|
|---|
| 102 | </p><p>
|
|---|
| 103 | The real implementation of this method is described in the class
|
|---|
| 104 | <span class="emphasis"><em>G4VisManager</em></span>.
|
|---|
| 105 | </p><p>
|
|---|
| 106 | At the end of one event, a set of trajectories can be stored as
|
|---|
| 107 | a list of <span class="emphasis"><em>G4Trajectory</em></span> objects. Therefore you can visualize
|
|---|
| 108 | trajectories, for example, at the end of each event, by
|
|---|
| 109 | implementing the method <code class="literal">MyEventAction::EndOfEventAction()</code>
|
|---|
| 110 | as follows:
|
|---|
| 111 |
|
|---|
| 112 | </p><div class="informalexample"><pre class="programlisting">
|
|---|
| 113 | //----- C++ source codes
|
|---|
| 114 | void ExN03EventAction::EndOfEventAction(const G4Event* evt)
|
|---|
| 115 | {
|
|---|
| 116 | .....
|
|---|
| 117 | // extract the trajectories and draw them
|
|---|
| 118 | if (G4VVisManager::GetConcreteInstance())
|
|---|
| 119 | {
|
|---|
| 120 | G4TrajectoryContainer* trajectoryContainer = evt->GetTrajectoryContainer();
|
|---|
| 121 | G4int n_trajectories = 0;
|
|---|
| 122 | if (trajectoryContainer) n_trajectories = trajectoryContainer->entries();
|
|---|
| 123 |
|
|---|
| 124 | for (G4int i=0; i < n_trajectories; i++)
|
|---|
| 125 | { G4Trajectory* trj=(G4Trajectory*)((*(evt->GetTrajectoryContainer()))[i]);
|
|---|
| 126 | if (drawFlag == "all") trj->DrawTrajectory(50);
|
|---|
| 127 | else if ((drawFlag == "charged")&&(trj->GetCharge() != 0.))
|
|---|
| 128 | trj->DrawTrajectory(50);
|
|---|
| 129 | else if ((drawFlag == "neutral")&&(trj->GetCharge() == 0.))
|
|---|
| 130 | trj->DrawTrajectory(50);
|
|---|
| 131 | }
|
|---|
| 132 | }
|
|---|
| 133 | }
|
|---|
| 134 | //----- end of C++ source codes
|
|---|
| 135 | </pre></div><p>
|
|---|
| 136 | </p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.VisCntCmpl.EnhTrjct"></a>8.5.4.
|
|---|
| 137 | Enhanced trajectory drawing
|
|---|
| 138 | </h3></div></div></div><p>
|
|---|
| 139 | It is possible to use the enhanced trajectory drawing functionality
|
|---|
| 140 | in compiled code as well as from commands. Multiple trajectory
|
|---|
| 141 | models can be instantiated, configured and registered with
|
|---|
| 142 | G4VisManager. For details, see the section on
|
|---|
| 143 | <a href="ch08s07.html#sect.VisEnhTrj.CntlCmpl" title="8.7.4.
|
|---|
| 144 | Controlling from Compiled Code
|
|---|
| 145 | ">Section 8.7.4</a>
|
|---|
| 146 | Enhanced Trajectory Drawing.
|
|---|
| 147 | </p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.VisCntCmpl.AttTrjct"></a>8.5.5.
|
|---|
| 148 | HepRep Attributes for Trajectories
|
|---|
| 149 | </h3></div></div></div><p>
|
|---|
| 150 | The HepRep file formats, HepRepFile and HepRepXML, attach various
|
|---|
| 151 | attributes to trajectories such that you can view these attributes,
|
|---|
| 152 | label trajectories by these attributes or make visibility cuts
|
|---|
| 153 | based on these attributes. If you use the default Geant4 trajectory
|
|---|
| 154 | class, from /tracking/src/G4Trajectory.cc, available attributes
|
|---|
| 155 | will be:
|
|---|
| 156 |
|
|---|
| 157 | </p><div class="itemizedlist"><ul type="disc" compact><li><p>
|
|---|
| 158 | Track ID
|
|---|
| 159 | </p></li><li><p>
|
|---|
| 160 | Parent ID
|
|---|
| 161 | </p></li><li><p>
|
|---|
| 162 | Particle Name
|
|---|
| 163 | </p></li><li><p>
|
|---|
| 164 | Charge
|
|---|
| 165 | </p></li><li><p>
|
|---|
| 166 | PDG Encoding
|
|---|
| 167 | </p></li><li><p>
|
|---|
| 168 | Momentum 3-Vector
|
|---|
| 169 | </p></li><li><p>
|
|---|
| 170 | Momentum magnitude
|
|---|
| 171 | </p></li><li><p>
|
|---|
| 172 | Number of points
|
|---|
| 173 | </p></li></ul></div><p>
|
|---|
| 174 | </p><p>
|
|---|
| 175 | You can add additional attributes of your choosing by modifying the
|
|---|
| 176 | relevant part of G4Trajectory (look for the methods GetAttDefs and
|
|---|
| 177 | CreateAttValues). If you are using your own trajectory class, you
|
|---|
| 178 | may want to consider copying these methods from G4Trajectory.
|
|---|
| 179 | </p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.VisCntCmpl.Hits"></a>8.5.6.
|
|---|
| 180 | Visualization of hits
|
|---|
| 181 | </h3></div></div></div><p>
|
|---|
| 182 | Hits are visualized with classes <span class="emphasis"><em>G4Square</em></span> or
|
|---|
| 183 | <span class="emphasis"><em>G4Circle</em></span>, or other user-defined classes inheriting the
|
|---|
| 184 | abstract base class <span class="emphasis"><em>G4VMarker</em></span>. Drawing methods for hits are
|
|---|
| 185 | not supported by default. Instead, ways of their implementation are
|
|---|
| 186 | guided by virtual methods, <code class="literal">G4VHit::Draw()</code> and
|
|---|
| 187 | <code class="literal">G4VHitsCollection::DrawAllHits()</code>, of the abstract base
|
|---|
| 188 | classes <span class="emphasis"><em>G4VHit</em></span> and <span class="emphasis"><em>G4VHitsCollection</em></span>.
|
|---|
| 189 | These methods are defined as empty functions in the <code class="literal">digits+hits</code>
|
|---|
| 190 | category. You can overload these methods, using the following
|
|---|
| 191 | drawing methods of class <span class="emphasis"><em>G4VVisManager</em></span>, in order to
|
|---|
| 192 | visualize hits:
|
|---|
| 193 |
|
|---|
| 194 | </p><div class="informalexample"><pre class="programlisting">
|
|---|
| 195 | //----- Drawing methods of G4Square and G4Circle
|
|---|
| 196 | virtual void G4VVisManager::Draw (const G4Circle&, ...) ;
|
|---|
| 197 | virtual void G4VVisManager::Draw (const G4Square&, ...) ;
|
|---|
| 198 | </pre></div><p>
|
|---|
| 199 | </p><p>
|
|---|
| 200 | The real implementations of these <code class="literal">Draw()</code> methods are
|
|---|
| 201 | described in class <span class="emphasis"><em>G4VisManager</em></span>.
|
|---|
| 202 | </p><p>
|
|---|
| 203 | The overloaded implementation of <code class="literal">G4VHits::Draw()</code> will
|
|---|
| 204 | be held by, for example, class <span class="emphasis"><em>MyTrackerHits</em></span> inheriting
|
|---|
| 205 | <span class="emphasis"><em>G4VHit</em></span> as follows:
|
|---|
| 206 |
|
|---|
| 207 | </p><div class="informalexample"><pre class="programlisting">
|
|---|
| 208 | //----- C++ source codes: An example of giving concrete implementation of
|
|---|
| 209 | // G4VHit::Draw(), using class MyTrackerHit : public G4VHit {...}
|
|---|
| 210 | //
|
|---|
| 211 | void MyTrackerHit::Draw()
|
|---|
| 212 | {
|
|---|
| 213 | G4VVisManager* pVVisManager = G4VVisManager::GetConcreteInstance();
|
|---|
| 214 | if(pVVisManager)
|
|---|
| 215 | {
|
|---|
| 216 | // define a circle in a 3D space
|
|---|
| 217 | G4Circle circle(pos);
|
|---|
| 218 | circle.SetScreenSize(0.3);
|
|---|
| 219 | circle.SetFillStyle(G4Circle::filled);
|
|---|
| 220 |
|
|---|
| 221 | // make the circle red
|
|---|
| 222 | G4Colour colour(1.,0.,0.);
|
|---|
| 223 | G4VisAttributes attribs(colour);
|
|---|
| 224 | circle.SetVisAttributes(attribs);
|
|---|
| 225 |
|
|---|
| 226 | // make a 3D data for visualization
|
|---|
| 227 | pVVisManager->Draw(circle);
|
|---|
| 228 | }
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | //----- end of C++ source codes
|
|---|
| 232 |
|
|---|
| 233 | </pre></div><p>
|
|---|
| 234 | </p><p>
|
|---|
| 235 | The overloaded implementation of
|
|---|
| 236 | <code class="literal">G4VHitsCollection::DrawAllHits()</code> will be held by, for
|
|---|
| 237 | example, class <span class="emphasis"><em>MyTrackerHitsCollection</em></span> inheriting class
|
|---|
| 238 | <span class="emphasis"><em>G4VHitsCollection</em></span> as follows:
|
|---|
| 239 |
|
|---|
| 240 | </p><div class="informalexample"><pre class="programlisting">
|
|---|
| 241 | //----- C++ source codes: An example of giving concrete implementation of
|
|---|
| 242 | // G4VHitsCollection::Draw(),
|
|---|
| 243 | // using class MyTrackerHit : public G4VHitsCollection{...}
|
|---|
| 244 | //
|
|---|
| 245 | void MyTrackerHitsCollection::DrawAllHits()
|
|---|
| 246 | {
|
|---|
| 247 | G4int n_hit = theCollection.entries();
|
|---|
| 248 | for(G4int i=0;i < n_hit;i++)
|
|---|
| 249 | {
|
|---|
| 250 | theCollection[i].Draw();
|
|---|
| 251 | }
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | //----- end of C++ source codes
|
|---|
| 255 |
|
|---|
| 256 | </pre></div><p>
|
|---|
| 257 | </p><p>
|
|---|
| 258 | Thus, you can visualize hits as well as trajectories, for
|
|---|
| 259 | example, at the end of each event by implementing the method
|
|---|
| 260 | <code class="literal">MyEventAction::EndOfEventAction()</code> as follows:
|
|---|
| 261 |
|
|---|
| 262 | </p><div class="informalexample"><pre class="programlisting">
|
|---|
| 263 | void MyEventAction::EndOfEventAction()
|
|---|
| 264 | {
|
|---|
| 265 | const G4Event* evt = fpEventManager->get_const_currentEvent();
|
|---|
| 266 |
|
|---|
| 267 | G4SDManager * SDman = G4SDManager::get_SDMpointer();
|
|---|
| 268 | G4String colNam;
|
|---|
| 269 | G4int trackerCollID = SDman->get_collectionID(colNam="TrackerCollection");
|
|---|
| 270 | G4int calorimeterCollID = SDman->get_collectionID(colNam="CalCollection");
|
|---|
| 271 |
|
|---|
| 272 | G4TrajectoryContainer * trajectoryContainer = evt->get_trajectoryContainer();
|
|---|
| 273 | G4int n_trajectories = 0;
|
|---|
| 274 | if(trajectoryContainer)
|
|---|
| 275 | { n_trajectories = trajectoryContainer->entries(); }
|
|---|
| 276 |
|
|---|
| 277 | G4HCofThisEvent * HCE = evt->get_HCofThisEvent();
|
|---|
| 278 | G4int n_hitCollection = 0;
|
|---|
| 279 | if(HCE)
|
|---|
| 280 | { n_hitCollection = HCE->get_capacity(); }
|
|---|
| 281 |
|
|---|
| 282 | G4VVisManager* pVVisManager = G4VVisManager::GetConcreteInstance();
|
|---|
| 283 |
|
|---|
| 284 | if(pVVisManager)
|
|---|
| 285 | {
|
|---|
| 286 |
|
|---|
| 287 | // Declare begininng of visualization
|
|---|
| 288 | G4UImanager::GetUIpointer()->ApplyCommand("/vis/scene/notifyHandlers");
|
|---|
| 289 |
|
|---|
| 290 | // Draw trajectories
|
|---|
| 291 | for(G4int i=0; i < n_trajectories; i++)
|
|---|
| 292 | {
|
|---|
| 293 | (*(evt->get_trajectoryContainer()))[i]->DrawTrajectory();
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| 296 | // Construct 3D data for hits
|
|---|
| 297 | MyTrackerHitsCollection* THC
|
|---|
| 298 | = (MyTrackerHitsCollection*)(HCE->get_HC(trackerCollID));
|
|---|
| 299 | if(THC) THC->DrawAllHits();
|
|---|
| 300 | MyCalorimeterHitsCollection* CHC
|
|---|
| 301 | = (MyCalorimeterHitsCollection*)(HCE->get_HC(calorimeterCollID));
|
|---|
| 302 | if(CHC) CHC->DrawAllHits();
|
|---|
| 303 |
|
|---|
| 304 | // Declare end of visualization
|
|---|
| 305 | G4UImanager::GetUIpointer()->ApplyCommand("/vis/viewer/update");
|
|---|
| 306 |
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| 311 | //----- end of C++ codes
|
|---|
| 312 |
|
|---|
| 313 | </pre></div><p>
|
|---|
| 314 | </p><p>
|
|---|
| 315 | You can re-visualize a physical volume, where a hit is detected,
|
|---|
| 316 | with a highlight color, in addition to the whole set of detector
|
|---|
| 317 | components. It is done by calling a drawing method of a physical
|
|---|
| 318 | volume directly. The method is:
|
|---|
| 319 |
|
|---|
| 320 | </p><div class="informalexample"><pre class="programlisting">
|
|---|
| 321 |
|
|---|
| 322 | //----- Drawing methods of a physical volume
|
|---|
| 323 | virtual void Draw (const G4VPhysicalVolume&, ...) ;
|
|---|
| 324 |
|
|---|
| 325 | </pre></div><p>
|
|---|
| 326 | </p><p>
|
|---|
| 327 | This method is, for example, called in a method
|
|---|
| 328 | <code class="literal">MyXXXHit::Draw()</code>, describing the visualization of hits
|
|---|
| 329 | with markers. The following is an example for this:
|
|---|
| 330 |
|
|---|
| 331 | </p><div class="informalexample"><pre class="programlisting">
|
|---|
| 332 | //----- C++ source codes: An example of visualizing hits with
|
|---|
| 333 | void MyCalorimeterHit::Draw()
|
|---|
| 334 | {
|
|---|
| 335 | G4VVisManager* pVVisManager = G4VVisManager::GetConcreteInstance();
|
|---|
| 336 | if(pVVisManager)
|
|---|
| 337 | {
|
|---|
| 338 | G4Transform3D trans(rot,pos);
|
|---|
| 339 | G4VisAttributes attribs;
|
|---|
| 340 | G4LogicalVolume* logVol = pPhys->GetLogicalVolume();
|
|---|
| 341 | const G4VisAttributes* pVA = logVol->GetVisAttributes();
|
|---|
| 342 | if(pVA) attribs = *pVA;
|
|---|
| 343 | G4Colour colour(1.,0.,0.);
|
|---|
| 344 | attribs.SetColour(colour);
|
|---|
| 345 | attribs.SetForceSolid(true);
|
|---|
| 346 |
|
|---|
| 347 | //----- Re-visualization of a selected physical volume with red color
|
|---|
| 348 | pVVisManager->Draw(*pPhys,attribs,trans);
|
|---|
| 349 |
|
|---|
| 350 | }
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | //----- end of C++ codes
|
|---|
| 354 |
|
|---|
| 355 | </pre></div><p>
|
|---|
| 356 | </p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.VisCntCmpl.AttHits"></a>8.5.7.
|
|---|
| 357 | HepRep Attributes for Hits
|
|---|
| 358 | </h3></div></div></div><p>
|
|---|
| 359 | The HepRep file formats, HepRepFile and HepRepXML, attach various
|
|---|
| 360 | attributes to hits such that you can view these attributes, label
|
|---|
| 361 | trajectories by these attributes or make visibility cuts based on
|
|---|
| 362 | these attributes. Examples of adding HepRep attributes to hit
|
|---|
| 363 | classes can be found in examples /extended/analysis/A01 and
|
|---|
| 364 | /extended/runAndEvent/RE01.
|
|---|
| 365 | </p><p>
|
|---|
| 366 | For example, in example RE01's class RE01CalorimeterHit.cc,
|
|---|
| 367 | available attributes will be:
|
|---|
| 368 |
|
|---|
| 369 | </p><div class="itemizedlist"><ul type="disc" compact><li><p>
|
|---|
| 370 | Hit Type
|
|---|
| 371 | </p></li><li><p>
|
|---|
| 372 | Track ID
|
|---|
| 373 | </p></li><li><p>
|
|---|
| 374 | Z Cell ID
|
|---|
| 375 | </p></li><li><p>
|
|---|
| 376 | Phi Cell ID
|
|---|
| 377 | </p></li><li><p>
|
|---|
| 378 | Energy Deposited
|
|---|
| 379 | </p></li><li><p>
|
|---|
| 380 | Energy Deposited by Track
|
|---|
| 381 | </p></li><li><p>
|
|---|
| 382 | Position
|
|---|
| 383 | </p></li><li><p>
|
|---|
| 384 | Logical Volume
|
|---|
| 385 | </p></li></ul></div><p>
|
|---|
| 386 | </p><p>
|
|---|
| 387 | You can add additional attributes of your choosing by modifying the
|
|---|
| 388 | relevant part of the hit class (look for the methods GetAttDefs and
|
|---|
| 389 | CreateAttValues).
|
|---|
| 390 | </p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.VisCntCmpl.txt"></a>8.5.8.
|
|---|
| 391 | Visualization of text
|
|---|
| 392 | </h3></div></div></div><p>
|
|---|
| 393 | In Geant4 Visualization, a text, i.e., a character string, is
|
|---|
| 394 | described by class <span class="emphasis"><em>G4Text</em></span> inheriting
|
|---|
| 395 | <span class="emphasis"><em>G4VMarker</em></span> as well as <span class="emphasis"><em>G4Square</em></span>
|
|---|
| 396 | and <span class="emphasis"><em>G4Circle</em></span>. Therefore, the way to
|
|---|
| 397 | visualize text is the same as for hits. The corresponding drawing
|
|---|
| 398 | method of <span class="emphasis"><em>G4VVisManager</em></span> is:
|
|---|
| 399 |
|
|---|
| 400 | </p><div class="informalexample"><pre class="programlisting">
|
|---|
| 401 | //----- Drawing methods of G4Text
|
|---|
| 402 | virtual void G4VVisManager::Draw (const G4Text&, ...);
|
|---|
| 403 |
|
|---|
| 404 | </pre></div><p>
|
|---|
| 405 | </p><p>
|
|---|
| 406 | The real implementation of this method is described in class
|
|---|
| 407 | <span class="emphasis"><em>G4VisManager</em></span>.
|
|---|
| 408 | </p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.VisCntCmpl.PlyTrkStp"></a>8.5.9.
|
|---|
| 409 | Visualization of polylines and tracking steps
|
|---|
| 410 | </h3></div></div></div><p>
|
|---|
| 411 | Polylines, i.e., sets of successive line segments, are described by
|
|---|
| 412 | class <span class="emphasis"><em>G4Polyline</em></span>. For <span class="emphasis"><em>G4Polyline</em></span>,
|
|---|
| 413 | the following drawing method of class <span class="emphasis"><em>G4VVisManager</em></span>
|
|---|
| 414 | is prepared:
|
|---|
| 415 |
|
|---|
| 416 | </p><div class="informalexample"><pre class="programlisting">
|
|---|
| 417 | //----- A drawing method of G4Polyline
|
|---|
| 418 | virtual void G4VVisManager::Draw (const G4Polyline&, ...) ;
|
|---|
| 419 |
|
|---|
| 420 | </pre></div><p>
|
|---|
| 421 | </p><p>
|
|---|
| 422 | The real implementation of this method is described in class
|
|---|
| 423 | <span class="emphasis"><em>G4VisManager</em></span>.
|
|---|
| 424 | </p><p>
|
|---|
| 425 | Using this method, C++ source codes to visualize
|
|---|
| 426 | <span class="emphasis"><em>G4Polyline</em></span> are described as follows:
|
|---|
| 427 |
|
|---|
| 428 | </p><div class="informalexample"><pre class="programlisting">
|
|---|
| 429 | //----- C++ source code: How to visualize a polyline
|
|---|
| 430 | G4VVisManager* pVVisManager = G4VVisManager::GetConcreteInstance();
|
|---|
| 431 |
|
|---|
| 432 | if (pVVisManager) {
|
|---|
| 433 | G4Polyline polyline ;
|
|---|
| 434 |
|
|---|
| 435 | ..... (C++ source codes to set vertex positions, color, etc)
|
|---|
| 436 |
|
|---|
| 437 | pVVisManager -> Draw(polyline);
|
|---|
| 438 |
|
|---|
| 439 | }
|
|---|
| 440 |
|
|---|
| 441 | //----- end of C++ source codes
|
|---|
| 442 |
|
|---|
| 443 | </pre></div><p>
|
|---|
| 444 | </p><p>
|
|---|
| 445 | Tracking steps are able to be visualized based on the above
|
|---|
| 446 | visualization of <span class="emphasis"><em>G4Polyline</em></span>. You can visualize tracking
|
|---|
| 447 | steps at each step automatically by writing a proper implementation
|
|---|
| 448 | of class <span class="emphasis"><em>MySteppingAction</em></span> inheriting
|
|---|
| 449 | <span class="emphasis"><em>G4UserSteppingAction</em></span>, and also with the help of the Run
|
|---|
| 450 | Manager.
|
|---|
| 451 | </p><p>
|
|---|
| 452 | First, you must implement a method,
|
|---|
| 453 | <code class="literal">MySteppingAction::UserSteppingAction()</code>. A typical
|
|---|
| 454 | implementation of this method is as follows:
|
|---|
| 455 |
|
|---|
| 456 | </p><div class="informalexample"><pre class="programlisting">
|
|---|
| 457 | //----- C++ source code: An example of visualizing tracking steps
|
|---|
| 458 | void MySteppingAction::UserSteppingAction()
|
|---|
| 459 | {
|
|---|
| 460 | G4VVisManager* pVVisManager = G4VVisManager::GetConcreteInstance();
|
|---|
| 461 |
|
|---|
| 462 | if (pVVisManager) {
|
|---|
| 463 |
|
|---|
| 464 | //----- Get the Stepping Manager
|
|---|
| 465 | const G4SteppingManager* pSM = GetSteppingManager();
|
|---|
| 466 |
|
|---|
| 467 | //----- Define a line segment
|
|---|
| 468 | G4Polyline polyline;
|
|---|
| 469 | G4double charge = pSM->GetTrack()->GetDefinition()->GetPDGCharge();
|
|---|
| 470 | G4Colour colour;
|
|---|
| 471 | if (charge < 0.) colour = G4Colour(1., 0., 0.);
|
|---|
| 472 | else if (charge < 0.) colour = G4Colour(0., 0., 1.);
|
|---|
| 473 | else colour = G4Colour(0., 1., 0.);
|
|---|
| 474 | G4VisAttributes attribs(colour);
|
|---|
| 475 | polyline.SetVisAttributes(attribs);
|
|---|
| 476 | polyline.push_back(pSM->GetStep()->GetPreStepPoint()->GetPosition());
|
|---|
| 477 | polyline.push_back(pSM->GetStep()->GetPostStepPoint()->GetPosition());
|
|---|
| 478 |
|
|---|
| 479 | //----- Call a drawing method for G4Polyline
|
|---|
| 480 | pVVisManager -> Draw(polyline);
|
|---|
| 481 |
|
|---|
| 482 | }
|
|---|
| 483 | }
|
|---|
| 484 |
|
|---|
| 485 | //----- end of C++ source code
|
|---|
| 486 |
|
|---|
| 487 | </pre></div><p>
|
|---|
| 488 | </p><p>
|
|---|
| 489 | Next, in order that the above C++ source code works, you have to
|
|---|
| 490 | pass the information of the <span class="emphasis"><em>MySteppingAction</em></span> to the Run
|
|---|
| 491 | Manager in the <code class="literal">main()</code> function:
|
|---|
| 492 |
|
|---|
| 493 | </p><div class="informalexample"><pre class="programlisting">
|
|---|
| 494 |
|
|---|
| 495 | //----- C++ source code: Passing what to do at each step to the Run Manager
|
|---|
| 496 |
|
|---|
| 497 | int main()
|
|---|
| 498 | {
|
|---|
| 499 | ...
|
|---|
| 500 |
|
|---|
| 501 | // Run Manager
|
|---|
| 502 | G4RunManager * runManager = new G4RunManager;
|
|---|
| 503 |
|
|---|
| 504 | // User initialization classes
|
|---|
| 505 | ...
|
|---|
| 506 | runManager->SetUserAction(new MySteppingAction);
|
|---|
| 507 | ...
|
|---|
| 508 | }
|
|---|
| 509 |
|
|---|
| 510 | //----- end of C++ source code
|
|---|
| 511 |
|
|---|
| 512 | </pre></div><p>
|
|---|
| 513 | </p><p>
|
|---|
| 514 | Thus you can visualize tracking steps with various visualization
|
|---|
| 515 | attributes, e.g., color, at each step, automatically.
|
|---|
| 516 | </p><p>
|
|---|
| 517 | As well as tracking steps, you can visualize any kind 3D object
|
|---|
| 518 | made of line segments, using class <span class="emphasis"><em>G4Polyline</em></span> and its
|
|---|
| 519 | drawing method, defined in class <span class="emphasis"><em>G4VVisManager</em></span>. See, for
|
|---|
| 520 | example, the implementation of the <code class="literal">/vis/scene/add/axes</code>
|
|---|
| 521 | command.
|
|---|
| 522 | </p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.VisCntCmpl.UsrAct"></a>8.5.10.
|
|---|
| 523 | Visualization User Action
|
|---|
| 524 | </h3></div></div></div><p>
|
|---|
| 525 | You can implement the <code class="literal">Draw</code> method of
|
|---|
| 526 | <code class="literal">G4VUserVisAction</code>, e.g., the class definition could be:
|
|---|
| 527 |
|
|---|
| 528 | </p><div class="informalexample"><pre class="programlisting">
|
|---|
| 529 | class StandaloneVisAction: public G4VUserVisAction {
|
|---|
| 530 | void Draw();
|
|---|
| 531 | };
|
|---|
| 532 | </pre></div><p>
|
|---|
| 533 |
|
|---|
| 534 | and the implementation:
|
|---|
| 535 |
|
|---|
| 536 | </p><div class="informalexample"><pre class="programlisting">
|
|---|
| 537 | void StandaloneVisAction::Draw() {
|
|---|
| 538 | G4VVisManager* pVisManager = G4VVisManager::GetConcreteInstance();
|
|---|
| 539 | if (pVisManager) {
|
|---|
| 540 |
|
|---|
| 541 | // Simple box...
|
|---|
| 542 | pVisManager->Draw(G4Box("box",2*m,2*m,2*m),
|
|---|
| 543 | G4VisAttributes(G4Colour(1,1,0)));
|
|---|
| 544 |
|
|---|
| 545 | // Boolean solid...
|
|---|
| 546 | G4Box boxA("boxA",3*m,3*m,3*m);
|
|---|
| 547 | G4Box boxB("boxB",1*m,1*m,1*m);
|
|---|
| 548 | G4SubtractionSolid subtracted("subtracted_boxes",&boxA,&boxB,
|
|---|
| 549 | G4Translate3D(3*m,3*m,3*m));
|
|---|
| 550 | pVisManager->Draw(subtracted,
|
|---|
| 551 | G4VisAttributes(G4Colour(0,1,1)),
|
|---|
| 552 | G4Translate3D(6*m,6*m,6*m));
|
|---|
| 553 | }
|
|---|
| 554 | }
|
|---|
| 555 | </pre></div><p>
|
|---|
| 556 | </p><p>
|
|---|
| 557 | Explicit use of polyhedron objects is equivalent, e.g.:
|
|---|
| 558 |
|
|---|
| 559 | </p><div class="informalexample"><pre class="programlisting">
|
|---|
| 560 |
|
|---|
| 561 | // Same, but explicit polyhedron...
|
|---|
| 562 | G4Polyhedron* pA = G4Box("boxA",3*m,3*m,3*m).CreatePolyhedron();
|
|---|
| 563 | G4Polyhedron* pB = G4Box("boxB",1*m,1*m,1*m).CreatePolyhedron();
|
|---|
| 564 | pB->Transform(G4Translate3D(3*m,3*m,3*m));
|
|---|
| 565 | G4Polyhedron* pSubtracted = new G4Polyhedron(pA->subtract(*pB));
|
|---|
| 566 | G4VisAttributes subVisAtts(G4Colour(0,1,1));
|
|---|
| 567 | pSubtracted->SetVisAttributes(&subVisAtts);
|
|---|
| 568 | pVisManager->Draw(*pSubtracted,G4Translate3D(6*m,6*m,6*m));
|
|---|
| 569 | delete pA;
|
|---|
| 570 | delete pB;
|
|---|
| 571 | delete pSubtracted;
|
|---|
| 572 | </pre></div><p>
|
|---|
| 573 | </p><p>
|
|---|
| 574 | If efficiency is an issue, create the objects in the constructor,
|
|---|
| 575 | delete them in the destructor and draw them in your <code class="literal">Draw</code>
|
|---|
| 576 | method. Anyway, an instance of your class needs to be registered
|
|---|
| 577 | with the vis manager, e.g.:
|
|---|
| 578 |
|
|---|
| 579 | </p><div class="informalexample"><pre class="programlisting">
|
|---|
| 580 | ...
|
|---|
| 581 | G4VisManager* visManager = new G4VisExecutive;
|
|---|
| 582 | visManager->Initialize ();
|
|---|
| 583 |
|
|---|
| 584 | visManager->SetUserAction
|
|---|
| 585 | (new StandaloneVisAction,
|
|---|
| 586 | G4VisExtent(-5*m,5*m,-5*m,5*m,-5*m,5*m)); // 2nd argument optional.
|
|---|
| 587 | ...
|
|---|
| 588 | </pre></div><p>
|
|---|
| 589 |
|
|---|
| 590 | then activate by adding to a scene, e.g:
|
|---|
| 591 |
|
|---|
| 592 | </p><div class="informalexample"><pre class="programlisting">
|
|---|
| 593 | /control/verbose 2
|
|---|
| 594 | /vis/verbose c
|
|---|
| 595 | /vis/open OGLSXm
|
|---|
| 596 | /vis/scene/create
|
|---|
| 597 | #/vis/scene/add/userAction
|
|---|
| 598 | /vis/scene/add/userAction -10 10 -10 10 -10 10 m
|
|---|
| 599 | #/vis/scene/add/axes 0 0 0 10 m
|
|---|
| 600 | #/vis/scene/add/scale 10 m
|
|---|
| 601 | /vis/sceneHandler/attach
|
|---|
| 602 | /vis/viewer/refresh
|
|---|
| 603 | </pre></div><p>
|
|---|
| 604 | </p><p>
|
|---|
| 605 | The extent can be added on registration or on the command line or
|
|---|
| 606 | neither (if the extent of the scene is set by other components).
|
|---|
| 607 | Your <code class="literal">Draw</code> method will be called whenever needed to refresh
|
|---|
| 608 | the screen or rebuild a graphics database, for any chosen viewer.
|
|---|
| 609 | The scene can be attached to any scene handler and your drawing
|
|---|
| 610 | will be shown.
|
|---|
| 611 | </p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sect.VisCntCmpl.StdAln"></a>8.5.11.
|
|---|
| 612 | Standalone Visualization
|
|---|
| 613 | </h3></div></div></div><p>
|
|---|
| 614 | The above raises the possibility of using Geant4 as a "standalone"
|
|---|
| 615 | graphics package without invoking the run manager. The following
|
|---|
| 616 | main program, together with a user visualization action and a macro
|
|---|
| 617 | file, will allow you to view your drawing interactively on any of
|
|---|
| 618 | the supported graphics systems.
|
|---|
| 619 |
|
|---|
| 620 | </p><div class="informalexample"><pre class="programlisting">
|
|---|
| 621 | #include "globals.hh"
|
|---|
| 622 | #include "G4VisExecutive.hh"
|
|---|
| 623 | #include "G4VisExtent.hh"
|
|---|
| 624 | #include "G4UImanager.hh"
|
|---|
| 625 | #include "G4UIterminal.hh"
|
|---|
| 626 | #include "G4UItcsh.hh"
|
|---|
| 627 |
|
|---|
| 628 | #include "StandaloneVisAction.hh"
|
|---|
| 629 |
|
|---|
| 630 | int main() {
|
|---|
| 631 |
|
|---|
| 632 | G4VisManager* visManager = new G4VisExecutive;
|
|---|
| 633 | visManager->Initialize ();
|
|---|
| 634 |
|
|---|
| 635 | visManager->SetUserAction
|
|---|
| 636 | (new StandaloneVisAction,
|
|---|
| 637 | G4VisExtent(-5*m,5*m,-5*m,5*m,-5*m,5*m)); // 2nd argument optional.
|
|---|
| 638 |
|
|---|
| 639 | G4UImanager* UI = G4UImanager::GetUIpointer ();
|
|---|
| 640 | UI->ApplyCommand ("/control/execute standalone.g4m");
|
|---|
| 641 |
|
|---|
| 642 | G4UIsession* session = new G4UIterminal(new G4UItcsh);
|
|---|
| 643 | session->SessionStart();
|
|---|
| 644 |
|
|---|
| 645 | delete session;
|
|---|
| 646 | delete visManager;
|
|---|
| 647 | }
|
|---|
| 648 | </pre></div><p>
|
|---|
| 649 | </p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch08s04.html"><img src="AllResources/IconsGIF/prev.gif" alt="Prev"></a> </td><td width="20%" align="center"><a accesskey="u" href="ch08.html"><img src="AllResources/IconsGIF/up.gif" alt="Up"></a></td><td width="40%" align="right"> <a accesskey="n" href="ch08s06.html"><img src="AllResources/IconsGIF/next.gif" alt="Next"></a></td></tr><tr><td width="40%" align="left" valign="top">8.4.
|
|---|
| 650 | Controlling Visualization from Commands
|
|---|
| 651 | </td><td width="20%" align="center"><a accesskey="h" href="index.html"><img src="AllResources/IconsGIF/home.gif" alt="Home"></a></td><td width="40%" align="right" valign="top"> 8.6.
|
|---|
| 652 | Visualization Attributes
|
|---|
| 653 | </td></tr></table></div></body></html>
|
|---|