Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hd_root/MyProcessor.cc
Go to the documentation of this file.
1 // Author: Edward Brash February 15, 2005
2 // revised severely 2006-2007 David Lawrence
3 //
4 //
5 // MyProcessor.cc
6 //
7 
8 #include <unistd.h>
9 
10 #include <iostream>
11 using namespace std;
12 
13 #include <TROOT.h>
14 #include <TTree.h>
15 
16 #include "MyProcessor.h"
17 
18 vector<string> toprint;
19 bool ACTIVATE_ALL=false;
20 
21 extern string OUTPUT_FILENAME;
22 
23 #define ansi_escape ((char)0x1b)
24 #define ansi_bold ansi_escape<<"[1m"
25 #define ansi_black ansi_escape<<"[30m"
26 #define ansi_red ansi_escape<<"[31m"
27 #define ansi_green ansi_escape<<"[32m"
28 #define ansi_blue ansi_escape<<"[34m"
29 #define ansi_normal ansi_escape<<"[0m"
30 #define ansi_up(A) ansi_escape<<"["<<(A)<<"A"
31 #define ansi_down(A) ansi_escape<<"["<<(A)<<"B"
32 #define ansi_forward(A) ansi_escape<<"["<<(A)<<"C"
33 #define ansi_back(A) ansi_escape<<"["<<(A)<<"D"
34 
35 
36 //------------------------------------------------------------------
37 // MyProcessor
38 //------------------------------------------------------------------
40 {
41  ROOTfile = NULL;
42 }
43 
44 //------------------------------------------------------------------
45 // ~MyProcessor
46 //------------------------------------------------------------------
48 {
49  //Close the ROOT file
50  if(ROOTfile!=NULL){
51  ROOTfile->Write();
52  ROOTfile->Close();
53  delete ROOTfile;
54  ROOTfile=NULL;
55  cout<<endl<<"Closed ROOT file"<<endl;
56  }
57 }
58 
59 //------------------------------------------------------------------
60 // init -Open output file here (e.g. a ROOT file)
61 //------------------------------------------------------------------
62 jerror_t MyProcessor::init(void)
63 {
64  // open ROOT file
65  ROOTfile = new TFile(OUTPUT_FILENAME.c_str(),"RECREATE","Produced by hd_root");
66  if(!ROOTfile->IsOpen()){
67  cout << "Cannot open ROOT file. Quitting now." << endl;
68  exit(0);
69  }
70 
71  cout<<"Opened ROOT file \""<<OUTPUT_FILENAME<<"\" ..."<<endl;
72 
73  return NOERROR;
74 }
75 
76 //------------------------------------------------------------------
77 // brun
78 //------------------------------------------------------------------
79 jerror_t MyProcessor::brun(JEventLoop *eventLoop, int32_t runnumber)
80 {
81  vector<string> factory_names;
82  eventLoop->GetFactoryNames(factory_names);
83 
84  usleep(100000); //this just gives the Main thread a chance to finish printing the "Launching threads" message
85  cout<<endl;
86 
87  // If ACTIVATE_ALL is set then add EVERYTHING.
88  if(ACTIVATE_ALL){
89  toprint = factory_names;
90  }else{
91  // make sure factories exist for all requested data types
92  // If a factory isn't found, but one with a "D" prefixed
93  // is, go ahead and correct the name.
94  vector<string> really_toprint;
95  for(unsigned int i=0; i<toprint.size();i++){
96  int found = 0;
97  int dfound = 0;
98  for(unsigned int j=0;j<factory_names.size();j++){
99  if(factory_names[j] == toprint[i])found = 1;
100  if(factory_names[j] == "D" + toprint[i])dfound = 1;
101  }
102  if(found)
103  really_toprint.push_back(toprint[i]);
104  else if(dfound)
105  really_toprint.push_back("D" + toprint[i]);
106  else
107  cout<<ansi_red<<"WARNING:"<<ansi_normal
108  <<" Couldn't find factory for \""
110  <<"\"!"<<endl;
111  }
112 
113  toprint = really_toprint;
114  }
115 
116  // At this point, toprint should contain a list of all factories
117  // in dataClassName:tag format, that both exist and were requested.
118  // Seperate the tag from the name and fill the fac_info vector.
119  fac_info.clear();
120  for(unsigned int i=0;i<toprint.size();i++){
121  string name = toprint[i];
122  string tag = "";
123  unsigned int pos = name.rfind(":",name.size()-1);
124  if(pos != (unsigned int)string::npos){
125  tag = name.substr(pos+1,name.size());
126  name.erase(pos);
127  }
128  factory_info_t f;
129  f.dataClassName = name;
130  f.tag = tag;
131  fac_info.push_back(f);
132  }
133 
134  cout<<endl;
135 
136  return NOERROR;
137 }
138 
139 //------------------------------------------------------------------
140 // evnt -Fill tree here
141 //------------------------------------------------------------------
142 jerror_t MyProcessor::evnt(JEventLoop *eventLoop, uint64_t eventnumber)
143 {
144  // Loop over factories explicitly mentioned on command line
145  for(unsigned int i=0;i<toprint.size();i++){
146  string name =fac_info[i].dataClassName;
147  string tag = fac_info[i].tag;
148  JFactory_base *factory = eventLoop->GetFactory(name,tag.c_str());
149  if(!factory)factory = eventLoop->GetFactory("D" + name,tag.c_str());
150  if(factory){
151  try{
152  factory->GetNrows();
153  }catch(...){
154  // someone threw an exception
155  }
156  }
157  }
158  return NOERROR;
159 }
160 
161 //------------------------------------------------------------------
162 // fini
163 //------------------------------------------------------------------
164 jerror_t MyProcessor::fini(void)
165 {
166  return NOERROR;
167 }
168 
TFile * ROOTfile
jerror_t init(void)
string OUTPUT_FILENAME
Definition: hd_root.cc:17
jerror_t brun(JEventLoop *eventLoop, int32_t runnumber)
Called once at program start.
TF1 * f
Definition: FitGains.C:21
#define ansi_bold
bool ACTIVATE_ALL
#define ansi_normal
set< string > toprint
jerror_t fini(void)
Called everytime run number changes, provided brun has been called.
#define ansi_red
jerror_t evnt(JEventLoop *eventLoop, uint64_t eventnumber)
Called every event.