Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JEventProcessor_TRIG_online.cc
Go to the documentation of this file.
1 // $Id$
2 //
3 // File: JEventProcessor_FCAL_online.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 
18 #include <JANA/JApplication.h>
19 
20 #include "BCAL/DBCALPoint.h"
21 #include "FCAL/DFCALHit.h"
22 #include "TRIGGER/DL1Trigger.h"
23 #include <DANA/DStatusBits.h>
24 #include "FCAL/DFCALGeometry.h"
25 
26 
27 using namespace std;
28 using namespace jana;
29 
30 #include <TDirectory.h>
31 #include <TH1.h>
32 
33 // Routine used to create our JEventProcessor
34 extern "C"{
35  void InitPlugin(JApplication *locApplication){
36  InitJANAPlugin(locApplication);
37  locApplication->AddProcessor(new JEventProcessor_TRIG_online());
38  }
39 }
40 
41 
42 //----------------------------------------------------------------------------------
43 
44 
46 }
47 
48 
49 //----------------------------------------------------------------------------------
50 
51 
53 }
54 
55 
56 //----------------------------------------------------------------------------------
57 
59 
60  timing = 0;
61  gPARMS->SetDefaultParameter("TRIG_ONLINE:TIMING", timing, "Fill trigger timing histograms: default = false");
62 
63  // create root folder for trig and cd to it, store main dir
64  TDirectory *main = gDirectory;
65  gDirectory->mkdir("trig")->cd();
66 
67  int const nbins=100;
68 
69  h1trig_trgbits = new TH1I("h1trig_trgbits", "Trig Trgbits",150,0,150);
70  h1trig_trgbits->SetXTitle("trig_mask || (128+fp_trig_mask/256)");
71  h1trig_trgbits->SetYTitle("counts");
72  //h2trig_fcalVSbcal= new TH2I("h2trig_fcalVSbcal", "E fcal vs E bcal (GeV); Bcal Energy (GeV); Fcal Energy (GeV)",nbins,0,1,nbins,0,2);
73  h2trig_fcalVSbcal= new TH2I("h2trig_fcalVSbcal", "E fcal vs E bcal (GeV); Bcal Energy (GeV); Fcal Energy (GeV)",nbins,0,15,nbins,0,15);
74 
75  // include timing histograms only if flag set
76  if(timing) {
77  h2trig_tfcalVStbcal = new TH2I("h2trig_tfcalVStbcal", "T fcal vs T bcal (ns); Bcal time (ns); Fcal time (ns)",nbins,-200,200,nbins,-200,200);
78  h2trig_tfcalVSfcal = new TH2I("h2trig_tfcalVSfcal", "T fcal vs E fcal; Fcal energy (GeV); Fcal time (ns)",nbins,0,2,nbins,-200,200);
79  h2trig_tbcalVSbcal = new TH2I("h2trig_tbcalVSbcal", "T bcal vs E bcal; Bcal energ (GeV); Bcal time (ns)",nbins,0,2,nbins,-200,200);
80  }
81 
82  // monitor some trigger bits separately
83  dTrigBits.push_back(1); // FCAL-BCAL
84  dTrigBits.push_back(3); // FCAL-BCAL && FCAL
85  dTrigBits.push_back(4); // BCAL only
86  dTrigBits.push_back(5); // FCAL-BCAL && FCAL
87  dTrigBits.push_back(7); // FCAL-BCAL && FCAL && BCAL
88  dTrigBits.push_back(32); //
89  dTrigBits.push_back(33); //
90  dTrigBits.push_back(37); //
91  dTrigBits.push_back(64); //
92  dTrigBits.push_back(65); //
93  dTrigBits.push_back(97); //
94  dTrigBits.push_back(101); //
95 
96  for(size_t loc_i = 0; loc_i < dTrigBits.size(); ++loc_i) {
97 
98  h2trigbits_fcalVSbcal[dTrigBits[loc_i]] = new TH2I(Form("h2trigbit%d_fcalVSbcal", dTrigBits[loc_i]), Form("Trig %d: E fcal vs E bcal (GeV); Bcal Energy (GeV); Fcal Energy (GeV)",dTrigBits[loc_i]),nbins,0,2,nbins,0,4);
99 
100  // include timing histograms only if flag set
101  if(timing) {
102  h2trigbits_tfcalVStbcal[dTrigBits[loc_i]] = new TH2I(Form("h2trigbit%d_tfcalVStbcal", dTrigBits[loc_i]), Form("Trig %d: T fcal vs T bcal (ns); Bcal time (ns); Fcal time (ns)", dTrigBits[loc_i]),nbins,-200,200,nbins,-200,200);
103  h2trigbits_tfcalVSfcal[dTrigBits[loc_i]] = new TH2I(Form("h2trigbit%d_tfcalVSfcal", dTrigBits[loc_i]), Form("Trig %d: T fcal vs E fcal; Fcal energy (GeV); Fcal time (ns)", dTrigBits[loc_i]),nbins,0,2,nbins,-200,200);
104  h2trigbits_tbcalVSbcal[dTrigBits[loc_i]] = new TH2I(Form("h2trigbit%d_tbcalVSbcal", dTrigBits[loc_i]), Form("Trig %d: T bcal vs E bcal; Bcal energy (GeV); Bcal time (ns)", dTrigBits[loc_i]),nbins,0,2,nbins,-200,200);
105  }
106  }
107 
108  // back to main dir
109  main->cd();
110 
111  return NOERROR;
112 }
113 
114 
115 //----------------------------------------------------------------------------------
116 
117 
118 jerror_t JEventProcessor_TRIG_online::brun(jana::JEventLoop* locEventLoop, int locRunNumber) {
119  // This is called whenever the run number changes
120  return NOERROR;
121 }
122 
123 
124 //----------------------------------------------------------------------------------
125 
126 
127 jerror_t JEventProcessor_TRIG_online::evnt(jana::JEventLoop* locEventLoop, uint64_t locEventNumber) {
128  // This is called for every event. Use of common resources like writing
129  // to a file or filling a histogram should be mutex protected. Using
130  // loop-Get(...) to get reconstructed objects (and thereby activating the
131  // reconstruction algorithm) should be done outside of any mutex lock
132  // since multiple threads may call this method at the same time.
133 
134  vector<const DBCALPoint*> bcalpoints;
135  vector<const DFCALHit*> fcalhits;
136  locEventLoop->Get(bcalpoints);
137  locEventLoop->Get(fcalhits);
139 
140  bool isPhysics = locEventLoop->GetJEvent().GetStatusBit(kSTATUS_PHYSICS_EVENT);
141  if(!isPhysics)
142  return NOERROR;
143 
144  // first get trigger bits
145  const DL1Trigger *trig_words = NULL;
146  uint32_t trig_mask, fp_trig_mask;
147  try {
148  locEventLoop->GetSingle(trig_words);
149  } catch(...) {};
150  if (trig_words) {
151  trig_mask = trig_words->trig_mask;
152  fp_trig_mask = trig_words->fp_trig_mask;
153  }
154  else {
155  trig_mask = 0;
156  fp_trig_mask = 0;
157  }
158 
159  int trig_bits = fp_trig_mask > 0? 128 + fp_trig_mask/256: trig_mask;
160  //if (fp_trig_mask>0) printf (" Event=%d trig_bits=%d trig_mask=%X fp_trig_mask=%X\n",(int)locEventNumber,trig_bits,trig_mask,fp_trig_mask);
161 
162  /* fp_trig_mask & 0x100 - upstream LED
163  fp_trig_mask & 0x200 - downstream LED
164  trig_mask & 0x1 - cosmic trigger*/
165 
166  // Compute total energy sums for fcal and bcal (as in the trigger)
167 
168  // loop over all points in FCAL
169  float fcal_energy = 0;
170  float fcal_time = 0;
171  double fcal_rings_masked = 2;
172  if(locEventLoop->GetJEvent().GetRunNumber() < 11127) // ugly run dependence for now
173  fcal_rings_masked = 4;
174  float rmin = fcal_rings_masked*4*sqrt(2); // N rings x 4 cm on the diagonal.
175  for (unsigned int jj=0; jj<fcalhits.size(); jj++) {
176  int rowhit = fcalhits[jj]->row;
177  int columnhit = fcalhits[jj]->column;
178  // printf (" Event=%d, jj=%d, rowhit=%d, columnhit=%d\n",(int)locEventNumber,jj,rowhit,columnhit);
179  DVector2 pos = fcalgeom.positionOnFace(rowhit,columnhit);
180  double r = sqrt(pos.X()*pos.X() + pos.Y()*pos.Y());
181  if (r <= rmin) continue; // keep only hits that are outside a minimum radius
182 
183  // require trigger threshold in sum
184  // if (fcalhits[jj]->E > 65*0.27*7.5/1000) {
185  if (fcalhits[jj]->E*7.5/fcalhits[jj]->intOverPeak > 1.0*(65*0.27*7.5/1000)) {
186  fcal_energy += fcalhits[jj]->E*7.5/fcalhits[jj]->intOverPeak;
187  fcal_time += fcalhits[jj]->t*fcalhits[jj]->E*7.5/fcalhits[jj]->intOverPeak; // calculate energy weighted time
188  }
189  }
190  fcal_time = fcal_energy > 0 ? fcal_time/fcal_energy : -200;
191 
192  // loop over all points in BCAL
193 
194  float bcal_energy = 0;
195  float bcal_time = 0;
196  for (unsigned int jj=0; jj<bcalpoints.size(); jj++) {
197  if (bcalpoints[jj]->E() > (20*0.045*15/2)/1000) {
198  bcal_energy += bcalpoints[jj]->E();
199  bcal_time += bcalpoints[jj]->t()*bcalpoints[jj]->E();
200  }
201  }
202  bcal_time = bcal_energy > 0 ? bcal_time/bcal_energy: -200;
203 
204  // FILL HISTOGRAMS
205  // Since we are filling histograms local to this plugin, it will not interfere with other ROOT operations: can use plugin-wide ROOT fill lock
206  japp->RootFillLock(this); //ACQUIRE ROOT FILL LOCK
207 
208  h1trig_trgbits->Fill(trig_bits);
209  h2trig_fcalVSbcal->Fill(bcal_energy,fcal_energy);
210  if(timing) {
211  h2trig_tfcalVStbcal->Fill(bcal_time,fcal_time);
212  h2trig_tfcalVSfcal->Fill(fcal_energy,fcal_time);
213  h2trig_tbcalVSbcal->Fill(bcal_energy,bcal_time);
214  }
215 
216  for(size_t loc_i = 0; loc_i < dTrigBits.size(); ++loc_i) {
217  if(trig_bits != (int)dTrigBits[loc_i])
218  continue;
219 
220  h2trigbits_fcalVSbcal[dTrigBits[loc_i]]->Fill(bcal_energy,fcal_energy);
221  if(timing) {
222  h2trigbits_tfcalVStbcal[dTrigBits[loc_i]]->Fill(bcal_time,fcal_time);
223  h2trigbits_tfcalVSfcal[dTrigBits[loc_i]]->Fill(fcal_energy,fcal_time);
224  h2trigbits_tbcalVSbcal[dTrigBits[loc_i]]->Fill(bcal_energy,bcal_time);
225  }
226  }
227 
228  japp->RootFillUnLock(this); //RELEASE ROOT FILL LOCK
229 
230  return NOERROR;
231 }
232 
233 
234 //----------------------------------------------------------------------------------
235 
236 
238  // This is called whenever the run number changes, before it is
239  // changed to give you a chance to clean up before processing
240  // events from the next run number.
241  return NOERROR;
242 }
243 
244 
245 //----------------------------------------------------------------------------------
246 
247 
249  // Called before program exit after event processing is finished.
250  return NOERROR;
251 }
252 
253 
254 //----------------------------------------------------------------------------------
255 //----------------------------------------------------------------------------------
jerror_t brun(jana::JEventLoop *eventLoop, int32_t runnumber)
Called everytime a new run number is detected.
TVector2 DVector2
Definition: DVector2.h:9
uint32_t trig_mask
Definition: DL1Trigger.h:18
uint32_t fp_trig_mask
Definition: DL1Trigger.h:19
jerror_t evnt(jana::JEventLoop *eventLoop, uint64_t eventnumber)
Called every event.
TH1F * h1trig_trgbits
jerror_t init(void)
Called once at program start.
double rmin
DVector2 positionOnFace(int row, int column) const
JApplication * japp
TH1F * h2trig_fcalVSbcal
Definition: trig_fcalbcal.C:33
jerror_t fini(void)
Called after last event of last event source has been processed.
TH2F * h2trig_tbcalVSbcal
InitPlugin_t InitPlugin
TH2F * h2trig_tfcalVStbcal
TH2F * h2trig_tfcalVSfcal
DFCALGeometry fcalgeom
double sqrt(double)
jerror_t erun(void)
Called everytime run number changes, provided brun has been called.
int main(int argc, char *argv[])
Definition: gendoc.cc:6