Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JEventProcessor_PS_E_calib.cc
Go to the documentation of this file.
1 // $Id$
2 // // File: JEventProcessor_PS_E_calib.cc // Created: Thu Jul 9 17:44:32 EDT 5015
3 // Creator: aebarnes (on Linux ifarm1401 2.6.32-431.el6.x86_64 x86_64)
4 //
5 
7 using namespace jana;
8 
9 #include <iostream>
10 #include <iomanip>
11 #include <sstream>
12 #include <math.h>
13 
14 #include <TTree.h>
15 #include <TBranch.h>
16 #include <TDirectory.h>
17 #include <TProfile.h>
18 #include <TH2.h>
19 
20 #include <TAGGER/DTAGMHit.h>
21 #include <TAGGER/DTAGHHit.h>
22 #include <TAGGER/DTAGMGeometry.h>
25 
26 #define CORRECTIONS false
27 
28 // Define constants
29 const float Ebw_PS = 0.013; // Energy bin width for the PS in GeV
30 const float Ebl_PS = 2.3; // Low energy of the PS total energy in GeV
31 const float Ebh_PS = 4.9; // High energy of the PS total energy in GeV
32 const float NEb_PS = (Ebh_PS - Ebl_PS)/Ebw_PS; // Number of energy bins for the PS
33 
34 const int MAX_COLUMNS = 102; // Total columns in the TAGM
35 const int MAX_COUNTERS = 274; // Total possible counters in the TAGH
36 
37 // Declare variables
38 double p0 = 0; // PS energy correction parameter
39 double p1 = 0; // PS energy correction parameter
40 double p2 = 0; // PS energy correction parameter
41 // TAGM
42 int column = 0; // TAGM column
43 double tm_E = 0; // TAGM energy
44 double tm_t = 0; // TAGM time
45 double tdiff_tm = 0; // time difference PS avg - TAGM
46 // TAGH
47 int counter = 0; // TAGH counter
48 double th_E = 0; // TAGH energy
49 double th_t = 0; // TAGH time
50 double tdiff_th = 0; // time difference PS avg - TAGH
51 // PS
52 double ps_E = 0; // total PS energy
53 double ps_El = 0; // left PS arm energy
54 double ps_Er = 0; // right PS arm energy
55 double ps_t = 0; // average PS time
56 double ps_tl = 0; // left PS time
57 double ps_tr = 0; // right PS time
58 
59 // This is not correct. Fix this when the timing is aligned
60 int run = 0; // Run number
61 
62 // Declare histograms
63 // TAGM
64 static TH2F *h_psE_vs_psEl_tm[MAX_COLUMNS]; // PS total vs fraction of PS left
65 // TAGH
66 static TH2F *h_psE_vs_psEl_th[MAX_COUNTERS]; // PS total vs fraction of PS left
67 // Timing check
68 static TH1F *h_dt; // PS - TAGX timing check
69 
70 // Routine used to create our JEventProcessor
71 #include <JANA/JApplication.h>
72 #include <JANA/JFactory.h>
73 extern "C"{
74 void InitPlugin(JApplication *app){
75  InitJANAPlugin(app);
76  app->AddProcessor(new JEventProcessor_PS_E_calib());
77 }
78 } // "C"
79 
80 
81 //------------------
82 // JEventProcessor_PS_E_calib (Constructor)
83 //------------------
85 {
86 
87 }
88 
89 //------------------
90 // ~JEventProcessor_PS_E_calib (Destructor)
91 //------------------
93 {
94 
95 }
96 
97 //------------------
98 // init
99 //------------------
101 {
102  // This is called once at program startup. If you are creating
103  // and filling historgrams in this plugin, you should lock the
104  // ROOT mutex like this:
105  //
106  // japp->RootWriteLock();
107  // ... fill historgrams or trees ...
108  // japp->RootUnLock();
109  //
110 
111  // create root folder tagm
112  TDirectory *mainDir = gDirectory;
113  TDirectory *tagmDir = gDirectory->mkdir("TAGM");
114  TDirectory *taghDir = gDirectory->mkdir("TAGH");
115 
116  // Book 2d histograms
117  // TAGM
118  tagmDir->cd();
119 
120  h_dt = new TH1F("h_dt","Time difference PS - TAGM;PS-TAGM (ns)",200,-17,37);
121 
122  for (int col = 0; col < MAX_COLUMNS; ++col) {
123  h_psE_vs_psEl_tm[col] = new TH2F(Form("h_psE_vs_psEl_tm_%i",col+1),
124  Form("PS E vs PS left, TAGM col %i;\
125  Energy asymmetry;PS energy (GeV)",col+1),
126  50,0,1,NEb_PS,Ebl_PS,Ebh_PS);
127  }
128 
129  // TAGH
130  taghDir->cd();
131 
132  for (int hodo = 0; hodo < MAX_COUNTERS; ++hodo) {
133  h_psE_vs_psEl_th[hodo] = new TH2F(Form("psE_vs_psEl_th_%i",hodo+1),
134  Form("PS E vs PS left, TAGH counter %i;\
135  Energy asymmetry;PS energy (GeV)",hodo+1),
136  50,0,1,NEb_PS,Ebl_PS,Ebh_PS);
137  }
138 
139 
140  mainDir->cd();
141  return NOERROR;
142 }
143 
144 //------------------
145 // brun
146 //------------------
147 jerror_t JEventProcessor_PS_E_calib::brun(JEventLoop *eventLoop, int32_t runnumber)
148 {
149  // This is called whenever the run number changes
150 
151  // THIS IS TEMPORARY! FIX ONCE PS-TAGX COINCIDENCE
152  // IS THE SAME FOR ALL RUNS
153  run = runnumber;
154 
155  // Get the PS energy corrections from CCDB
156  std::vector< std::map<std::string, double> > table;
157  std::string ccdb_key = "/PHOTON_BEAM/pair_spectrometer/fine/energy_corrections";
158  if (eventLoop->GetCalib(ccdb_key, table)) {
159  jout << "Error loading " << ccdb_key << " from ccdb!" << std::endl;
160  }
161  for (unsigned int i=0; i < table.size(); ++i) {
162  p0 = (table[i])["constant"];
163  p1 = (table[i])["linear"];
164  p2 = (table[i])["quadratic"];
165  }
166 
167  return NOERROR;
168 }
169 
170 //------------------
171 // evnt
172 //------------------
173 jerror_t JEventProcessor_PS_E_calib::evnt(JEventLoop *loop, uint64_t eventnumber)
174 {
175  // This is called for every event. Use of common resources like writing
176  // to a file or filling a histogram should be mutex protected. Using
177  // loop->Get(...) to get reconstructed objects (and thereby activating the
178  // reconstruction algorithm) should be done outside of any mutex lock
179  // since multiple threads may call this method at the same time.
180  // Here's an example:
181  //
182  // vector<const MyDataClass*> mydataclasses;
183  // loop->Get(mydataclasses);
184  //
185  // japp->RootWriteLock();
186  // ... fill historgrams or trees ...
187  // japp->RootUnLock();
188 
189  vector<const DTAGMHit*> tm_hits;
190  vector<const DTAGHHit*> th_hits;
191  vector<const DPSPair*> ps_pairs;
192  vector<const DPSCPair*> psc_pairs;
193 
194  loop->Get(tm_hits);
195  loop->Get(th_hits);
196  loop->Get(ps_pairs);
197  loop->Get(psc_pairs);
198 
199  // FILL HISTOGRAMS
200  // Since we are filling histograms local to this plugin, it will not interfere with other ROOT operations: can use plugin-wide ROOT fill lock
201  japp->RootFillLock(this); //ACQUIRE ROOT FILL LOCK
202 
203  if (psc_pairs.size() > 0 ) {
204  for (uint32_t i = 0; i < ps_pairs.size(); ++i) {
205  // left arm
206  ps_El = ps_pairs[i]->ee.first->E;
207  ps_tl = ps_pairs[i]->ee.first->t;
208  // right arm
209  ps_Er = ps_pairs[i]->ee.second->E;
210  ps_tr = ps_pairs[i]->ee.second->t;
211  // combined
212  ps_t = (ps_tl + ps_tr)/2;
213  ps_E = ps_El + ps_Er;
214 
215  #if CORRECTIONS
216  ps_E /= p0 + p1*(ps_El/ps_E) + p2*(ps_El/ps_E)*(ps_El/ps_E);
217  #endif
218 
219  // loop over TAGM hits
220  for (uint32_t j = 0; j < tm_hits.size(); ++j) {
221  tm_E = tm_hits[j]->E;
222  tm_t = tm_hits[j]->t;
223  tdiff_tm = ps_t - tm_t;
224  column = tm_hits[j]->column;
225 
226  if (!tm_hits[j]->has_fADC || !tm_hits[j]->has_TDC) continue;
227  if (tm_hits[j]->row != 0) continue;
228 
229  h_dt->Fill(tdiff_tm);
230 
231  if (run == 3180 && tdiff_tm < 26.5 && tdiff_tm >= 22.5) {
233  }
234  else if (run == 3185 && tdiff_tm < 34.5 && tdiff_tm >= 30.5) {
236  }
237  }
238 
239  // loop over TAGH hits
240  for (uint32_t j = 0; j < th_hits.size(); ++j) {
241  th_E = th_hits[j]->E;
242  th_t = th_hits[j]->t;
243  tdiff_th = ps_t - th_t;
244  counter = th_hits[j]->counter_id;
245 
246  if (!th_hits[j]->has_fADC || !th_hits[j]->has_TDC) continue;
247 
248  if (run == 3180 && tdiff_th < 26.5 && tdiff_th >= 22.5) {
250  }
251  else if (run == 3185 && tdiff_th < 34.5 && tdiff_th >= 30.5) {
253  }
254  }
255  }
256  }
257 
258  japp->RootFillUnLock(this); //RELEASE ROOT FILL LOCK
259 
260  return NOERROR;
261 }
262 
263 //------------------
264 // erun
265 //------------------
267 {
268  // This is called whenever the run number changes, before it is
269  // changed to give you a chance to clean up before processing
270  // events from the next run number.
271  return NOERROR;
272 }
273 
274 //------------------
275 // fini
276 //------------------
278 {
279  // Called before program exit after event processing is finished.
280  return NOERROR;
281 }
282 
static TH1F * h_dt
jerror_t brun(jana::JEventLoop *eventLoop, int32_t runnumber)
Called everytime a new run number is detected.
jerror_t evnt(jana::JEventLoop *eventLoop, uint64_t eventnumber)
Called every event.
const float Ebw_PS
jerror_t fini(void)
Called after last event of last event source has been processed.
char string[256]
TDirectory * mainDir
Definition: p2k_hists.C:2
jerror_t init(void)
Called once at program start.
JApplication * japp
double tdiff_tm
double counter
Definition: FitGains.C:151
InitPlugin_t InitPlugin
const float Ebl_PS
jerror_t erun(void)
Called everytime run number changes, provided brun has been called.
double tdiff_th
const float Ebh_PS
static TH2F * h_psE_vs_psEl_th[MAX_COUNTERS]
const float NEb_PS
const int MAX_COUNTERS
static TH2F * h_psE_vs_psEl_tm[MAX_COLUMNS]
const int MAX_COLUMNS