Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hd_dump/MyProcessor.cc
Go to the documentation of this file.
1 // Author: David Lawrence June 25, 2004
2 //
3 //
4 // MyProcessor.cc
5 //
6 
7 #include <iostream>
8 using namespace std;
9 
10 #include <stdio.h>
11 #include <unistd.h>
12 
13 #include <JANA/JApplication.h>
14 #include <JANA/JApplication.h>
15 
16 #include "MyProcessor.h"
17 
18 
20 bool SKIP_BORING_EVENTS = false;
21 bool PRINT_ALL = false;
22 bool PRINT_CORE = false;
24 bool PRINT_SUMMARY_ALL = false;
25 bool PRINT_SUMMARY_HEADER = false;
26 bool PRINT_STATUS_BITS = false;
28 extern bool SPARSIFY_SUMMARY;
29 
30 set<string> toprint;
31 set<string> tosummarize;
32 
33 #define ansi_escape ((char)0x1b)
34 #define ansi_bold ansi_escape<<"[1m"
35 #define ansi_black ansi_escape<<"[30m"
36 #define ansi_red ansi_escape<<"[31m"
37 #define ansi_green ansi_escape<<"[32m"
38 #define ansi_blue ansi_escape<<"[34m"
39 #define ansi_normal ansi_escape<<"[0m"
40 #define ansi_up(A) ansi_escape<<"["<<(A)<<"A"
41 #define ansi_down(A) ansi_escape<<"["<<(A)<<"B"
42 #define ansi_forward(A) ansi_escape<<"["<<(A)<<"C"
43 #define ansi_back(A) ansi_escape<<"["<<(A)<<"D"
44 
45 //------------------------------------------------------------------
46 // brun
47 //------------------------------------------------------------------
48 jerror_t MyProcessor::brun(JEventLoop *eventLoop, int32_t runnumber)
49 {
50  vector<string> factory_names;
51  eventLoop->GetFactoryNames(factory_names);
52 
53  usleep(100000); //this just gives the Main thread a chance to finish printing the "Launching threads" message
54  cout<<endl;
55 
56  // If int PRINT_ALL is set then add EVERYTHING.
57  if(PRINT_ALL){
58  for(auto fac_name : factory_names) toprint.insert(fac_name);
59  SKIP_BORING_EVENTS = 0; // with PRINT_ALL, nothing is boring!
60  }else if(PRINT_SUMMARY_ALL){
61  for(auto fac_name : factory_names) tosummarize.insert(fac_name);
62  SKIP_BORING_EVENTS = 0; // with PRINT_ALL, nothing is boring!
63  }else if(PRINT_CORE){
64 
65  // Make list of "core" factories. n.b. these high level
66  // objects will automatically activate lower level ones
67  set<string> core_factories;
68 
69  tosummarize.insert("DChargedTrack");
70  tosummarize.insert("DNeutralShower");
71 
72  tosummarize.insert("DMCTrackHit");
73  tosummarize.insert("DMCThrown");
74  tosummarize.insert("DMCTrajectoryPoint");
75  tosummarize.insert("DMCReaction");
76 
77  }else{
78  // make sure factories exist for all requested data types
79  // If a factory isn't found, but one with a "D" prefixed
80  // is, go ahead and correct the name.
81  set<string> really_toprint;
82  for(auto fac_name : toprint){
83  bool found = false;
84  bool dfound = false;
85  for(unsigned int j=0;j<factory_names.size();j++){
86  if(factory_names[j] == fac_name)found = true;
87  if(factory_names[j] == "D" + fac_name)dfound = true;
88  }
89  if(found)
90  really_toprint.insert(fac_name);
91  else if(dfound)
92  really_toprint.insert("D" + fac_name);
93  else
94  cout<<ansi_red<<"WARNING:"<<ansi_normal
95  << " Couldn't find factory for \""
96  << ansi_bold << fac_name << ansi_normal
97  << "\"!"<<endl;
98  }
99 
100  toprint = really_toprint;
101  }
102 
103  // Make sure all factories to print are also included in summary
104  tosummarize.insert(toprint.begin(), toprint.end());
105 
106  // At this point, toprint should contain a list of all factories
107  // in dataClassName:tag format, that both exist and were requested.
108  // Seperate the tag from the name and fill the fac_info vector.
109  fac_info.clear();
110  for(auto fac_name : toprint){
111  string tag = "";
112  unsigned int pos = fac_name.rfind(":",fac_name.size()-1);
113  if(pos != (unsigned int)string::npos){
114  tag = fac_name.substr(pos+1,fac_name.size());
115  fac_name.erase(pos);
116  }
118  f.dataClassName = fac_name;
119  f.tag = tag;
120  f.fac = eventLoop->GetFactory(f.dataClassName, f.tag.c_str());
121  fac_info.push_back(f);
122  }
123 
124  cout<<endl;
125 
126  return NOERROR;
127 }
128 
129 //------------------------------------------------------------------
130 // evnt
131 //------------------------------------------------------------------
132 jerror_t MyProcessor::evnt(JEventLoop *eventLoop, uint64_t eventnumber)
133 {
134 
135  // If we're skipping boring events (events with no rows for any of
136  // the types we're printing) we must find out first if the event is
137  // "boring".
138  int event_is_boring = 1;
139 
140  for(unsigned int i=0;i<toprint.size();i++){
141 
142  string name =fac_info[i].dataClassName;
143  string tag = fac_info[i].tag;
144  JFactory_base *factory = eventLoop->GetFactory(name,tag.c_str());
145  if(!factory)factory = eventLoop->GetFactory("D" + name,tag.c_str());
146  if(factory){
147  try{
148  if(factory->GetNrows()>0){
149  event_is_boring=0;
150  if(PRINT_SUMMARY_HEADER)break;
151  }
152  }catch(...){
153  // someone threw an exception
154  }
155  }
156  }
157 
158  if(SKIP_BORING_EVENTS && event_is_boring)return NOERROR;
159  if(!SKIP_BORING_EVENTS)event_is_boring= 0;
160 
161  // Print event separator
162  cout<<"================================================================"<<endl;
163  cout<<"Event: "<<eventnumber<<endl;
164 
165  // We want to print info about all factories results, even if we aren't
166  // printing the actual data. To make sure the informational messages often
167  // printed during brun are printed first, call the GetNrows() method of all factories
168  // ourself first.
170  for( auto fac : eventLoop->GetFactories() ){
171  string name = fac->GetDataClassName();
172  string tag = fac->Tag()==NULL ? "":fac->Tag();
173  if(tag.size()>0)name = name + ":" + tag;
174  if( tosummarize.count(name) ) fac->GetNrows();
175  }
176  }
177 
178  if(PRINT_STATUS_BITS) cout << japp->StatusWordToString(eventLoop->GetJEvent().GetStatus()) << endl;
179  if(PRINT_SUMMARY_HEADER) eventLoop->PrintFactories(SPARSIFY_SUMMARY ? 2:0);
180 
181  // Print data for all specified factories
182  for(unsigned int i=0;i<fac_info.size();i++){
183  try{
184  string name =fac_info[i].dataClassName;
185  string tag = fac_info[i].tag;
186  eventLoop->Print(name,tag.c_str());
187 
188  if(LIST_ASSOCIATED_OBJECTS)PrintAssociatedObjects(eventLoop, &fac_info[i]);
189  }catch(...){
190  // exception thrown
191  }
192  }
193 
194  // If the program is quitting, then don't bother waiting for the user
195  if(eventLoop->GetJApplication()->GetQuittingStatus())return NOERROR;
196 
197  // Wait for user input if pausing
198  if(PAUSE_BETWEEN_EVENTS && !event_is_boring){
199  cerr.flush();
200  cout<<endl<<"< Hit return for the next event (P=prev. Q=quit) >";
201  cout.flush();
202  char c = getchar(); // see note in hd_dump.cc:main()
203  if(c=='\n')cout<<ansi_up(1);
204  cout<<endl;
205  switch(toupper(c)){
206  case 'Q':
207  eventLoop->QuitProgram();
208  break;
209  case 'P':
210  //eventLoop->GotoEvent(eventnumber-1);
211  break;
212  case 'N':
213  break;
214  }
215 
216  cout<<ansi_up(1)<<"\r \r";
217  cout.flush();
218  }
219 
220  return NOERROR;
221 }
222 
223 //------------------------------------------------------------------
224 // PrintAssociatedObjects
225 //------------------------------------------------------------------
226 void MyProcessor::PrintAssociatedObjects(JEventLoop *loop, const factory_info_t *fac_info)
227 {
228  // cast away const-ness of JFactory_base class pointer
229  JFactory_base *fac = const_cast<JFactory_base*>(fac_info->fac);
230  if(!fac)return;
231 
232  // Get list of all objects from this factory
233  vector<void*> vobjs = fac->Get();
234  vector<JObject*> objs;
235  for(unsigned int i=0; i<vobjs.size(); i++)objs.push_back((JObject*)vobjs[i]);
236 
237  // Loop over objects from this factory
238  for(unsigned int i=0; i<objs.size(); i++){
239 
240  // First, get a list of all associated objects
241  vector<const JObject*> aobjs;
242  objs[i]->GetT(aobjs);
243  // If no associated objects, just go on to the next object
244  if(aobjs.size()==0)continue;
245 
246  // Print separator
247  cout<<" [== Associated objects for row "<<i<<" ==]"<<endl;
248 
249  // Make a list of all factories that made objects associated to this one
250  map<JFactory_base*, vector<const JObject*> > aofacs;
251  for(unsigned int j=0; j<aobjs.size(); j++){
252  JFactory_base *aofac = loop->FindOwner(aobjs[j]);
253 
254  map<JFactory_base*, vector<const JObject*> >::iterator iter = aofacs.find(aofac);
255  if(iter==aofacs.end()){
256  vector<const JObject*> tmp;
257  aofacs[aofac] = tmp;
258  }
259  // Record this object as belonging to this factory
260  aofacs[aofac].push_back(aobjs[j]);
261  }
262  // Figure out number of spaces to indent objects based on factory name length
263  map<JFactory_base*, vector<const JObject*> >::iterator iter;
264  unsigned int indent=4; // some minimal string length
265  for(iter=aofacs.begin(); iter!=aofacs.end(); iter++){
266  JFactory_base *fac = iter->first;
267  string name = fac->GetDataClassName();
268  if(strlen(fac->Tag())!=0)name += string(":") + fac->Tag();
269  if(name.length()>indent)indent=name.length();
270  }
271  indent += 4; // indent the factory name itself
272 
273  // Loop over factories that produced associated objects for this object and
274  // list the objects it created
275  for(iter=aofacs.begin(); iter!=aofacs.end(); iter++){
276  JFactory_base *fac = iter->first;
277  vector<const JObject*> &ptrs = iter->second;
278 
279  // Print out factory name
280  string name = fac->GetDataClassName();
281  if(strlen(fac->Tag())!=0)name += string(":") + fac->Tag();
282  cout<<string(indent-name.length()-1,' ');
283  cout<<name<<" ";
284 
285  // Loop over objects from this factory that are in the list
286  for(unsigned int j=0; j<ptrs.size(); j++){
287  if(j!=0)cout<<string(indent,' ');
288  cout<<"0x"<<hex<<(unsigned long)ptrs[j]<<dec<<endl;
289  }
290  }
291  }
292 
293 }
294 
bool LIST_ASSOCIATED_OBJECTS
bool SPARSIFY_SUMMARY
Definition: hd_dump.cc:20
bool SKIP_BORING_EVENTS
char string[256]
#define c
#define ansi_normal
#define ansi_bold
bool PRINT_SUMMARY_HEADER
jerror_t brun(JEventLoop *eventLoop, int32_t runnumber)
Called once at program start.
JApplication * japp
TF1 * f
Definition: FitGains.C:21
bool PAUSE_BETWEEN_EVENTS
#define ansi_up(A)
bool PRINT_SUMMARY_ALL
set< string > tosummarize
#define ansi_red
set< string > toprint
bool PRINT_CORE
bool PRINT_STATUS_BITS
bool ACTIVATE_TAGGED_FOR_SUMMARY
void PrintAssociatedObjects(JEventLoop *eventLoop, const factory_info_t *fac_info)
jerror_t evnt(JEventLoop *eventLoop, uint64_t eventnumber)
Called every event.
Called after last event of last event source has been processed.
bool PRINT_ALL