Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hdview2.cc
Go to the documentation of this file.
1 
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <pthread.h>
5 #include <TGApplication.h>
6 
7 #include "hdview2.h"
8 #include "MyProcessor.h"
9 
10 int GO = 0; // 1=continuously display events 0=wait for user
11 bool PRINT_FACTORY_LIST = false;
12 bool SKIP_EPICS_EVENTS = true;
13 
14 TCanvas *maincanvas=NULL;
15 extern JApplication *japp;
16 JEventLoop *eventloop =NULL;
18 
19 int32_t RUNNUMBER = 9999; // set with RUNNUMBER config paramter
20 
21 void PrintFactoryList(JApplication *japp);
22 void ParseCommandLineArguments(int &narg, char *argv[], JApplication *japp);
23 void Usage(JApplication *japp);
24 
25 //-------------------
26 // main
27 //-------------------
28 int main(int narg, char *argv[])
29 {
30  // Parse the command line arguments
31  ParseCommandLineArguments(narg, argv, japp);
32 
33  // Instantiate a DApplication object. This has to be done BEFORE
34  // creating the TApplication object since that modifies the argument list.
35  japp = new DApplication(narg, argv);
36 
37  // Check if user specified a run number via config. parameter
38  try{ gPARMS->GetParameter("RUNNUMBER", RUNNUMBER); }catch(...){}
39 
40  // Create a ROOT TApplication object
41  TApplication app("HDView", &narg, argv);
42 
43  // This is done AFTER creating the TApplication object so when the
44  // init routine is called, the window will be mapped and it can
45  // draw the detectors.
46  myproc = new MyProcessor();
47  japp->AddProcessor(myproc);
48 
49  // Call the JApplication's Init() routine to attach any plugins
50  japp->Init();
51 
52  // Create the JEventLoop object explicitly.
53  eventloop = new JEventLoop(japp);
54  eventloop->SetAutoFree(0); // prevent auto-freeing of event after OneEvent is called
55 
56 
57 
58  // We need to re-call myproc->init here (it was already called from the japp->Init()
59  // call above). This is because the previous call was done before the JEventLoop
60  // object existed and so the hdv_mainframe object wasn't created etc... We have
61  // to call japp->Init() before instantiated the JEventLoop so that the plugins
62  // are attached and the JEventLoop can capture the full list of factories and
63  // processors.
64  myproc->init();
65 
66  // If the PRINT_FACTORY_LIST flag was set, then print the factory list
68 
69  // Process the first event
70  eventloop->OneEvent();
71 
72  // Hand control to the ROOT "event" loop
73  japp->GetJParameterManager()->PrintParameters();
74  app.SetReturnFromRun(true);
75  app.Run();
76 
77  // Clean-up the app (call erun and fini methods, delete sources)
78  //japp->Fini(); // This now actually done in hdv_mainframe::DoQuit()
79 
80  if(japp)delete japp;
81 
82  return 0;
83 }
84 
85 //-----------
86 // PrintFactoryList
87 //-----------
88 void PrintFactoryList(JApplication *japp)
89 {
90  // When we get here, the Run() method hasn't been
91  // called so the JEventLoop objects haven't
92  // been created yet and cansequently the factory objects
93  // don't yet exist. Since we want the "list factories"
94  // option to work even without an input file, we need
95  // to first make the factories before we can list them.
96  // To do this we only need to instantiate a JEventLoop object
97  // passing it our "app" pointer. The JEventLoop will automatically
98  // register itself with the DApplication and the factories
99  // will be made, even ones from plugins passed on the command
100  // line.
101  vector<JEventLoop*> loops = japp->GetJEventLoops();
102  if(loops.size()<1){
103  _DBG_<<"No JEventLoops in japp!"<<endl;
104  return;
105  }
106  JEventLoop *loop = loops[0];
107 
108  // Print header
109  cout<<endl;
110  cout<<" Factory List"<<endl;
111  cout<<"-------------------------"<<endl;
112 
113  // Get list of factories from the JEventLoop and loop over them
114  // Printing out the data types and tags.
115  vector<JFactory_base*> factories = loop->GetFactories();
116  vector<JFactory_base*>::iterator iter = factories.begin();
117  for(; iter!=factories.end(); iter++){
118  cout<<" "<<(*iter)->GetDataClassName();
119  if(strlen((*iter)->Tag()) !=0){
120  cout<<" : "<<(*iter)->Tag();
121  }
122  cout<<endl;
123  }
124  cout<<endl;
125  cout<<" "<<factories.size()<<" factories registered"<<endl;
126  cout<<endl;
127 
128 }
129 
130 
131 //-----------
132 // ParseCommandLineArguments
133 //-----------
134 void ParseCommandLineArguments(int &narg, char *argv[], JApplication *japp)
135 {
136  if(narg==1)Usage(japp);
137 
138  for(int i=1;i<narg;i++){
139  if(argv[i][0] != '-')continue;
140  switch(argv[i][1]){
141  case 'h':
142  Usage(japp);
143  break;
144  case 'L':
145  PRINT_FACTORY_LIST = true;
146  //PrintFactoryList(japp);
147  break;
148  }
149  }
150 
151  // Check if DISPLAY environment variable is set and warn user if not.
152  const char *DISPLAY = getenv("DISPLAY");
153  if(DISPLAY == NULL){
154  jerr << endl;
155  jerr << " WARNING: You do not appear to have your DISPLAY environment" << endl;
156  jerr << " variable set. This may prevent the graphics window" << endl;
157  jerr << " from opening or even cause a seg. fault. Consider" << endl;
158  jerr << " setting this to something like \"localhost:0\" if" << endl;
159  jerr << " you have trouble opening the display." << endl;
160  jerr << endl;
161  }
162 }
163 
164 //-----------
165 // Usage
166 //-----------
167 void Usage(JApplication *japp)
168 {
169  cout<<"Usage:"<<endl;
170  cout<<" hdview2 [options] source1 source2 ..."<<endl;
171  cout<<endl;
172  cout<<"Print the contents of a Hall-D data source (e.g. a file)"<<endl;
173  cout<<"to the screen."<<endl;
174  cout<<endl;
175  cout<<"Options:"<<endl;
176  cout<<endl;
177  cout<<" -h Print this message"<<endl;
178  cout<<" -Dname Print the data of type \"name\" (can be used multiple times)"<<endl;
179  cout<<" -A Print ALL data types (overrides and -DXXX options)"<<endl;
180  cout<<" -L List available factories and exit"<<endl;
181  cout<<" -p Don't pause for keystroke between events (def. is to pause)"<<endl;
182  cout<<" -s Skip events which don't have any of the specified data types"<<endl;
183  cout<<endl;
184  cout<<"JANA options:"<<endl;
185  cout<<endl;
186  japp->Usage();
187  cout<<endl;
188 
189  exit(0);
190 }
jerror_t init(void)
JEventLoop * eventloop
Definition: hdview2.cc:16
bool SKIP_EPICS_EVENTS
Definition: hdview2.cc:12
JApplication * japp
void ParseCommandLineArguments(int &narg, char *argv[])
Definition: hd_dump.cc:124
MyProcessor * myproc
Definition: hdview2.cc:17
#define _DBG_
Definition: HDEVIO.h:12
int32_t RUNNUMBER
Definition: hdview2.cc:19
void PrintFactoryList(DApplication *app)
Definition: hd_dump.cc:84
TCanvas * maincanvas
Definition: hdview2.cc:14
int GO
Definition: hdview2.cc:10
void Usage(JApplication &app)
Definition: hd_ana.cc:33
int main(int argc, char *argv[])
Definition: gendoc.cc:6
bool PRINT_FACTORY_LIST
Definition: hdview2.cc:11