Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JEventProcessor_EPICS_dump.cc
Go to the documentation of this file.
1 // $Id$
2 //
3 // File: JEventProcessor_EPICS_dump.cc
4 // Created: Fri Nov 9 11:58:09 EST 2012
5 // Creator: wolin (on Linux stan.jlab.org 2.6.32-279.11.1.el6.x86_64 x86_64)
6 
7 
8 #include <stdint.h>
9 #include <vector>
10 #include <deque>
11 #include <string>
12 #include <iostream>
13 #include <algorithm>
14 #include <stdio.h>
15 #include <stdlib.h>
16 
17 #include "TH2I.h"
18 
20 #include <JANA/JApplication.h>
21 
22 #include "TRIGGER/DL1Trigger.h"
23 #include <DANA/DStatusBits.h>
24 #include "DAQ/DEPICSvalue.h"
25 
26 using namespace std;
27 using namespace jana;
28 
29 #include <TDirectory.h>
30 #include <TH1.h>
31 
32 
33 // root hist pointers
34 static TH1I* h1epics_AD00 = NULL;
35 static TH2I* h2epics_pos_inner = NULL;
36 static TH2I* h2epics_pos_outer = NULL;
37 static TH1F* h1epics_AD00_VSevent = NULL;
38 static TH1F* h1epics_entries_VSevent = NULL;
39 
40 //----------------------------------------------------------------------------------
41 
42 
43 // Routine used to create our JEventProcessor
44 extern "C"{
45  void InitPlugin(JApplication *locApplication){
46  InitJANAPlugin(locApplication);
47  locApplication->AddProcessor(new JEventProcessor_EPICS_dump());
48  }
49 }
50 
51 
52 //----------------------------------------------------------------------------------
53 
54 
56 }
57 
58 
59 //----------------------------------------------------------------------------------
60 
61 
63 }
64 
65 
66 //----------------------------------------------------------------------------------
67 
69 
70  // First thread to get here makes all histograms. If one pointer is
71  // already not NULL, assume all histograms are defined and return now
72  if(h1epics_AD00 != NULL){
73  return NOERROR;
74  }
75 
76  // create root folder for trig and cd to it, store main dir
77  TDirectory *main = gDirectory;
78  gDirectory->mkdir("EPICS_dump")->cd();
79 
80  // book hist
81  int const nbins=100;
82 
83  h1epics_AD00 = new TH1I("h1epics_AD00", "Current AD00",nbins,0,500);
84  h1epics_AD00->SetXTitle("Current AD00 (nA)");
85  h1epics_AD00->SetYTitle("counts");
86 
87  h2epics_pos_inner = new TH2I("h1epics_pos_inner", "Position AC inner",nbins,-10,10,nbins,-10,10);
88  h2epics_pos_inner->SetXTitle("Position AC inner x (mm)");
89  h2epics_pos_inner->SetYTitle("Position AC inner y (mm)");
90  h2epics_pos_outer = new TH2I("h1epics_pos_outer", "Position AC outer",nbins,-20,20,nbins,-20,20);
91  h2epics_pos_outer->SetXTitle("Position AC outer x (mm)");
92  h2epics_pos_outer->SetYTitle("Position AC outer y (mm)");
93 
94  h1epics_AD00_VSevent = new TH1F("h1epics_AD00_VSevent", "Current AD00 (nA)",200,0,2e6);
95  h1epics_AD00_VSevent->SetXTitle("Event number");
96  h1epics_AD00_VSevent->SetYTitle("Current (nA)");
97 
98  h1epics_entries_VSevent = new TH1F("h1epics_entries_VSevent", "No epics events/bin",200,0,2e6);
99  h1epics_entries_VSevent->SetXTitle("Event number");
100  h1epics_entries_VSevent->SetYTitle("No of epics events");
101 
102  // back to main dir
103  main->cd();
104 
105  return NOERROR;
106 }
107 
108 
109 //----------------------------------------------------------------------------------
110 
111 
112 jerror_t JEventProcessor_EPICS_dump::brun(jana::JEventLoop* locEventLoop, int locRunNumber) {
113  // This is called whenever the run number changes
114  return NOERROR;
115 }
116 
117 
118 //----------------------------------------------------------------------------------
119 
120 
121 jerror_t JEventProcessor_EPICS_dump::evnt(jana::JEventLoop* locEventLoop, uint64_t locEventNumber) {
122  // This is called for every event. Use of common resources like writing
123  // to a file or filling a histogram should be mutex protected. Using
124  // loop-Get(...) to get reconstructed objects (and thereby activating the
125  // reconstruction algorithm) should be done outside of any mutex lock
126  // since multiple threads may call this method at the same time.
127 
128  vector<const DEPICSvalue*> epicsvalues;
129  locEventLoop->Get(epicsvalues);
130 
131  bool isEPICS = locEventLoop->GetJEvent().GetStatusBit(kSTATUS_EPICS_EVENT);
132  if(!isEPICS)
133  return NOERROR;
134 
135  // FILL HISTOGRAMS
136  // Since we are filling histograms local to this plugin, it will not interfere with other ROOT operations: can use plugin-wide ROOT fill lock
137  japp->RootFillLock(this); //ACQUIRE ROOT FILL LOCK
138 
139  // process EPICS records
140  //printf (" Event=%d is an EPICS record\n",(int)locEventNumber);
141 
142  // read in whatever epics values are in this event
143 
144  // save their values
145  float pos_default=-1000.;
146  float xpos_inner = pos_default;
147  float ypos_inner = pos_default;;
148  float xpos_outer = pos_default;
149  float ypos_outer = pos_default;
150  for(vector<const DEPICSvalue*>::const_iterator val_itr = epicsvalues.begin();
151  val_itr != epicsvalues.end(); val_itr++) {
152  const DEPICSvalue* epics_val = *val_itr;
153  //cout << "EPICS: " << epics_val->name << " = " << epics_val->sval << endl;
154  float fconv = atof(epics_val->sval.c_str());
155  unsigned int iconv = atoi(epics_val->sval.c_str());
156  bool isDigit = epics_val->name.length()> 12 && isdigit(epics_val->name[12]);
157  // cout << "isDigit=" << isDigit << " string=" << epics_val->name << endl;
158  if ((epics_val->name.substr(0,11) == "BCAL:pulser") & isdigit(epics_val->name[11])) {
159  val_itr++; // increment counter and get low order word
160  epics_val = *val_itr;
161  //cout << "EPICS: " << epics_val->name << " = " << epics_val->sval << endl;
162  unsigned int iconv_low = atoi(epics_val->sval.c_str());
163  // cout << "BCAL:pulser status=" << epics_val->name.substr(0,11) << endl;
164  // printf ("BCAL:pulser iconv=%d, %0X, iconv_low=%d %0X\n",iconv,iconv,iconv_low,iconv_low);
165  iconv_low = iconv_low >> 31;
166  iconv = ((iconv <<1) & 0XFFFF) + iconv_low;
167  // cout << "BCAL:pulser status=" << epics_val->name.substr(0,11) << endl;
168  //printf ("BCAL:pulser status=%d, %0X, iconv_low=%0X\n",iconv,iconv,iconv_low);
169  }
170  else if ((epics_val->name.substr(0,11) == "BCAL:pulser") & isDigit) {
171  //double freq = 1.e8/fconv; // cover to s: period is in units 10 ns
172  //cout << "BCAL:pulser=" << epics_val->name.substr(0,11) << epics_val->fval << " freq=" << freq <<endl;
173  }
174  else if (epics_val->name == "IBCAD00CRCUR6") {
175  h1epics_AD00->Fill(fconv);
176  h1epics_AD00_VSevent->Fill((float)locEventNumber,fconv);
177  h1epics_entries_VSevent->Fill((float)locEventNumber);
178  //cout << "IBCAD00CRCUR6 " << epics_val->name << " fconv=" << fconv << endl;
179  }
180  else if (epics_val->name == "AC:inner:position:x") {
181  xpos_inner = fconv;
182  }
183  else if (epics_val->name == "AC:inner:position:y") {
184  ypos_inner = fconv;
185  }
186  else if (epics_val->name == "AC:outer:position:x") {
187  xpos_outer = fconv;
188  }
189  else if (epics_val->name == "AC:outer:position:y") {
190  ypos_outer = fconv;
191  }
192  }
193  if (xpos_inner> pos_default && ypos_inner > pos_default) {
194  h2epics_pos_inner->Fill(xpos_inner,ypos_inner);
195  }
196  if (xpos_outer> pos_default && ypos_outer > pos_default) {
197  h2epics_pos_outer->Fill(xpos_outer,ypos_outer);
198  }
199 
200  japp->RootFillUnLock(this); //RELEASE ROOT FILL LOCK
201 
202  return NOERROR;
203 }
204 
205 
206 //----------------------------------------------------------------------------------
207 
208 
210  // This is called whenever the run number changes, before it is
211  // changed to give you a chance to clean up before processing
212  // events from the next run number.
213  return NOERROR;
214 }
215 
216 
217 //----------------------------------------------------------------------------------
218 
219 
221  // Called before program exit after event processing is finished.
222  return NOERROR;
223 }
224 
225 
226 //----------------------------------------------------------------------------------
227 //----------------------------------------------------------------------------------
jerror_t evnt(jana::JEventLoop *eventLoop, uint64_t eventnumber)
Called every event.
static TH1F * h1epics_AD00_VSevent
string sval
Definition: DEPICSvalue.h:65
jerror_t erun(void)
Called everytime run number changes, provided brun has been called.
JApplication * japp
InitPlugin_t InitPlugin
jerror_t init(void)
Called once at program start.
jerror_t fini(void)
Called after last event of last event source has been processed.
string name
Definition: DEPICSvalue.h:64
static TH2I * h2epics_pos_outer
static TH1F * h1epics_entries_VSevent
static TH2I * h2epics_pos_inner
static TH1I * h1epics_AD00
jerror_t brun(jana::JEventLoop *eventLoop, int32_t runnumber)
Called everytime a new run number is detected.
int main(int argc, char *argv[])
Definition: gendoc.cc:6
A DEPICSvalue object holds information for a single EPICS value read from the data stream...
Definition: DEPICSvalue.h:37