source: trunk/documents/UserDoc/DocBookUsersGuides/ForApplicationDeveloper/xml/Control/userInterfaceCommand.xml @ 905

Last change on this file since 905 was 904, checked in by garnier, 16 years ago

ajout de la doc

File size: 32.3 KB
Line 
1<!-- ******************************************************** -->
2<!--                                                          -->
3<!--  [History]                                               -->
4<!--    Changed by: Katsuya Amako, 21-Sep-1998                -->
5<!--    Changed by: Katsuya Dosanjh, 15-Jul-2000              -->
6<!--    Changed by: Dennis Wright, 29-Nov-2001                -->
7<!--    Changed by: Jacek Generowicz, 13-Mar-2002             -->
8<!--    Proof read by: Joe Chuma,   2-Jul-1999                -->
9<!--    Converted to DocBook: Katsuya Amako, Aug-2006         -->
10<!--                                                          -->
11<!-- ******************************************************** -->
12
13
14<!-- ******************* Section (Level#1) ****************** -->
15<sect1 id="sect.UIDefNew">
16<title>
17User Interface - Defining New Commands
18</title>
19
20
21<!-- ******************* Section (Level#2) ****************** -->
22<sect2 id="sect.UIDefNew.UIMess">
23<title>
24G4UImessenger
25</title>
26
27<para>
28<emphasis>G4UImessenger</emphasis> is a base class which represents a
29messenger that delivers command(s) to the destination class object. Your
30concrete messenger should have the following functionalities.
31
32<itemizedlist spacing="compact">
33  <listitem><para>
34    Construct your command(s) in the constructor of your messenger.
35  </para></listitem>
36  <listitem><para>
37    Destruct your command(s) in the destructor of your messenger.
38  </para></listitem>
39</itemizedlist>
40</para>
41
42<para>
43These requirements mean that your messenger should keep all
44pointers to your command objects as its data members.
45</para>
46
47<para>
48You can use <emphasis>G4UIcommand</emphasis> derived classes for the most
49frequent types of command. These derived classes have their own
50conversion methods according to their types, and they make
51implementation of the <literal>SetNewValue()</literal> and
52<literal>GetCurrentValue()</literal> methods of your messenger much easier
53and simpler.
54</para>
55
56<para>
57For complicated commands which take various parameters, you can
58use the <emphasis>G4UIcommand</emphasis> base class, and construct
59<emphasis>G4UIparameter</emphasis> objects by yourself. You don't need to delete
60<emphasis>G4UIparameter</emphasis> object(s).
61</para>
62
63<para>
64In the <literal>SetNewValue()</literal> and <literal>GetCurrentValue()</literal>
65methods of your messenger, you can compare the <emphasis>G4UIcommand</emphasis>
66pointer given in the argument of these methods with the pointer of
67your command, because your messenger keeps the pointers to the
68commands. Thus, you don't need to compare by command name. Please
69remember, in the cases where you use <emphasis>G4UIcommand</emphasis> derived
70classes, you should store the pointers with the types of these
71derived classes so that you can use methods defined in the derived
72classes according to their types without casting.
73</para>
74
75<para>
76<emphasis>G4UImanager/G4UIcommand/G4UIparameter</emphasis> have very powerful
77type and range checking routines. You are strongly recommended to
78set the range of your parameters. For the case of a numerical value
79(<literal>int</literal> or <literal>double</literal>), the range can be given by a
80<emphasis>G4String</emphasis> using C++ notation, e.g., <literal>"X &gt; 0 &amp;&amp;
81X &lt; 10"</literal>. For the case of a string type parameter, you can
82set a candidate list. Please refer to the detailed descriptions below.
83</para>
84
85<para>
86<literal>GetCurrentValue()</literal> will be invoked after the user's
87application of the corresponding command, and before the
88<literal>SetNewValue()</literal> invocation. This <literal>GetCurrentValue()</literal>
89method will be invoked only if
90
91<itemizedlist spacing="compact">
92  <listitem><para>
93    at least one parameter of the command has a range
94  </para></listitem>
95  <listitem><para>
96    at least one parameter of the command has a candidate list
97  </para></listitem>
98  <listitem><para>
99    at least the value of one parameter is omitted and this
100    parameter is defined as omittable and
101    <literal>currentValueAsDefault</literal>
102  </para></listitem>
103</itemizedlist>
104</para>
105
106<para>
107For the first two cases, you can re-set the range or the candidate
108list if you need to do so, but these ``re-set'' parameters are
109needed only for the case where the range or the candidate list
110varies dynamically.
111</para>
112
113<para>
114A command can be ``state sensitive'', i.e., the command can be
115accepted only for a certain <emphasis>G4ApplicationState</emphasis>(s). For
116example, the <literal>/run/beamOn</literal> command should not be accepted
117when Geant4 is processing another event (``G4State_EventProc''
118state). You can set the states available for the command with the
119<literal>AvailableForStates()</literal> method.
120</para>
121
122</sect2>
123
124
125<!-- ******************* Section (Level#2) ****************** -->
126<sect2 id="sect.UIDefNew.DervCls">
127<title>
128G4UIcommand and its derived classes
129</title>
130
131<!-- ******* Bridgehead ******* -->
132<bridgehead renderas='sect4'>
133Methods available for all derived classes
134</bridgehead>
135
136<para>
137These are methods defined in the <emphasis>G4UIcommand</emphasis> base class
138which should be used from the derived classes.
139
140<itemizedlist spacing="compact">
141  <listitem><para>
142    <literal>void SetGuidance(char*)</literal>
143    <para>
144    Define a guidance line. You can invoke this method as many times
145    as you need to give enough amount of guidance. Please note that the
146    first line will be used as a title head of the command guidance.
147    </para>
148  </para></listitem>
149  <listitem><para>
150    <literal>void availableForStates(G4ApplicationState s1,...)</literal>
151    <para>
152    If your command is valid only for certain states of the Geant4
153    kernel, specify these states by this method. Currently available
154    states are <literal>G4State_PreInit, G4State_Init, G4State_Idle,
155    G4State_GeomClosed,</literal> and <literal>G4State_EventProc</literal>.
156    Refer to the section 3.4.2 for meaning of each state. Please note that
157    the <literal>Pause</literal> state had been removed from
158    <emphasis>G4ApplicationState</emphasis>.
159    </para>
160  </para></listitem>
161  <listitem><para>
162    <literal>void SetRange(char* range)</literal>
163    <para>
164    Define a range of the parameter(s). Use C++ notation, e.g.,
165    <literal>"x &gt; 0 &amp;&amp; x &lt; 10"</literal>, with variable name(s)
166    defined by the <literal>SetParameterName()</literal> method. For the case of
167    a <emphasis>G4ThreeVector</emphasis>, you can set the relation between
168    parameters, e.g., <literal>"x &gt; y"</literal>.
169    </para>
170  </para></listitem>
171</itemizedlist>
172</para>
173
174
175<!-- ******* Bridgehead ******* -->
176<bridgehead renderas='sect4'>
177G4UIdirectory
178</bridgehead>
179
180<para>
181This is a <emphasis>G4UIcommand</emphasis> derived class for defining a
182directory.
183
184<itemizedlist spacing="compact">
185  <listitem><para>
186    <literal>G4UIdirectory(char* directoryPath)</literal>
187    <para>
188    Constructor. Argument is the (full-path) directory, which must
189    begin and terminate with `<literal>/</literal>'.
190    </para>
191  </para></listitem>
192</itemizedlist>
193</para>
194
195<!-- ******* Bridgehead ******* -->
196<bridgehead renderas='sect4'>
197G4UIcmdWithoutParameter
198</bridgehead>
199
200<para>
201This is a <emphasis>G4UIcommand</emphasis> derived class for a command which
202takes no parameter.
203
204<itemizedlist spacing="compact">
205  <listitem><para>
206    <literal>G4UIcmdWithoutParameter(char* commandPath,
207    G4UImessenger* theMessenger)</literal>
208    <para>
209    Constructor. Arguments are the (full-path) command name and the
210    pointer to your messenger.
211    </para>
212  </para></listitem>
213</itemizedlist>
214</para>
215
216<!-- ******* Bridgehead ******* -->
217<bridgehead renderas='sect4'>
218G4UIcmdWithABool
219</bridgehead>
220
221<para>
222This is a <emphasis>G4UIcommand</emphasis> derived class which takes one
223boolean type parameter.
224
225<itemizedlist spacing="compact">
226  <listitem><para>
227    <literal>G4UIcmdWithABool(char* commandpath,G4UImanager*
228    theMessenger)</literal>
229    <para>
230    Constructor. Arguments are the (full-path) command name and the
231    pointer to your messenger.
232    </para>
233  </para></listitem>
234  <listitem><para>
235    <literal>void SetParameterName(char* paramName,
236    G4bool omittable)</literal>
237    <para>
238    Define the name of the boolean parameter and set the omittable
239    flag. If omittable is true, you should define the default value
240    using the next method.
241    </para>
242  </para></listitem>
243  <listitem><para>
244    <literal>void SetDefaultValue(G4bool defVal)</literal>
245    <para>
246    Define the default value of the boolean parameter.
247    </para>
248  </para></listitem>
249  <listitem><para>
250    <literal>G4bool GetNewBoolValue(G4String paramString)</literal>
251    <para>
252    Convert <emphasis>G4String</emphasis> parameter value given by the
253    <literal>SetNewValue()</literal> method of your messenger into boolean.
254    </para>
255  </para></listitem>
256  <listitem><para>
257    <literal>G4String convertToString(G4bool currVal)</literal>
258    <para>
259    Convert the current boolean value to <emphasis>G4String</emphasis> 
260    whichshould be returned by the <literal>GetCurrentValue()</literal> method
261    of your messenger.
262    </para>
263  </para></listitem>
264</itemizedlist>
265</para>
266
267<!-- ******* Bridgehead ******* -->
268<bridgehead renderas='sect4'>
269G4UIcmdWithAnInteger
270</bridgehead>
271
272<para>
273This is a <emphasis>G4UIcommand</emphasis> derived class which takes one
274integer type parameter.
275
276<itemizedlist spacing="compact">
277  <listitem><para>
278    <literal>G4UIcmdWithAnInteger(char* commandpath,
279    G4UImanager* theMessenger)</literal>
280    <para>
281    Constructor. Arguments are the (full-path) command name and the
282    pointer to your messenger.
283    </para>
284  </para></listitem>
285  <listitem><para>
286    <literal>void SetParameterName(char* paramName,
287    G4bool omittable)</literal>
288    <para>
289    Define the name of the integer parameter and set the omittable
290    flag. If omittable is true, you should define the default value
291    using the next method.
292    </para>
293  </para></listitem>
294  <listitem><para>
295    <literal>void SetDefaultValue(G4int defVal)</literal>
296    <para>
297    Define the default value of the integer parameter.
298    </para>
299  </para></listitem>
300  <listitem><para>
301    <literal>G4int GetNewIntValue(G4String paramString)</literal>
302    <para>
303    Convert <emphasis>G4String</emphasis> parameter value given by the
304    <literal>SetNewValue()</literal> method of your messenger into integer.
305    </para>
306  </para></listitem>
307  <listitem><para>
308    <literal>G4String convertToString(G4int currVal)</literal>
309    <para>
310    Convert the current integer value to <emphasis>G4String</emphasis>, which
311    should be returned by the <literal>GetCurrentValue()</literal> method of your
312    messenger.
313    </para>
314</para></listitem>
315</itemizedlist>
316</para>
317
318<!-- ******* Bridgehead ******* -->
319<bridgehead renderas='sect4'>
320G4UIcmdWithADouble
321</bridgehead>
322
323<para>
324This is a <emphasis>G4UIcommand</emphasis> derived class which takes one
325double type parameter.
326
327<itemizedlist spacing="compact">
328  <listitem><para>
329    <literal>G4UIcmdWithADouble(char* commandpath,
330    G4UImanager* theMessenger)</literal>
331    <para>
332    Constructor. Arguments are the (full-path) command name and the
333    pointer to your messenger.
334    </para>
335  </para></listitem>
336  <listitem><para>
337    <literal>void SetParameterName(char* paramName,
338    G4bool omittable)</literal>
339    <para>
340    Define the name of the double parameter and set the omittable
341    flag. If omittable is true, you should define the default value
342    using the next method.
343    </para>
344  </para></listitem>
345  <listitem><para><literal>void SetDefaultValue(G4double defVal)</literal>
346  <para>
347  Define the default value of the double parameter.
348  </para>
349  </para></listitem>
350  <listitem><para>
351    <literal>G4double GetNewDoubleValue(G4String paramString)</literal>
352    <para>
353    Convert <emphasis>G4String</emphasis> parameter value given by the
354    <literal>SetNewValue()</literal> method of your messenger into double.
355    </para>
356  </para></listitem>
357  <listitem><para>
358    <literal>G4String convertToString(G4double currVal)</literal>
359    <para>
360    Convert the current double value to <emphasis>G4String</emphasis> which
361    should be returned by the <literal>GetCurrentValue()</literal> method of
362    your messenger.
363    </para>
364  </para></listitem>
365</itemizedlist>
366</para>
367
368<!-- ******* Bridgehead ******* -->
369<bridgehead renderas='sect4'>
370G4UIcmdWithAString
371</bridgehead>
372
373<para>
374This is a <emphasis>G4UIcommand</emphasis> derived class which takes one
375string type parameter.
376
377<itemizedlist spacing="compact">
378  <listitem><para>
379    <literal>G4UIcmdWithAString(char* commandpath,
380    G4UImanager* theMessenger)</literal>
381    <para>
382    Constructor. Arguments are the (full-path) command name and the
383    pointer to your messenger.
384    </para>
385  </para></listitem>
386  <listitem><para>
387    <literal>void SetParameterName(char* paramName,
388    G4bool omittable)</literal>
389    <para>
390    Define the name of the string parameter and set the omittable
391    flag. If omittable is true, you should define the default value
392    using the next method.
393    </para>
394  </para></listitem>
395  <listitem><para>
396    <literal>void SetDefaultValue(char* defVal)</literal>
397    <para>
398    Define the default value of the string parameter.
399    </para>
400  </para></listitem>
401  <listitem><para>
402    <literal>void SetCandidates(char* candidateList)</literal>
403    <para>
404    Define a candidate list which can be taken by the parameter.
405    Each candidate listed in this list should be separated by a single
406    space. If this candidate list is given, a string given by the user
407    but which is not listed in this list will be rejected.
408    </para>
409  </para></listitem>
410</itemizedlist>
411</para>
412
413<!-- ******* Bridgehead ******* -->
414<bridgehead renderas='sect4'>
415G4UIcmdWith3Vector
416</bridgehead>
417
418<para>
419This is a <emphasis>G4UIcommand</emphasis> derived class which takes one
420three vector parameter.
421
422<itemizedlist spacing="compact">
423  <listitem><para>
424    <literal>G4UIcmdWith3Vector(char* commandpath,
425    G4UImanager* theMessenger)</literal>
426    <para>
427    Constructor. Arguments are the (full-path) command name and the
428    pointer to your messenger.
429    </para>
430  </para></listitem>
431  <listitem><para>
432    <literal>void SetParameterName(char* paramNamX, char* paramNamY,
433    char* paramNamZ, G4bool omittable)</literal>
434    <para>
435    Define the names of each component of the three vector and set
436    the omittable flag. If omittable is true, you should define the
437    default value using the next method.
438    </para>
439  </para></listitem>
440  <listitem><para>
441    <literal>void SetDefaultValue(G4ThreeVector defVal)</literal>
442    <para>
443    Define the default value of the three vector.
444    </para>
445  </para></listitem>
446  <listitem><para>
447    <literal>G4ThreeVector GetNew3VectorValue(G4String paramString)</literal>
448    <para>
449    Convert the <emphasis>G4String</emphasis> parameter value given by the
450    <literal>SetNewValue()</literal> method of your messenger into a
451    <emphasis>G4ThreeVector</emphasis>.
452    </para>
453  </para></listitem>
454  <listitem><para>
455    <literal>G4String convertToString(G4ThreeVector currVal)</literal>
456    <para>
457    Convert the current three vector to <emphasis>G4String</emphasis>, which
458    should be returned by the <literal>GetCurrentValue()</literal> method of
459    your messenger.
460    </para>
461  </para></listitem>
462</itemizedlist>
463</para>
464
465<!-- ******* Bridgehead ******* -->
466<bridgehead renderas='sect4'>
467G4UIcmdWithADoubleAndUnit
468</bridgehead>
469
470<para>
471This is a <emphasis>G4UIcommand</emphasis> derived class which takes one
472double type parameter and its unit.
473
474<itemizedlist spacing="compact">
475  <listitem><para>
476    <literal>G4UIcmdWithADoubleAndUnit(char* commandpath,
477    G4UImanager* theMessenger)</literal>
478    <para>
479    Constructor. Arguments are the (full-path) command name and the
480    pointer to your messenger.
481    </para>
482  </para></listitem>
483  <listitem><para>
484    <literal>void SetParameterName(char* paramName,
485    G4bool omittable)</literal>
486    <para>
487    Define the name of the double parameter and set the omittable
488    flag. If omittable is true, you should define the default value
489    using the next method.
490    </para>
491  </para></listitem>
492  <listitem><para>
493    <literal>void SetDefaultValue(G4double defVal)</literal>
494    <para>
495    Define the default value of the double parameter.
496    </para>
497</para><para>
498</para></listitem>
499<listitem><para>
500  <literal>void SetUnitCategory(char* unitCategory)</literal>
501  <para>
502  Define acceptable unit category.
503  </para>
504  </para></listitem>
505  <listitem><para>
506    <literal>void SetDefaultUnit(char* defUnit)</literal>
507    <para>
508    Define the default unit. Please use this method and the
509    <literal>SetUnitCategory()</literal> method alternatively.
510    </para>
511  </para></listitem>
512  <listitem><para>
513    <literal>G4double GetNewDoubleValue(G4String paramString)</literal>
514    <para>
515    Convert <emphasis>G4String</emphasis> parameter value given by the
516    <literal>SetNewValue()</literal> method of your messenger into double.
517    Please note that the return value has already been multiplied by the
518    value of the given unit.
519    </para>
520  </para></listitem>
521  <listitem><para><literal>G4double GetNewDoubleRawValue(G4String paramString)</literal>
522  <para>
523  Convert <emphasis>G4String</emphasis> parameter value given by the
524  <literal>SetNewValue()</literal> method of your messenger into double but
525  without multiplying the value of the given unit.
526  </para>
527  </para></listitem>
528  <listitem><para><literal>G4double GetNewUnitValue(G4String paramString)</literal>
529  <para>
530  Convert <emphasis>G4String</emphasis> unit value given by the
531  <literal>SetNewValue()</literal> method of your messenger into double.
532  </para>
533  </para></listitem>
534  <listitem><para>
535    <literal>G4String convertToString(G4bool currVal,
536    char* unitName)</literal>
537    <para>
538    Convert the current double value to a <emphasis>G4String</emphasis>, which
539    should be returned by the <literal>GetCurrentValue()</literal> method of your
540    messenger. The double value will be divided by the value of the
541    given unit and converted to a string. Given unit will be added to the string.
542    </para>
543  </para></listitem>
544</itemizedlist>
545</para>
546
547<!-- ******* Bridgehead ******* -->
548<bridgehead renderas='sect4'>
549G4UIcmdWith3VectorAndUnit
550</bridgehead>
551
552<para>
553This is a <emphasis>G4UIcommand</emphasis> derived class which takes one
554three vector parameter and its unit.
555
556<itemizedlist spacing="compact">
557  <listitem><para>
558    <literal>G4UIcmdWith3VectorAndUnit(char* commandpath,
559    G4UImanager* theMessenger)</literal>
560    <para>
561    Constructor. Arguments are the (full-path) command name and the
562    pointer to your messenger.</para>
563  </para></listitem>
564  <listitem><para>
565    <literal>void SetParameterName(char* paramNamX, char* paramNamY,
566    char* paramNamZ,G4bool omittable)</literal>
567    <para>
568    Define the names of each component of the three vector and set
569    the omittable flag. If omittable is true, you should define the
570    default value using the next method.
571    </para>
572  </para></listitem>
573  <listitem><para>
574    <literal>void SetDefaultValue(G4ThreeVector defVal)</literal>
575    <para>
576    Define the default value of the three vector.
577    </para>
578  </para></listitem>
579  <listitem><para>
580    <literal>void SetUnitCategory(char* unitCategory)</literal>
581    <para>
582    Define acceptable unit category.
583    </para>
584  </para></listitem>
585  <listitem><para>
586    <literal>void SetDefaultUnit(char* defUnit)</literal>
587    <para>
588    Define the default unit. Please use this method and the
589    <literal>SetUnitCategory()</literal> method alternatively.
590    </para>
591  </para></listitem>
592  <listitem><para>
593    <literal>G4ThreeVector GetNew3VectorValue(G4String paramString)</literal>
594    <para>
595    Convert a <emphasis>G4String</emphasis> parameter value given by the
596    <literal>SetNewValue()</literal> method of your messenger into a
597    <emphasis>G4ThreeVector</emphasis>. Please note that the return value has
598    already been multiplied by the value of the given unit.
599    </para>
600  </para></listitem>
601  <listitem><para>
602    <literal>G4ThreeVector GetNew3VectorRawValue(G4String paramString)</literal>
603    <para>
604    Convert a <emphasis>G4String</emphasis> parameter value given by the
605    <literal>SetNewValue()</literal> method of your messenger into three vector,
606    but without multiplying the value of the given unit.
607    </para>
608  </para></listitem>
609  <listitem><para>
610    <literal>G4double GetNewUnitValue(G4String paramString)</literal>
611    <para>
612    Convert a <emphasis>G4String</emphasis> unit value given by the
613    <literal>SetNewValue()</literal> method of your messenger into a double.
614    </para>
615  </para></listitem>
616  <listitem><para>
617    <literal>G4String convertToString(G4ThreeVector currVal,
618    char* unitName)</literal>
619    <para>
620    Convert the current three vector to a <emphasis>G4String</emphasis> which
621    should be returned by the <literal>GetCurrentValue()</literal> method of
622    your messenger. The three vector value will be divided by the value of
623    the given unit and converted to a string. Given unit will be added
624    to the string.
625    </para>
626  </para></listitem>
627</itemizedlist>
628</para>
629
630<!-- ******* Bridgehead ******* -->
631<bridgehead renderas='sect4'>
632Additional comments on the <literal>SetParameterName()</literal> method
633</bridgehead>
634
635<para>
636You can add one additional argument of <literal>G4bool</literal> type for
637every <literal>SetParameterName()</literal> method mentioned above. This
638additional argument is named <literal>currentAsDefaultFlag</literal> and the
639default value of this argument is <literal>false</literal>. If you assign
640this extra argument as <literal>true</literal>, the default value of the
641parameter will be overriden by the current value of the target
642class.
643</para>
644
645</sect2>
646
647
648<!-- ******************* Section (Level#2) ****************** -->
649<sect2 id="sect.UIDefNew.ExpMess">
650<title>
651An example messenger
652</title>
653
654<para>
655This example is of <emphasis>G4ParticleGunMessenger</emphasis>, which is 
656made by inheriting <emphasis>G4UIcommand</emphasis>.
657
658<example id="programlist_UIDefNew_1">
659<title>
660An example of <literal>G4ParticleGunMessenger.hh</literal>.
661</title>
662
663<programlisting>
664#ifndef G4ParticleGunMessenger_h
665#define G4ParticleGunMessenger_h 1
666
667class G4ParticleGun;
668class G4ParticleTable;
669class G4UIcommand;
670class G4UIdirectory;
671class G4UIcmdWithoutParameter;
672class G4UIcmdWithAString;
673class G4UIcmdWithADoubleAndUnit;
674class G4UIcmdWith3Vector;
675class G4UIcmdWith3VectorAndUnit;
676
677#include "G4UImessenger.hh"
678#include "globals.hh"
679
680class G4ParticleGunMessenger: public G4UImessenger
681{
682  public:
683    G4ParticleGunMessenger(G4ParticleGun * fPtclGun);
684    ~G4ParticleGunMessenger();
685   
686  public:
687    void SetNewValue(G4UIcommand * command,G4String newValues);
688    G4String GetCurrentValue(G4UIcommand * command);
689
690  private:
691    G4ParticleGun * fParticleGun;
692    G4ParticleTable * particleTable;
693   
694  private: //commands
695    G4UIdirectory *             gunDirectory;
696    G4UIcmdWithoutParameter *   listCmd;
697    G4UIcmdWithAString *        particleCmd;
698    G4UIcmdWith3Vector *        directionCmd;
699    G4UIcmdWithADoubleAndUnit * energyCmd;
700    G4UIcmdWith3VectorAndUnit * positionCmd;
701    G4UIcmdWithADoubleAndUnit * timeCmd;
702   
703};
704
705#endif
706</programlisting>
707</example>
708</para>
709
710
711<para>
712<example id="programlist_UIDefNew_2">
713<title>
714An example of <literal>G4ParticleGunMessenger.cc</literal>.
715</title>
716
717<programlisting>
718#include "G4ParticleGunMessenger.hh"
719#include "G4ParticleGun.hh"
720#include "G4Geantino.hh"
721#include "G4ThreeVector.hh"
722#include "G4ParticleTable.hh"
723#include "G4UIdirectory.hh"
724#include "G4UIcmdWithoutParameter.hh"
725#include "G4UIcmdWithAString.hh"
726#include "G4UIcmdWithADoubleAndUnit.hh"
727#include "G4UIcmdWith3Vector.hh"
728#include "G4UIcmdWith3VectorAndUnit.hh"
729#include &lt;iostream.h&gt;
730
731G4ParticleGunMessenger::G4ParticleGunMessenger(G4ParticleGun * fPtclGun)
732:fParticleGun(fPtclGun)
733{
734  particleTable = G4ParticleTable::GetParticleTable();
735
736  gunDirectory = new G4UIdirectory("/gun/");
737  gunDirectory-&gt;SetGuidance("Particle Gun control commands.");
738
739  listCmd = new G4UIcmdWithoutParameter("/gun/list",this);
740  listCmd-&gt;SetGuidance("List available particles.");
741  listCmd-&gt;SetGuidance(" Invoke G4ParticleTable.");
742
743  particleCmd = new G4UIcmdWithAString("/gun/particle",this);
744  particleCmd-&gt;SetGuidance("Set particle to be generated.");
745  particleCmd-&gt;SetGuidance(" (geantino is default)");
746  particleCmd-&gt;SetParameterName("particleName",true);
747  particleCmd-&gt;SetDefaultValue("geantino");
748  G4String candidateList;
749  G4int nPtcl = particleTable-&gt;entries();
750  for(G4int i=0;i&lt;nPtcl;i++)
751  {
752    candidateList += particleTable-&gt;GetParticleName(i);
753    candidateList += " ";
754  }
755  particleCmd-&gt;SetCandidates(candidateList);
756
757  directionCmd = new G4UIcmdWith3Vector("/gun/direction",this);
758  directionCmd-&gt;SetGuidance("Set momentum direction.");
759  directionCmd-&gt;SetGuidance("Direction needs not to be a unit vector.");
760  directionCmd-&gt;SetParameterName("Px","Py","Pz",true,true);
761  directionCmd-&gt;SetRange("Px != 0 || Py != 0 || Pz != 0");
762 
763  energyCmd = new G4UIcmdWithADoubleAndUnit("/gun/energy",this);
764  energyCmd-&gt;SetGuidance("Set kinetic energy.");
765  energyCmd-&gt;SetParameterName("Energy",true,true);
766  energyCmd-&gt;SetDefaultUnit("GeV");
767  energyCmd-&gt;SetUnitCandidates("eV keV MeV GeV TeV");
768
769  positionCmd = new G4UIcmdWith3VectorAndUnit("/gun/position",this);
770  positionCmd-&gt;SetGuidance("Set starting position of the particle.");
771  positionCmd-&gt;SetParameterName("X","Y","Z",true,true);
772  positionCmd-&gt;SetDefaultUnit("cm");
773  positionCmd-&gt;SetUnitCandidates("micron mm cm m km");
774
775  timeCmd = new G4UIcmdWithADoubleAndUnit("/gun/time",this);
776  timeCmd-&gt;SetGuidance("Set initial time of the particle.");
777  timeCmd-&gt;SetParameterName("t0",true,true);
778  timeCmd-&gt;SetDefaultUnit("ns");
779  timeCmd-&gt;SetUnitCandidates("ns ms s");
780 
781  // Set initial value to G4ParticleGun
782  fParticleGun-&gt;SetParticleDefinition( G4Geantino::Geantino() );
783  fParticleGun-&gt;SetParticleMomentumDirection( G4ThreeVector(1.0,0.0,0.0) );
784  fParticleGun-&gt;SetParticleEnergy( 1.0*GeV );
785  fParticleGun-&gt;SetParticlePosition(G4ThreeVector(0.0*cm, 0.0*cm, 0.0*cm));
786  fParticleGun-&gt;SetParticleTime( 0.0*ns );
787}
788</programlisting>
789</example>
790<informalexample>
791<programlisting>
792G4ParticleGunMessenger::~G4ParticleGunMessenger()
793{
794  delete listCmd;
795  delete particleCmd;
796  delete directionCmd;
797  delete energyCmd;
798  delete positionCmd;
799  delete timeCmd;
800  delete gunDirectory;
801}
802
803void G4ParticleGunMessenger::SetNewValue(
804  G4UIcommand * command,G4String newValues)
805{
806  if( command==listCmd )
807  { particleTable-&gt;dumpTable(); }
808  else if( command==particleCmd )
809  {
810    G4ParticleDefinition* pd = particleTable-&gt;findParticle(newValues);
811    if(pd != NULL)
812    { fParticleGun-&gt;SetParticleDefinition( pd ); }
813  }
814  else if( command==directionCmd )
815  { fParticleGun-&gt;SetParticleMomentumDirection(directionCmd-&gt;
816     GetNew3VectorValue(newValues)); }
817  else if( command==energyCmd )
818  { fParticleGun-&gt;SetParticleEnergy(energyCmd-&gt;
819     GetNewDoubleValue(newValues)); }
820  else if( command==positionCmd )
821  { fParticleGun-&gt;SetParticlePosition(
822     directionCmd-&gt;GetNew3VectorValue(newValues)); }
823  else if( command==timeCmd )
824  { fParticleGun-&gt;SetParticleTime(timeCmd-&gt;
825     GetNewDoubleValue(newValues)); }
826}
827
828G4String G4ParticleGunMessenger::GetCurrentValue(G4UIcommand * command)
829{
830  G4String cv;
831 
832  if( command==directionCmd )
833  { cv = directionCmd-&gt;ConvertToString(
834     fParticleGun-&gt;GetParticleMomentumDirection()); }
835  else if( command==energyCmd )
836  { cv = energyCmd-&gt;ConvertToString(
837     fParticleGun-&gt;GetParticleEnergy(),"GeV"); }
838  else if( command==positionCmd )
839  { cv = positionCmd-&gt;ConvertToString(
840     fParticleGun-&gt;GetParticlePosition(),"cm"); }
841  else if( command==timeCmd )
842  { cv = timeCmd-&gt;ConvertToString(
843     fParticleGun-&gt;GetParticleTime(),"ns"); }
844  else if( command==particleCmd )
845  { // update candidate list
846    G4String candidateList;
847    G4int nPtcl = particleTable-&gt;entries();
848    for(G4int i=0;i&lt;nPtcl;i++)
849    {
850      candidateList += particleTable-&gt;GetParticleName(i);
851      candidateList += " ";
852    }
853    particleCmd-&gt;SetCandidates(candidateList);
854  }
855  return cv;
856}
857</programlisting>
858</informalexample>
859</para>
860
861</sect2>
862
863
864<!-- ******************* Section (Level#2) ****************** -->
865<sect2 id="sect.UIDefNew.HowCont">
866<title>
867How to control the output of G4cout/G4cerr
868</title>
869
870<para>
871Instead of <emphasis>cout</emphasis> and <emphasis>cerr</emphasis>,
872Geant4 uses <emphasis>G4cout</emphasis> and
873<emphasis>G4cerr</emphasis>. Output streams from
874<emphasis>G4cout/G4cerr</emphasis> 
875are handled by <emphasis>G4UImanager</emphasis> which allows the application
876programmer to control the flow of the stream. Output strings may
877therefore be displayed on another window or stored in a file. This
878is accomplished as follows:
879
880<orderedlist spacing="compact">
881  <listitem><para>
882    Derive a class from <emphasis>G4UIsession</emphasis> and implement the two
883    methods:
884
885    <para>
886    <informalexample>
887    <programlisting>
888      G4int ReceiveG4cout(G4String coutString);
889      G4int ReceiveG4cerr(G4String cerrString);
890    </programlisting>
891    </informalexample> 
892
893    These methods receive the string stream of <emphasis>G4cout</emphasis> and
894    <emphasis>G4cerr</emphasis>, respectively. The string can be handled to meet
895    specific requirements. The following sample code shows how to make
896    a log file of the output stream:
897
898    <informalexample>
899    <programlisting>
900        ostream logFile;
901        logFile.open("MyLogFile");
902        G4int MySession::ReceiveG4cout(G4String coutString)
903        {
904          logFile &lt;&lt; coutString &lt;&lt; flush;
905          return 0;
906        }
907    </programlisting>
908    </informalexample>
909    </para>
910  </para></listitem>
911  <listitem><para>
912    <para>
913    Set the destination of <emphasis>G4cout/G4cerr</emphasis> using
914    <literal>G4UImanager::SetCoutDestination(session)</literal>.
915    </para>
916    <para>
917    Typically this method is invoked from the constructor of
918    <emphasis>G4UIsession</emphasis> and its derived classes, such as
919    <emphasis>G4UIGAG/G4UIteminal</emphasis>. This method sets the destination of
920    <emphasis>G4cout/G4cerr</emphasis> to the session. For example, when the
921    following code appears in the constructor of <emphasis>G4UIterminal</emphasis>,
922    the method <literal>SetCoutDestination(this)</literal> tells
923    <emphasis>UImanager</emphasis> that this instance of
924    <emphasis>G4UIterminal</emphasis> receives the stream
925    generated by <emphasis>G4cout</emphasis>.
926
927    <informalexample>
928    <programlisting>
929        G4UIterminal::G4UIterminal()
930        {
931          UI = G4UImanager::GetUIpointer();
932          UI-&gt;SetCoutDestination(this);
933          //  ...
934        }
935    </programlisting>
936    </informalexample>   
937 
938    Similarly, <literal>UI-&gt;SetCoutDestination(NULL)</literal> must be added
939    to the destructor of the class.
940    </para>
941  </para></listitem>
942  <listitem><para>
943    Write or modify the main program. To modify <literal>exampleN01</literal>
944    to produce a log file, derive a class as described in step 1 above,
945    and add the following lines to the main program:
946
947    <informalexample>
948    <programlisting>
949        #include "MySession.hh"
950        main()
951        {
952          // get the pointer to the User Interface manager
953          G4UImanager* UI = G4UImanager::GetUIpointer();
954          // construct a session which receives G4cout/G4cerr
955          MySession * LoggedSession = new MySession;
956          UI-&gt;SetCoutDestination(LoggedSession);
957          // session-&gt;SessionStart(); // not required in this case
958          // .... do simulation here ...
959
960          delete LoggedSession;
961          return 0;
962        }
963    </programlisting>
964    </informalexample> 
965  </para></listitem>
966</orderedlist>
967</para>
968
969<note>
970<para>
971<emphasis>G4cout/G4cerr</emphasis> should not be used in the constructor of a
972class if the instance of the class is intended to be used as
973<literal>static</literal>. This restriction comes from the language
974specification of C++. See the documents below for details:
975
976<itemizedlist spacing="compact">
977   <listitem><para>
978     M.A.Ellis, B.Stroustrup, ``Annotated C++ Reference Manual'', Section 3.4
979     <citation>
980     <xref linkend="biblio.ellis1990" endterm="biblio.ellis1990.abbrev" />
981     </citation>     
982   </para></listitem>
983   <listitem><para>
984     P.J.Plauger, ``The Draft Standard C++ Library''
985     <citation>
986     <xref linkend="biblio.plauger1995" endterm="biblio.plauger1995.abbrev" />
987     </citation>     
988   </para></listitem>
989</itemizedlist>
990</para>
991</note>
992
993
994</sect2>
995</sect1>
Note: See TracBrowser for help on using the repository browser.