Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DEventProcessor_eta_ntuple.cc
Go to the documentation of this file.
1 // $Id: DEventProcessor_eta_ntuple.cc 1816 2006-06-06 14:38:18Z davidl $
2 //
3 // File: DEventProcessor_eta_ntuple.cc
4 // Created: Thur Jan 11 15:42:21 EDT 2005
5 // Creator: davidl
6 //
7 
8 #include <iostream>
9 #include <cmath>
10 using namespace std;
11 
12 #include <TThread.h>
13 #include <TDirectoryFile.h>
14 #include <TLorentzVector.h>
15 
16 #include <JANA/JApplication.h>
17 #include <JANA/JEventLoop.h>
18 using namespace jana;
19 
20 #include <TRACKING/DMCThrown.h>
21 #include <PID/DKinematicData.h>
22 #include <FCAL/DFCALPhoton.h>
23 #include <BCAL/DBCALPhoton.h>
24 #include <START_COUNTER/DSCHit.h>
25 #include <PID/DPhoton.h>
26 #include <PID/DBeamPhoton.h>
27 
29 
30 #include "cern_c.h"
31 #define MEMH 8000000
32 #define LREC 8190 /* record length of hbook direct access file in WORDS */
33 #define LUN 3 /* logical unit number of hbook file */
34 extern "C" {
35  float pawc_[MEMH];
36  int quest_[100];
37 };
38 
39 //==========================================================================
40 // This file contains code for a plugin that will create a ROOT tree and/or
41 // and HBOOK Ntuple for eta primakoff events.
42 //==========================================================================
43 
44 // Routine used to create our JEventProcessor
45 extern "C"{
46 void InitPlugin(JApplication *app){
47  InitJANAPlugin(app);
48  app->AddProcessor(new DEventProcessor_eta_ntuple());
49 }
50 }
51 
52 
53 //------------------
54 // init
55 //------------------
57 {
58  make_hbook = true;
59  make_root = true;
60 
61  gPARMS->SetDefaultParameter("MAKE_HBOOK", make_hbook);
62  gPARMS->SetDefaultParameter("MAKE_ROOT", make_root);
63 
64  evt = new Event();
65 
66  // Create tree
67  if(make_root){
68  tree = new TTree("t", "#eta Primakoff events");
69  tree->Branch("E",&evt);
70  }
71 
72  // Create Ntuple
73  if(make_hbook){
74  // Initialize cernlib (monkey shines!)
75  quest_[9] = 65000;
76  int memh = MEMH;
77  hlimit(memh);
78 
79  // Open HBOOK file for writing
80  string hbook_fname = "eta_ntuple.hbook";
81  hropen(LUN, "lun", hbook_fname.c_str() , "N", LREC, 0);
82  cout<<"Opened "<<hbook_fname<<" for writing..."<<endl;
83 
84  // Create Ntuple
85  hbnt(10,"myeta","");
86  stringstream ntp;
87  ntp<<"event:I";
88  ntp<<",E_beam:R,px_beam,py_beam,pz_beam";
89  ntp<<",E_proton_thrown,px_proton_thrown,py_proton_thrown,pz_proton_thrown";
90  ntp<<",E_eta_thrown,px_eta_thrown,py_eta_thrown,pz_eta_thrown";
91  ntp<<",x,y,z";
92  ntp<<",prod_mech:I,decay_mode:I";
93  ntp<<",Nfcal[0,"<<MAX_PARTS<<"]";
94  ntp<<",E_fcal(Nfcal):R,px_fcal(Nfcal),py_fcal(Nfcal),pz_fcal(Nfcal)";
95  ntp<<",x_fcal(Nfcal),y_fcal(Nfcal),z_fcal(Nfcal)";
96  ntp<<",E_eta_best,px_eta_best,py_eta_best,pz_eta_best";
97  ntp<<",M_eta_best:R";
98  ntp<<",t:R";
99  ntp<<",Nstart[0,"<<MAX_START<<"]:I";
100  ntp<<",phi_start(Nstart):R,phi_start_diff(Nstart)";
101  ntp<<",E_bcal_tot";
102  ntp<<",Nbcal[0,"<<MAX_BCAL<<"]:I";
103  ntp<<",E_bcal(Nbcal):R,phi_bcal(Nbcal),theta_bcal(Nbcal)";
104 
105  hbname(10,"ETANT", &evt_ntuple, ntp.str().c_str());
106  }
107 
108  pthread_mutex_init(&mutex, NULL);
109 
110  return NOERROR;
111 }
112 
113 //------------------
114 // evnt
115 //------------------
116 jerror_t DEventProcessor_eta_ntuple::evnt(JEventLoop *loop, uint64_t eventnumber)
117 {
118  // Get reconstructed objects
119  vector<const DBeamPhoton*> beam_photons;
120  vector<const DFCALPhoton*> fcalphotons;
121  vector<const DBCALPhoton*> bcalphotons;
122  vector<const DMCThrown*> mcthrowns;
123  vector<const DSCHit*> schits;
124 
125  loop->Get(beam_photons); // from truth info
126  loop->Get(fcalphotons); // all reconstructed photons in FCAL
127  loop->Get(bcalphotons); // all reconstructed photons in BCAL
128  loop->Get(mcthrowns); // all thrown particles
129  loop->Get(schits); // all start counter hits
130 
131  // Target is proton at rest in lab frame
132  TLorentzVector target(0.0, 0.0, 0.0, 0.93827);
133 
134  // Create TLorentzVectors for reconstructed FCAL photons
135  vector<TLorentzVector> rec_photons;
136  vector<TVector3> rec_photons_pos;
137  for(unsigned int i=0; i<fcalphotons.size(); i++){
138  rec_photons.push_back(fcalphotons[i]->getMom4());
139  rec_photons_pos.push_back(fcalphotons[i]->getPosition());
140  }
141 
142  // Create TLorentzVectors for reconstructed BCAL photons
143  vector<TLorentzVector> rec_bcal_photons;
144  for(unsigned int i=0; i<bcalphotons.size(); i++){
145  rec_bcal_photons.push_back(bcalphotons[i]->lorentzMomentum());
146  }
147 
148  // Some generators don't supply information on the beam photon. If there
149  // are no DBeamPhoton objects, then punt
150  if(beam_photons.size()!=1){
151  cout<<"Wrong number of DBeamPhoton objects for event "<<eventnumber<<" ("<<beam_photons.size()<<"). Skipping."<<endl;
152  return NOERROR;
153  }
154  TLorentzVector beam_photon = MakeTLorentz(beam_photons[0], 0.0);
155 
156  // Find thrown eta
157  TLorentzVector eta;
158  TVector3 vertex;
159  bool found_eta=false;
160  for(unsigned int i=0; i<mcthrowns.size(); i++){
161  if(mcthrowns[i]->pdgtype == 221){
162  eta = MakeTLorentz(mcthrowns[i], 0.54745);
163  vertex = mcthrowns[i]->position();
164  found_eta = true;
165  break;
166  }
167  }
168  if(!found_eta){
169  cout<<"No thrown eta particle found for event "<<eventnumber<<". Skipping."<<endl;
170  return NOERROR;
171  }
172 
173  // Although we are only filling objects local to this plugin, TTree::Fill() periodically writes to file: Global ROOT lock
174  japp->RootWriteLock(); //ACQUIRE ROOT LOCK
175 
176  evt->Clear();
177  evt->event = eventnumber;
178  evt->beam = beam_photon;
179  evt->eta_thrown = eta;
180  evt->proton_thrown = target+beam_photon-eta; // assumes coherent!
181  evt->vertex = vertex;
182  evt->prod_mech = 0;
183  evt->decay_mode = 0;
184  evt->t = -(beam_photon-eta).M2();
185 
186  // Loop over reconstructed FCAL photons
187  for(unsigned int j=0; j<rec_photons.size(); j++){
188  evt->AddFCAL(rec_photons[j], rec_photons_pos[j]);
189  }
190 
191  // Loop over reconstructed BCAL photons
192  for(unsigned int j=0; j<rec_bcal_photons.size(); j++){
193  evt->AddBCAL(rec_bcal_photons[j]);
194  }
195 
196  // Loop over all 2-gamma combinations keeping the one closes to the eta mass
197  for(unsigned int j=0; j<rec_photons.size(); j++){
198  for(unsigned int k=j+1; k<rec_photons.size(); k++){
199  if(k==j)continue;
200  TLorentzVector my_eta = rec_photons[j] + rec_photons[k];
201  if(fabs(evt->eta_best.M()-0.54745)>fabs(my_eta.M()-0.54745))
202  evt->eta_best = my_eta;
203  }
204  }
205 
206  // Loop over start counter hits
207  for(unsigned int j=0; j<schits.size(); j++){
208  evt->AddSC(schits[j]->sector);
209  }
210 
211  // Fill tree
212  evt->fcal->Sort(); // sort by cluster energy (uses fcal_t::Compare );
213  evt->sc->Sort(); // sort by phi diff (uses sc_t::Compare );
214  evt->bcal->Sort(); // sort by cluster energy (uses bcal_t::Compare );
215 
216  if(make_root)tree->Fill();
217  if(make_hbook)FillNtuple();
218 
219  japp->RootUnLock(); //RELEASE ROOT LOCK
220 
221  return NOERROR;
222 }
223 
224 //------------------
225 // MakeTLorentz
226 //------------------
227 TLorentzVector DEventProcessor_eta_ntuple::MakeTLorentz(const DKinematicData *kd, double mass)
228 {
229  // Create a ROOT TLorentzVector object out of a Hall-D DKinematic Data object.
230  // Here, we have the mass passed in explicitly rather than use the mass contained in
231  // the DKinematicData object itself. This is because right now (Feb. 2009) the
232  // PID code is not mature enough to give reasonable guesses. See above code.
233 
234  double p = kd->momentum().Mag();
235  double theta = kd->momentum().Theta();
236  double phi = kd->momentum().Phi();
237  double px = p*sin(theta)*cos(phi);
238  double py = p*sin(theta)*sin(phi);
239  double pz = p*cos(theta);
240  double E = sqrt(mass*mass + p*p);
241 
242  return TLorentzVector(px,py,pz,E);
243 }
244 
245 //------------------
246 // FillNtuple
247 //------------------
249 {
250  // Use values in the member "evt" to fill values
251  // in the member "evt_ntuple".
252  evt_ntuple.event = evt->event;
253  evt_ntuple.E_beam = evt->beam.E();
254  evt_ntuple.px_beam = evt->beam.Px();
255  evt_ntuple.py_beam = evt->beam.Py();
256  evt_ntuple.pz_beam = evt->beam.Pz();
257  evt_ntuple.E_proton_thrown = evt->proton_thrown.E();
258  evt_ntuple.px_proton_thrown = evt->proton_thrown.Px();
259  evt_ntuple.py_proton_thrown = evt->proton_thrown.Py();
260  evt_ntuple.pz_proton_thrown = evt->proton_thrown.Pz();
261  evt_ntuple.E_eta_thrown = evt->eta_thrown.E();
262  evt_ntuple.px_eta_thrown = evt->eta_thrown.Px();
263  evt_ntuple.py_eta_thrown = evt->eta_thrown.Py();
264  evt_ntuple.pz_eta_thrown = evt->eta_thrown.Pz();
265  evt_ntuple.x = evt->vertex.X();
266  evt_ntuple.y = evt->vertex.Y();
267  evt_ntuple.z = evt->vertex.Z();
268  evt_ntuple.prod_mech = evt->prod_mech;
269  evt_ntuple.decay_mode = evt->decay_mode;
270  evt_ntuple.Nfcal = evt->Nfcal;
271  evt_ntuple.E_eta_best = evt->eta_best.E();
272  evt_ntuple.px_eta_best = evt->eta_best.Px();
273  evt_ntuple.py_eta_best = evt->eta_best.Py();
274  evt_ntuple.pz_eta_best = evt->eta_best.Pz();
275  evt_ntuple.M_eta_best = evt->eta_best.M();
276  evt_ntuple.t = evt->t;
277  evt_ntuple.Nstart = evt->Nstart;
278  evt_ntuple.E_bcal_tot = evt->E_bcal_tot;
279  evt_ntuple.Nfcal = evt->Nfcal;
280 
281  // FCAL
282  if(evt_ntuple.Nfcal>=MAX_PARTS)evt_ntuple.Nfcal=MAX_PARTS-1;
283  for(UInt_t i=0; i<(UInt_t)evt_ntuple.Nfcal; i++){
284  fcal_t *fcal = dynamic_cast<fcal_t*>((*evt->fcal)[i]);
285  if(!fcal){
286  _DBG_<<"dynamic cast of TClonesArray element "<<i<<" failed!!"<<endl;
287  return;
288  }
289 
290  evt_ntuple.E_fcal[i] = fcal->p.E();
291  evt_ntuple.px_fcal[i] = fcal->p.Px();
292  evt_ntuple.py_fcal[i] = fcal->p.Py();
293  evt_ntuple.pz_fcal[i] = fcal->p.Pz();
294 
295  evt_ntuple.x_fcal[i] = fcal->x.X();
296  evt_ntuple.y_fcal[i] = fcal->x.Y();
297  evt_ntuple.z_fcal[i] = fcal->x.Z();
298  }
299 
300  // Start counter
301  if(evt_ntuple.Nstart>=MAX_START)evt_ntuple.Nstart=MAX_START-1;
302  for(UInt_t i=0; i<(UInt_t)evt_ntuple.Nstart; i++){
303  sc_t *sc = dynamic_cast<sc_t*>((*evt->sc)[i]);
304  if(!sc){
305  _DBG_<<"dynamic cast of TClonesArray element "<<i<<" failed!!"<<endl;
306  return;
307  }
308 
309  evt_ntuple.phi_start[i] = sc->phi_center;
310  evt_ntuple.phi_start_diff[i] = sc->phi_diff;
311  }
312 
313  // BCAL
314  if(evt_ntuple.Nbcal>=MAX_BCAL)evt_ntuple.Nbcal=MAX_BCAL-1;
315  for(UInt_t i=0; i<(UInt_t)evt_ntuple.Nbcal; i++){
316  bcal_t *bcal = dynamic_cast<bcal_t*>((*evt->bcal)[i]);
317  if(!bcal){
318  _DBG_<<"dynamic cast of TClonesArray element "<<i<<" failed!!"<<endl;
319  return;
320  }
321 
322  evt_ntuple.E_bcal[i] = bcal->p.E();
323  evt_ntuple.phi_bcal[i] = bcal->p.Phi();
324  evt_ntuple.theta_bcal[i] = bcal->p.Theta();
325  }
326 
327  // Add event to Ntuple
328  hfnt(10);
329 }
330 
331 //------------------
332 // erun
333 //------------------
335 {
336 
337  return NOERROR;
338 }
339 
340 //------------------
341 // fini
342 //------------------
344 {
345  if(make_hbook){
346  // Close hbook file
347  int icycle=0;
348  hrout(0,icycle,"T");
349  hrend("lun");
350  }
351 
352 
353  return NOERROR;
354 }
void hbname(int id, const char *CHBLOK, void *VAR, const char *CHFORM)
float phi_diff
Definition: sc_t.h:25
void hropen(int lun, char *name, char *filename, char *status, int stor, int istat)
#define LREC
void hfnt(int id)
void hlimit(int size)
TLorentzVector MakeTLorentz(const DKinematicData *track, double mass)
void hbnt(int id, char *CHTITL, char *CHOPT)
Definition: sc_t.h:19
int quest_[100]
JApplication * japp
TLorentzVector p
Definition: fcal_t.h:24
Definition: fcal_t.h:19
jerror_t evnt(JEventLoop *loop, uint64_t eventnumber)
Invoked via DEventProcessor virtual method.
jerror_t fini(void)
Invoked via DEventProcessor virtual method.
InitPlugin_t InitPlugin
void hrout(int num, int icycle, char *opt)
Definition: bcal_t.h:19
#define MAX_BCAL
#define MAX_START
#define _DBG_
Definition: HDEVIO.h:12
float pawc_[MEMH]
void hrend(char *filename)
TH1D * py[NCHANNELS]
jerror_t erun(void)
Invoked via DEventProcessor virtual method.
double sqrt(double)
double sin(double)
jerror_t init(void)
Invoked via DEventProcessor virtual method.
TVector3 x
Definition: fcal_t.h:25
const DVector3 & momentum(void) const
float phi_center
Definition: sc_t.h:24
#define MAX_PARTS
TLorentzVector p
Definition: bcal_t.h:24
#define MEMH