Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DEventProcessor_BCAL_gainmatrix.cc
Go to the documentation of this file.
1 // $Id$
2 //
3 // File: DEventProcessor_BCAL_gainmatrix.cc
4 // Created: Fri Oct 10 16:41:18 EDT 2014
5 // Creator: wmcginle (on Linux ifarm1101 2.6.32-220.7.1.el6.x86_64 x86_64)
6 //
7 
9 
10 #include <TLorentzVector.h>
11 #include "TMath.h"
12 
13 #include "DANA/DApplication.h"
14 #include "BCAL/DBCALShower.h"
15 #include "BCAL/DBCALCluster.h"
16 #include "BCAL/DBCALPoint.h"
17 #include "PID/DVertex.h"
18 #include "TRIGGER/DTrigger.h"
19 
20 #include <vector>
21 #include <deque>
22 #include <string>
23 #include <iostream>
24 #include <algorithm>
25 #include <stdio.h>
26 #include <stdlib.h>
27 
28 // Routine used to create our DEventProcessor
29 
30 extern "C"
31 {
32  void InitPlugin(JApplication *locApplication)
33  {
34  InitJANAPlugin(locApplication);
35  locApplication->AddProcessor(new DEventProcessor_BCAL_gainmatrix()); //register this plugin
36  }
37 } // "C"
38 
39 //------------------
40 // init
41 //------------------
43 {
44  // This is called once at program startup. If you are creating
45  // and filling historgrams in this plugin, you should lock the
46  // ROOT mutex like this:
47 
48  // ... create historgrams or trees ...
49 
50  int n_channels = 768;
51  int m_nElements = 768;
52 
53  h2D_mC = new TH2F("h2D_mC", "C Matrix as TH2F ", n_channels, 0.0, n_channels, n_channels, 0.0, n_channels);
54  h1D_mD = new TH1F("h1D_mD", "D Matrix as TH1F", n_channels, 0.0, n_channels);
55  h1D_mL = new TH1F("h1D_mL", " L Matrix as TH1F", n_channels, 0.0, n_channels);
56  h1D_massbias = new TH1F( "h1D_massbias", "Mass Bias Value (in bin 2)",5,0.0,5.0);
57  h1D_nhits = new TH1F("h1D_nhits", "Number of hits as function of channel number", n_channels,0,n_channels);
58  h1D_nhits->SetXTitle("channel number");
59  h1D_nhits->SetYTitle("counts");
60 
61  m_mD.ResizeTo(m_nElements,1);
62  m_mC.ResizeTo(m_nElements,m_nElements);
63  m_mL.ResizeTo(m_nElements,1);
64  m_nhits.ResizeTo(m_nElements,1);
65  m_mD.Zero();
66  m_mL.Zero();
67  m_mC.Zero();
68  m_nhits.Zero();
69 
70  m_massbias = 0.0;
71  BCAL_Neutrals = new TTree("BCALNeutrals","BCALNeutrals");
72  BCAL_Neutrals->Branch("eventnum",&eventnum,"eventnum/i");
73 // BCAL_Neutrals->Branch("sh1_E",&E1);
74  BCAL_Neutrals->Branch("sh1_E_raw",&E1_raw);
75 // BCAL_Neutrals->Branch("sh2_E",&E2);
76  BCAL_Neutrals->Branch("sh2_E_raw",&E2_raw);
77 // BCAL_Neutrals->Branch("sh1_t",&t1);
78 // BCAL_Neutrals->Branch("sh2_t",&t2);
79 // BCAL_Neutrals->Branch("inv_mass",&inv_mass);
80  BCAL_Neutrals->Branch("inv_mass_raw",&inv_mass_raw);
81  BCAL_Neutrals->Branch("sh1_z",&z1);
82  BCAL_Neutrals->Branch("sh2_z",&z2);
83 // BCAL_Neutrals->Branch("sh1_x",&x1);
84 // BCAL_Neutrals->Branch("sh2_x",&x2);
85 // BCAL_Neutrals->Branch("sh1_y",&y1);
86 // BCAL_Neutrals->Branch("sh2_y",&y2);
87  BCAL_Neutrals->Branch("psi",&psi);
88  BCAL_Neutrals->Branch("cl1_contains_layer4",&logical1);
89  BCAL_Neutrals->Branch("cl2_contains_layer4",&logical2);
90  BCAL_Neutrals->Branch("vertexZ",&vertexz);
91  BCAL_Neutrals->Branch("num_showers",&num_showers,"num_showers/i");
92  BCAL_Neutrals->Branch("num_tracks",&num_tracks,"num_tracks/i");
93  BCAL_Neutrals->Branch("Run_Number", &Run_Number);
94 
95  dEventWriterROOT = NULL;
96  dEventWriterREST = NULL;
97 
98  return NOERROR;
99 }
100 
101 
102 //------------------
103 // brun
104 //------------------
105 jerror_t DEventProcessor_BCAL_gainmatrix::brun(jana::JEventLoop* locEventLoop, int32_t locRunNumber)
106 {
107  // This is called whenever the run number changes
108  Run_Number = locRunNumber;
109  /*
110  //Optional: Retrieve REST writer for writing out skims
111  locEventLoop->GetSingle(dEventWriterREST);
112 
113  //Recommeded: Create output ROOT TTrees (nothing is done if already created)
114  locEventLoop->GetSingle(dEventWriterROOT);
115  dEventWriterROOT->Create_DataTrees(locEventLoop);
116  */
117 
118  return NOERROR;
119 }
120 
121 //------------------
122 // evnt
123 //------------------
124 
125 
126 
127 
128 jerror_t DEventProcessor_BCAL_gainmatrix::evnt(jana::JEventLoop* locEventLoop, uint64_t locEventNumber)
129 {
130 
131  eventnum = locEventNumber;
132  // This is called for every event. Use of common resources like writing
133  // to a file or filling a histogram should be mutex protected. Using
134  // locEventLoop->Get(...) to get reconstructed objects (and thereby activating the
135  // reconstruction algorithm) should be done outside of any mutex lock
136  // since multiple threads may call this method at the same time.
137  //
138  // Here's an example:
139  //
140  // vector<const MyDataClass*> mydataclasses;
141  // locEventLoop->Get(mydataclasses);
142  //
143  // japp->RootWriteLock();
144  // ... fill historgrams or trees ...
145  // japp->RootUnLock();
146 
147  // DOCUMENTATION:
148  // ANALYSIS library: https://halldweb1.jlab.org/wiki/index.php/GlueX_Analysis_Software
149 
150  vector<const DTrackFitter *> fitters;
151  locEventLoop->Get(fitters);
152 
153  if(fitters.size()<1){
154  _DBG_<<"Unable to get a DTrackFinder object!"<<endl;
155  return RESOURCE_UNAVAILABLE;
156  }
157 
158  const DTrackFitter *fitter = fitters[0];
159 
160 
161  // select events with physics events, i.e., not LED and other front panel triggers
162  const DTrigger* locTrigger = NULL;
163  locEventLoop->GetSingle(locTrigger);
164  if(locTrigger->Get_L1FrontPanelTriggerBits() != 0)
165  return NOERROR;
166 
167  vector<const DBCALShower*> locBCALShowers;
168  vector<const DBCALCluster*> locBCALClusters;
169  vector<const DBCALPoint*> locBCALPoints;
170  vector<const DVertex*> locVertex;
171  locEventLoop->Get(locBCALShowers);
172  locEventLoop->Get(locBCALClusters);
173  locEventLoop->Get(locBCALPoints);
174  locEventLoop->Get(locVertex);
175 
176  vector<const DTrackTimeBased*> locTrackTimeBased;
177  locEventLoop->Get(locTrackTimeBased);
178 
179  num_tracks = locTrackTimeBased.size();
180  num_showers = locBCALShowers.size();
181 
182  vector <const DBCALShower *> matchedShowers;
183  DVector3 mypos(0.0,0.0,0.0);
184  for (unsigned int i=0; i < locTrackTimeBased.size() ; ++i){
185  for (unsigned int j=0; j< locBCALShowers.size(); ++j){
186  double x = locBCALShowers[j]->x;
187  double y = locBCALShowers[j]->y;
188  double z = locBCALShowers[j]->z;
189  DVector3 pos_bcal(x,y,z);
190  double R = pos_bcal.Perp();
191  vector<DTrackFitter::Extrapolation_t>extrapolations=locTrackTimeBased[i]->extrapolations.at(SYS_BCAL);
192  if (fitter->ExtrapolateToRadius(R,extrapolations,mypos)){
193  //double dPhi = TMath::Abs(mypos.Phi()-pos_bcal.Phi());
194  double dPhi=mypos.Phi()-pos_bcal.Phi();
195  if (dPhi<-M_PI) dPhi+=2.*M_PI;
196  if (dPhi>M_PI) dPhi-=2.*M_PI;
197  double dZ = TMath::Abs(mypos.Z() - z);
198  if(dZ < 30.0 && fabs(dPhi) < 0.18) matchedShowers.push_back(locBCALShowers[j]);
199  }
200  }
201  }
202 
203  double vertexX = 0.0;
204  double vertexY = 0.0;
205  double vertexZ = 0.0;
206  for (unsigned int i = 0 ; i < locVertex.size(); i++)
207  {
208  vertexX = locVertex[i]->dSpacetimeVertex.X();
209  vertexY = locVertex[i]->dSpacetimeVertex.Y();
210  vertexZ = locVertex[i]->dSpacetimeVertex.Z();
211  }
212 
213 //OoOoOoOoOoOoOoOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO CALCULATING THE PI0 INVARIANT MASS PORTION O0O0O0O0O0O0O0O0OOooooooooooOooOoOoooooOOooooOOo
214 
215  // Although we are only filling objects local to this plugin, TTree::Fill() periodically writes to file: Global ROOT lock
216  japp->RootWriteLock();
217 
218  for(unsigned int i=0; i<locBCALShowers.size(); i++){
219  double pi0_mass = 0.1349766;
220  if (find(matchedShowers.begin(), matchedShowers.end(),locBCALShowers[i]) != matchedShowers.end()) continue;
221  const DBCALShower *s1 = locBCALShowers[i];
222  vector<const DBCALPoint*> associated_points1;
223  s1->Get(associated_points1);
224  double dx1 = s1->x - vertexX;
225  double dy1 = s1->y - vertexY;
226  double dz1 = s1->z - vertexZ;
227  double dt1 = s1->t;
228  t1 = dt1;
229  z1 = s1->z;
230  x1 = s1->x;
231  y1 = s1->y;
232  double R1 = sqrt(dx1*dx1 + dy1*dy1 + dz1*dz1);
233  E1 = s1->E;
234  E1_raw = s1->E_raw;
235  TLorentzVector sh1_p(E1*dx1/R1, E1*dy1/R1, E1*dz1/R1, E1);
236  TLorentzVector sh1_p_raw(E1_raw*dx1/R1, E1_raw*dy1/R1, E1_raw*dz1/R1, E1_raw);
237  vertexz = vertexZ;
238  for(unsigned int j=i+1; j<locBCALShowers.size(); j++){
239  const DBCALShower *s2 = locBCALShowers[j];
240  if (find(matchedShowers.begin(), matchedShowers.end(),s2) != matchedShowers.end()) continue;
241  vector<const DBCALPoint*> associated_points2;
242  s2->Get(associated_points2);
243  double dx2 = s2->x - vertexX;
244  double dy2 = s2->y - vertexY;
245  double dz2 = s2->z - vertexZ; // shift to coordinate relative to center of target
246  double dt2 = s2->t;
247  t2=dt2;
248  z2 = s2->z;
249  x2 = s2->x;
250  y2 = s2->y;
251  double R2 = sqrt(dx2*dx2 + dy2*dy2 + dz2*dz2);
252  E2 = s2->E;
253  E2_raw = s2->E_raw;
254  TLorentzVector sh2_p(E2*dx2/R2, E2*dy2/R2, E2*dz2/R2, E2);
255  TLorentzVector sh2_p_raw(E2_raw*dx2/R2, E2_raw*dy2/R2, E2_raw*dz2/R2, E2_raw);
256  double cospsi = (dx1*dx2+dy1*dy2+dz1*dz2)/R1/R2;
257  psi=acos(cospsi);
258  TLorentzVector ptot = sh1_p+sh2_p;
259  TLorentzVector ptot_raw = sh1_p_raw + sh2_p_raw ;
260  inv_mass = ptot.M();
261  inv_mass_raw = ptot_raw.M();
262  point1_energy_calib.clear();
263  point2_energy_calib.clear();
264  frac_en.clear();
265  point1_channel.clear();
266  point2_channel.clear();
267  point_energy.clear();
268  logical1=0;
269  logical2=0;
270  for(unsigned int loc_k = 0; loc_k < associated_points1.size(); loc_k++){
271  int module1 = associated_points1[loc_k]->module();
272  int layer1 = associated_points1[loc_k]->layer();
273  int sector1 = associated_points1[loc_k]->sector();
274  double energy1_US = associated_points1[loc_k]->E_US();
275  double energy1_DS = associated_points1[loc_k]->E_DS();
276  int channel1 = 16*(module1-1)+4*(layer1-1)+(sector1-1);
277  if(layer1==4) logical1=1;
278  point1_energy_calib.push_back(sqrt(energy1_US*energy1_DS/E1_raw/E1_raw));
279  point1_channel.push_back(channel1);
280  for(unsigned int loc_p = 0 ; loc_p < associated_points2.size(); loc_p++){
281  int module2 = associated_points2[loc_p]->module();
282  int layer2 = associated_points2[loc_p]->layer();
283  int sector2 = associated_points2[loc_p]->sector();
284  double energy2_US = associated_points2[loc_p]->E_US();
285  double energy2_DS = associated_points2[loc_p]->E_DS();
286  int channel2 = 16*(module2-1)+4*(layer2-1)+(sector2-1);
287  if(layer2==4) logical2=1;
288  point2_energy_calib.push_back(sqrt(energy2_US*energy2_DS/E2_raw/E2_raw));
289  point2_channel.push_back(channel2);
290  }
291  }
292  for ( unsigned int f = 0 ; f < point1_energy_calib.size() ; ++f){
293  frac_en.push_back(make_pair(point1_energy_calib[f],point1_channel[f]));
294  }
295 
296  for ( unsigned int h = 0 ; h < point2_energy_calib.size() ; ++h){
297  frac_en.push_back(make_pair(point2_energy_calib[h],point2_channel[h]));
298  }
299 
300  for (unsigned int a = 0 ; a < frac_en.size() ; ++a){
301  point_energy.push_back(frac_en[a].first);
302  if(inv_mass_raw>0.12&&inv_mass_raw<.15&&E1_raw>1.&&E2_raw>1.&&num_tracks>0 &&frac_en[a].first > 0.0 && frac_en[a].first < 30.0) m_mD[frac_en[a].second][0] += -(inv_mass_raw*inv_mass_raw - pi0_mass*pi0_mass)*(inv_mass_raw*inv_mass_raw)*frac_en[a].first;
303  if(inv_mass_raw>0.12&&inv_mass_raw<.15&&E1_raw>1.&&E2_raw>1.&&num_tracks>0 &&frac_en[a].first > 0.0 && frac_en[a].first < 30.0 ) m_mL[frac_en[a].second][0] += (inv_mass_raw*inv_mass_raw)*frac_en[a].first;
304  if(inv_mass_raw > 0.12 && inv_mass_raw <.15 && E1_raw>1.&&E2_raw>1.&&num_tracks>0 && frac_en[a].first > 0.0 && frac_en[a].first < 30.0 ) m_nhits[frac_en[a].second] += 1;
305 
306  for(unsigned int b = 0 ; b < frac_en.size(); ++b)
307  {
308  if(inv_mass_raw>.12&&inv_mass_raw<.15&&E1_raw>1.&&E2_raw>1.&&num_tracks>0 && frac_en[a].first > 0.0 && frac_en[a].first < 30.0&& frac_en[b].first > 0.0 && frac_en[b].first < 30.0 ) m_mC[frac_en[a].second][frac_en[b].second] += (inv_mass_raw*inv_mass_raw*inv_mass_raw*inv_mass_raw)*(frac_en[a].first)*(frac_en[b].first);
309  }
310  }
311  for(unsigned int h=0;h<point_energy.size();h++){
312  if(inv_mass_raw>.12&&inv_mass_raw<.15&&E1_raw>1.&&E2_raw>1.&&num_tracks>0 && frac_en[h].first > 0.0 && frac_en[h].first < 30.0 ) m_massbias += (inv_mass_raw*inv_mass_raw - pi0_mass*pi0_mass);
313  }
314  BCAL_Neutrals->Fill();
315  }
316 
317  }
318 
319  japp->RootUnLock();
320 
321  return NOERROR;
322 }
323 
324 //------------------
325 // erun
326 //------------------
328 {
329  // This is called whenever the run number changes, before it is
330  // changed to give you a chance to clean up before processing
331  // events from the next run number.
332 
333  // FILL HISTOGRAMS
334  // Since we are filling histograms local to this plugin, it will not interfere with other ROOT operations: can use plugin-wide ROOT fill lock
335  japp->RootFillLock(this); //ACQUIRE ROOT FILL LOCK
336 
337  int n_channels = 768;
338 
339  h1D_nhits = new TH1F("h1D_nhits", "Number of hits as function of channel number", n_channels,0,767);
340  h1D_nhits->SetYTitle("Number of Hits");
341  h1D_massbias->SetBinContent(2,m_massbias);
342 
343  for(int i=0; i<n_channels; i++){
344  h1D_mD->SetBinContent(i+1,m_mD[i][0]);
345  h1D_mL->SetBinContent(i+1,m_mL[i][0]);
346  h1D_nhits->SetBinContent(i+1,m_nhits[i][0]);
347 
348 
349  for(int j=0; j<n_channels; j++){
350  h2D_mC->SetBinContent(i+1,j+1,m_mC[i][j]);
351  }
352  }
353 
354  japp->RootFillUnLock(this); //RELEASE ROOT FILL LOCK
355  return NOERROR;
356 }
357 
358 //------------------
359 // fini
360 //------------------
362 {
363  // Called before program exit after event processing is finished.
364  return NOERROR;
365 }
366 
bool ExtrapolateToRadius(double R, const vector< Extrapolation_t > &extraps, DVector3 &pos, DVector3 &mom, double &t, double &s) const
jerror_t brun(jana::JEventLoop *locEventLoop, int32_t locRunNumber)
Called every time a new run number is detected.
The DTrackFitter class is a base class for different charged track fitting algorithms. It does not actually fit the track itself, but provides the interface and some common support features most algorthims will need to implement.
Definition: DTrackFitter.h:61
TVector3 DVector3
Definition: DVector3.h:14
Double_t x[NCHANNELS]
Definition: st_tw_resols.C:39
#define y
uint32_t Get_L1FrontPanelTriggerBits(void) const
jerror_t erun(void)
Called every time run number changes, provided brun has been called.
Definition: GlueX.h:19
JApplication * japp
jerror_t init(void)
Called once at program start.
TF1 * f
Definition: FitGains.C:21
jerror_t fini(void)
Called after last event of last event source has been processed.
InitPlugin_t InitPlugin
#define _DBG_
Definition: HDEVIO.h:12
jerror_t evnt(jana::JEventLoop *locEventLoop, uint64_t locEventNumber)
Called every event.
double sqrt(double)
char logical1
Definition: grkuta.cc:48
const DTrackFitter * fitter
float E_raw
Definition: DBCALShower.h:18