Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JEventProcessor_syncskim.cc
Go to the documentation of this file.
1 // $Id$
2 //
3 // File: JEventProcessor_syncskim.cc
4 // Created: Wed Feb 22 20:04:25 EST 2017
5 // Creator: davidl (on Linux gluon48.jlab.org 2.6.32-431.20.3.el6.x86_64 x86_64)
6 //
7 
8 #include <limits>
9 using namespace std;
10 
12 using namespace jana;
13 
14 #include <DAQ/DL1Info.h>
15 #include <DAQ/DCODAEventInfo.h>
16 #include <DAQ/DCODAControlEvent.h>
17 
18 
19 // Routine used to create our JEventProcessor
20 #include <JANA/JApplication.h>
21 #include <JANA/JFactory.h>
22 extern "C"{
23 void InitPlugin(JApplication *app){
24  InitJANAPlugin(app);
25  app->AddProcessor(new JEventProcessor_syncskim());
26 }
27 } // "C"
28 
29 
30 //------------------
31 // JEventProcessor_syncskim (Constructor)
32 //------------------
34 {
35  sum_n = 0.0;
36  sum_x = 0.0;
37  sum_y = 0.0;
38  sum_xy = 0.0;
39  sum_x2 = 0.0;
40 
41  last_control_event_t = 0.0;
42  last_physics_event_t = 0.0;
43 }
44 
45 //------------------
46 // ~JEventProcessor_syncskim (Destructor)
47 //------------------
49 {
50 
51 }
52 
53 //------------------
54 // init
55 //------------------
57 {
58 
59  gPARMS->SetParameter("EVIO:LINK", false);
60  gPARMS->SetParameter("EVIO:LINK_BORCONFIG", false);
61  gPARMS->SetParameter("EVIO:PARSE_F250", false);
62  gPARMS->SetParameter("EVIO:PARSE_F125", false);
63  gPARMS->SetParameter("EVIO:PARSE_F1TDC", false);
64  gPARMS->SetParameter("EVIO:PARSE_CAEN1290TDC", false);
65  gPARMS->SetParameter("EVIO:PARSE_CONFIG", false);
66  gPARMS->SetParameter("EVIO:PARSE_BOR", false);
67  gPARMS->SetParameter("EVIO:PARSE_EPICS", false);
68  gPARMS->SetParameter("EVIO:PARSE_EVENTTAG", false);
69  //gPARMS->SetParameter("EVIO:PARSE_TRIGGER", false);
70  gPARMS->SetParameter("EVIO:APPLY_TRANSLATION_TABLE", false);
71  gPARMS->SetParameter("EVIO:F250_EMULATION_MODE", 0);
72  gPARMS->SetParameter("EVIO:F125_EMULATION_MODE", 0);
73 
74  tree = new TTree("T", "Sync Events Tree");
75  tree->Branch("run_number", &synevt.run_number, "run_number/i" );
76  tree->Branch("run_type", &synevt.run_type, "run_type/i" );
77  tree->Branch("event_number", &synevt.event_number, "event_number/l" );
78  tree->Branch("event_type", &synevt.event_type, "event_type/s" );
79  tree->Branch("avg_timestamp", &synevt.avg_timestamp, "avg_timestamp/l" );
80 
81  tree->Branch("nsync", &synevt.nsync, "nsync/i" );
82  tree->Branch("trig_number", &synevt.trig_number, "trig_number/i" );
83  tree->Branch("live_time", &synevt.live_time, "live_time/i" );
84  tree->Branch("busy_time", &synevt.nsync, "busy_time/i" );
85  tree->Branch("live_inst", &synevt.live_inst, "live_inst/i" );
86  tree->Branch("unix_time", &synevt.unix_time, "unix_time/i" );
87 
88  tree->Branch("gtp_sc", &synevt.gtp_sc, "gtp_sc[32]/i" );
89  tree->Branch("fp_sc", &synevt.fp_sc, "fp_sc[16]/i" );
90  tree->Branch("gtp_rate", &synevt.gtp_rate, "gtp_rate[32]/i" );
91  tree->Branch("fp_rate", &synevt.fp_rate, "fp_rate[16]/i" );
92  tree->Print();
93 
94  return NOERROR;
95 }
96 
97 //------------------
98 // brun
99 //------------------
100 jerror_t JEventProcessor_syncskim::brun(JEventLoop *eventLoop, int32_t runnumber)
101 {
102  return NOERROR;
103 }
104 
105 //------------------
106 // evnt
107 //------------------
108 jerror_t JEventProcessor_syncskim::evnt(JEventLoop *loop, uint64_t eventnumber)
109 {
110  vector<const DCODAControlEvent*> controlevents;
111  loop->Get(controlevents);
112  if(!controlevents.empty()){
113  last_control_event_t = (double) controlevents[0]->unix_time;
114  last_physics_event_t = 0.0;
115  }
116 
117  vector<const DCODAEventInfo*> codainfos;
118  loop->Get(codainfos);
119  if(codainfos.empty()) return NOERROR;
120  const DCODAEventInfo *codainfo = codainfos[0];
121 
122  if( (last_control_event_t!=0.0) && (last_physics_event_t==0.0) ){
123  last_physics_event_t = (double)codainfo->avg_timestamp / 250.0E6;
124  }
125 
126  vector<const DL1Info*> l1infos;
127  loop->Get(l1infos);
128  if(l1infos.empty()) return NOERROR;
129 
130  japp->RootFillLock(this);
131 
132  for(auto l1info : l1infos){
133 
134  time_t t = l1info->unix_time;
135  cout << "sync event at " << ctime(&t);
136 
137  synevt.run_number = codainfo->run_number;
138  synevt.run_type = codainfo->run_type;
139  synevt.event_number = codainfo->event_number;
140  synevt.event_type = codainfo->event_type;
141  synevt.avg_timestamp = codainfo->avg_timestamp;
142 
143  synevt.nsync = l1info->nsync;
144  synevt.trig_number = l1info->trig_number;
145  synevt.live_time = l1info->live_time;
146  synevt.busy_time = l1info->busy_time;
147  synevt.live_inst = l1info->live_inst;
148  synevt.unix_time = l1info->unix_time;
149  for(int i=0; i<32; i++){
150  synevt.gtp_sc[i] = l1info->gtp_sc[i];
151  synevt.gtp_rate[i] = l1info->gtp_rate[i];
152  if(i<16){
153  synevt.fp_sc[i] = l1info->fp_sc[i];
154  synevt.fp_rate[i] = l1info->fp_rate[i];
155  }
156  }
157 
158  tree->Fill();
159 
160  // scale and shift x/y values to make sure range of sum doesn't cut them off
161  double x = (double)synevt.avg_timestamp / 250.0E6;
162  double y = (double)synevt.unix_time - 13.0E8;
163  sum_n += 1.0;
164  sum_x += x;
165  sum_y += y;
166  sum_xy += x * y;
167  sum_x2 += x * x;
168  }
169 
170  japp->RootFillUnLock(this);
171 
172  return NOERROR;
173 }
174 
175 //------------------
176 // erun
177 //------------------
179 {
180  return NOERROR;
181 }
182 
183 //------------------
184 // fini
185 //------------------
187 {
188 
189  double m = (sum_n*sum_xy - sum_x*sum_y)/(sum_n*sum_x2 - sum_x*sum_x);
190  double b = (sum_y*sum_x2 - sum_x*sum_xy)/(sum_n*sum_x2 - sum_x*sum_x);
191 
192  // scale shift back
193  b += 13.0E8;
194  double one_over_m = 250.0E6/m;
195 
196  // Fallback if no sync events
197  if(sum_n==0.0){
198  cout << endl << "No sync events found! Conversion values will be taken from control+physics events." << endl;
199 
200  one_over_m = 250.0E6;
201  b = last_control_event_t - last_physics_event_t;
202  }
203 
204  typedef std::numeric_limits< double > dbl;
205  cout.precision(dbl::max_digits10);
206 
207  cout << endl << "timestamp to unix time conversion: tics_per_sec=" << one_over_m << " unix_start_time=" << b << endl << endl;
208 
209  return NOERROR;
210 }
211 
jerror_t fini(void)
Called after last event of last event source has been processed.
Double_t x[NCHANNELS]
Definition: st_tw_resols.C:39
jerror_t erun(void)
Called everytime run number changes, provided brun has been called.
#define y
uint16_t event_type
JApplication * japp
jerror_t evnt(jana::JEventLoop *eventLoop, uint64_t eventnumber)
Called every event.
uint64_t avg_timestamp
InitPlugin_t InitPlugin
uint32_t run_number
jerror_t init(void)
Called once at program start.
jerror_t brun(jana::JEventLoop *eventLoop, int32_t runnumber)
Called everytime a new run number is detected.
double unix_time
uint32_t run_type
uint64_t event_number