Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DEventProcessor_radlen_hists.cc
Go to the documentation of this file.
1 // $Id: DEventProcessor_radlen_hists.cc 1816 2006-06-06 14:38:18Z davidl $
2 //
3 // File: DEventProcessor_radlen_hists.cc
4 // Created: Sun Apr 24 06:45:21 EDT 2005
5 // Creator: davidl (on Darwin Harriet.local 7.8.0 powerpc)
6 //
7 
8 #include <iostream>
9 using namespace std;
10 
11 #include <TThread.h>
12 
13 #include <JANA/JApplication.h>
14 #include <JANA/JEventLoop.h>
15 using namespace jana;
16 
17 extern JApplication *japp;
18 
19 #include <DANA/DApplication.h>
20 
23 #include "TRACKING/DMCThrown.h"
24 
25 // The executable should define the ROOTfile global variable. It will
26 // be automatically linked when dlopen is called.
27 //extern TFile *ROOTfile;
28 
29 // Routine used to create our JEventProcessor
30 extern "C"{
31 void InitPlugin(JApplication *app){
32  InitJANAPlugin(app);
33  app->AddProcessor(new DEventProcessor_radlen_hists());
34 }
35 }
36 
37 //------------------
38 // DEventProcessor_radlen_hists
39 //------------------
41 {
42  pthread_mutex_init(&mutex, NULL);
43 }
44 
45 //------------------
46 // ~DEventProcessor_radlen_hists
47 //------------------
49 {
50 }
51 
52 //------------------
53 // init
54 //------------------
56 {
57  // open ROOT file (if needed)
58  //if(ROOTfile != NULL) ROOTfile->cd();
59 
60  // Create THROWN directory
61  TDirectory *dir = new TDirectoryFile("RADLEN","RADLEN");
62  dir->cd();
63 
64  // Create histograms
65  dE_vs_r = new TH2F("dE_vs_r","dE (keV) vs r", 300, 0.0, 65.0,1000, 0.0, 1000.0);
66  dE_vs_z = new TH2F("dE_vs_z","dE (keV) vs z", 650, 0.0, 650.0, 1000, 0.0, 1000.0);
67 
68  int Ntheta_bins = 480;
69  double theta_min = 0.0;
70  double theta_max = 120.0;
71  theta_nevents = new TH1F("theta_nevents","Number of events per #theta bin in int_radlen_vs_z_vs_theta", Ntheta_bins, theta_min, theta_max);
72  nXo_vs_z_vs_theta = new TH2F("nXo_vs_z_vs_theta","Radiation lengths vs. z and #theta", 650, 0.0, 650.0, Ntheta_bins, theta_min, theta_max);
73  nXo_vs_z_vs_theta->SetXTitle("z-position along beamline (cm)");
74  nXo_vs_z_vs_theta->SetYTitle("Thrown #theta angle (deg)");
75  inXo_vs_z_vs_theta = new TH2F("inXo_vs_z_vs_theta","Integrated radiation lengths vs. z and #theta", 650, 0.0, 650.0, Ntheta_bins, theta_min, theta_max);
76  inXo_vs_z_vs_theta->SetXTitle("z-position along beamline (cm)");
77  inXo_vs_z_vs_theta->SetYTitle("Thrown #theta angle (deg)");
78 
79  nXo_vs_r_vs_theta = new TH2F("nXo_vs_r_vs_theta","Radiation lengths vs. r and #theta", 180, 0.0, 90.0, Ntheta_bins, theta_min, theta_max);
80  nXo_vs_r_vs_theta->SetXTitle("Radial distance from beamline (cm)");
81  nXo_vs_r_vs_theta->SetYTitle("Thrown #theta angle (deg)");
82  inXo_vs_r_vs_theta = new TH2F("inXo_vs_r_vs_theta","Integrated radiation lengths vs. r and #theta", 180, 0.0, 90.0, Ntheta_bins, theta_min, theta_max);
83  inXo_vs_r_vs_theta->SetXTitle("Radial distance from beamline (cm)");
84  inXo_vs_r_vs_theta->SetYTitle("Thrown #theta angle (deg)");
85 
86  nXo_vs_z = new TH1F("nXo_vs_z","Radiation lengths vs. z", 650, 0.0, 650.0);
87  inXo_vs_z = new TH1F("inXo_vs_z","Integrated radiation lengths vs. z", 650, 0.0, 650.0);
88  nXo_vs_r = new TH1F("nXo_vs_r","Radiation lengths vs. r", 1000, 0.0, 90.0);
89  inXo_vs_r = new TH1F("inXo_vs_r","Integrated radiation lengths vs. r", 1000, 0.0, 90.0);
90 
91  nXo_vs_z->SetStats(0);
92  nXo_vs_z->SetFillStyle(3000);
93  nXo_vs_z->SetFillColor(kMagenta);
94  inXo_vs_z->SetStats(0);
95  inXo_vs_z->SetFillStyle(3000);
96  inXo_vs_z->SetFillColor(kMagenta);
97  nXo_vs_r->SetStats(0);
98  nXo_vs_r->SetFillStyle(3000);
99  nXo_vs_r->SetFillColor(kMagenta);
100  inXo_vs_r->SetStats(0);
101  inXo_vs_r->SetFillStyle(3000);
102  inXo_vs_r->SetFillColor(kMagenta);
103 
104  // Tree
105  rstep_ptr = &rstep;
106  tradstep = new TTree("radstep","Radlen steps");
107  tradstep->Branch("R","radstep",&rstep_ptr);
108 
109  // Go back up to the parent directory
110  dir->cd("../");
111 
112  bfield = NULL;
113 
114  return NOERROR;
115 }
116 
117 //------------------
118 // brun
119 //------------------
120 jerror_t DEventProcessor_radlen_hists::brun(JEventLoop *loop, int32_t runnumber)
121 {
122  DApplication *dapp = dynamic_cast<DApplication*>(japp);
123  bfield = dapp->GetBfield(runnumber);
124 
125  return NOERROR;
126 }
127 
128 //------------------
129 // evnt
130 //------------------
131 jerror_t DEventProcessor_radlen_hists::evnt(JEventLoop *loop, uint64_t eventnumber)
132 {
133  vector<const DMCTrajectoryPoint*> trajpoints;
134  vector<const DMCThrown*> throwns;
135  loop->Get(trajpoints);
136  loop->Get(throwns);
137 
138  // Get theta of thrown particle
139  double theta = 0.0;
140  if(throwns.size()!=1){
141  _DBG_<<"Event doesn't have exactly 1 thrown (has"<<throwns.size()<<"). Skipping..."<<endl;
142  return NOERROR;
143  }
144  theta = throwns[0]->momentum().Theta()*57.3;
145  theta_nevents->Fill(theta);
146 
147  // Although we are only filling objects local to this plugin, TTree::Fill() periodically writes to file: Global ROOT lock
148  japp->RootWriteLock(); //ACQUIRE ROOT LOCK
149 
150  rstep.stot = 0.0;
151  rstep.ix_over_Xo = 0.0;
152  rstep.iB_cross_p_dl = 0.0;
153  rstep.iB_dl = 0.0;
154  DVector3 tmp = throwns[0]->momentum();
155  rstep.pthrown.SetXYZ(tmp.X(), tmp.Y(), tmp.Z());
156  for(unsigned int i=0;i<trajpoints.size();i++){
157  const DMCTrajectoryPoint *traj = trajpoints[i];
158 
159  double dE = traj->dE*1.0E6; // convert to keV
160  double r = sqrt(pow((double)traj->x,2.0) + pow((double)traj->y,2.0));
161  dE_vs_r->Fill( r, dE);
162  dE_vs_z->Fill( traj->z, dE);
163 
164  double dnXo = traj->step/traj->radlen;
165  nXo_vs_r_vs_theta->Fill(r, theta, dnXo);
166  nXo_vs_z_vs_theta->Fill(traj->z, theta, dnXo);
167  nXo_vs_r->Fill(r, dnXo);
168  nXo_vs_z->Fill(traj->z, dnXo);
169 
170  double Bx, By, Bz;
171  bfield->GetField(traj->x, traj->y, traj->z, Bx, By, Bz);
172  rstep.B.SetXYZ(Bx, By, Bz);
173  TVector3 mom(traj->px, traj->py, traj->pz);
174  TVector3 B_cross_p = rstep.B.Cross(mom);
175  rstep.iB_cross_p_dl += B_cross_p.Mag() * traj->step/mom.Mag();
176  rstep.iB_dl += rstep.B.Mag()*traj->step;
177 
178  rstep.pos.SetXYZ(traj->x, traj->y, traj->z);
179  rstep.s = traj->step;
180  rstep.stot += (double)traj->step;
181  rstep.Xo = traj->radlen;
182  rstep.ix_over_Xo += dnXo;
183 
184  tradstep->Fill();
185  }
186  pthread_mutex_unlock(&mutex);
187 
188  return NOERROR;
189 }
190 
191 //------------------
192 // erun
193 //------------------
195 {
196  // Since we are filling histograms local to this plugin, it will not interfere with other ROOT operations: can use plugin-wide ROOT fill lock
197  japp->RootFillLock(this); //ACQUIRE ROOT FILL LOCK
198 
199  // Scale the nXo_vs_r_vs_theta and nXo_vs_z_vs_theta histos by the
200  // number of events for each theta bin. At the same time, integrate
201  // the number of radiation lengths and fill the
202  // inXo_vs_r_vs_theta and inXo_vs_z_vs_theta histos.
203  TH2F *h = nXo_vs_r_vs_theta;
204  for(int i=1; i<=h->GetNbinsY(); i++){
205  double N = theta_nevents->GetBinContent(i);
206  double nXo_vs_r_sum=0.0;
207  for(int j=1; j<=h->GetNbinsX(); j++){
208  double v = nXo_vs_r_vs_theta->GetBinContent(j,i);
209  double nXo = N==0.0 ? 0.0:v/N;
210  nXo_vs_r_sum += nXo;
211  nXo_vs_r_vs_theta->SetBinContent(j,i, nXo);
212  inXo_vs_r_vs_theta->SetBinContent(j,i, nXo_vs_r_sum);
213  }
214  }
215 
216  h = nXo_vs_z_vs_theta;
217  for(int i=1; i<=h->GetNbinsY(); i++){
218  double N = theta_nevents->GetBinContent(i);
219  double nXo_vs_z=0.0;
220  for(int j=1; j<=h->GetNbinsX(); j++){
221  double v = nXo_vs_z_vs_theta->GetBinContent(j,i);
222  double nXo = N==0.0 ? 0.0:v/N;
223  nXo_vs_z += nXo;
224  nXo_vs_z_vs_theta->SetBinContent(j,i, nXo);
225  inXo_vs_z_vs_theta->SetBinContent(j,i, nXo_vs_z);
226  }
227  }
228 
229  double N = theta_nevents->Integral();
230 _DBG_<<"N="<<N<<endl;
231  nXo_vs_r->Scale(1.0/N);
232  nXo_vs_z->Scale(1.0/N);
233  inXo_vs_r->SetBinContent(1,nXo_vs_r->GetBinContent(1));
234  for(int j=2; j<=nXo_vs_r->GetNbinsX(); j++){
235  inXo_vs_r->SetBinContent(j, nXo_vs_r->GetBinContent(j)+inXo_vs_r->GetBinContent(j-1));
236  }
237  inXo_vs_z->SetBinContent(1,nXo_vs_z->GetBinContent(1));
238  for(int j=2; j<=nXo_vs_z->GetNbinsX(); j++){
239  inXo_vs_z->SetBinContent(j, nXo_vs_z->GetBinContent(j)+inXo_vs_z->GetBinContent(j-1));
240  }
241 
242  japp->RootFillUnLock(this); //RELEASE ROOT FILL LOCK
243 
244  return NOERROR;
245 }
246 
247 
248 //------------------
249 // GapIntegration
250 //------------------
252 {
253  //======== THIS IS NOT CURRENTLY USED ==================
254 
255  // Generate histogram of integrals of radiation lengths
256  // Loop over bins of the histogram looking for gaps where (essentially) zero
257  // radiation lengths exist. Integrate the radiation lengths between the
258  // gaps.
259  int bin =0;
260  int Nbins = hin->GetNbinsX();
261  double min_nXo = 1.0E-7;
262  while(bin<Nbins){
263  while(hin->GetBinContent(bin)<min_nXo)if(++bin>=Nbins)break;
264  if(bin>=Nbins)break;
265 
266  double nXo;
267  double nXotot = 0.0;
268  double pos = 0.0;
269  double left = hin->GetBinCenter(bin);
270  double right = left;
271  while((nXo=hin->GetBinContent(bin))>=min_nXo){
272  nXotot += nXo;
273  right = hin->GetBinCenter(bin);
274  pos += nXo*hin->GetBinCenter(bin);
275  if(++bin>=Nbins)break;
276  }
277 
278  pos/=nXotot; // center position
279  //if(nXotot>1.0E-4)cout<<"r(weighted)="<<pos<<" r(center)="<<(left+right)/2.0<<" num_Xo="<<nXotot<<endl;
280 
281  // Find the bins in hout corrsponding to
282  // left and right positions of this material
283  int bin_left = hout->FindBin(left);
284  int bin_right = hout->FindBin(right);
285  for(int i=bin_left; i<=bin_right; i++)hout->SetBinContent(i, nXotot);
286  }
287 }
288 
289 //------------------
290 // fini
291 //------------------
293 {
294  return NOERROR;
295 }
DApplication * dapp
TVector3 DVector3
Definition: DVector3.h:14
DMagneticFieldMap * GetBfield(unsigned int run_number=1)
JApplication * japp
InitPlugin_t InitPlugin
#define _DBG_
Definition: HDEVIO.h:12
double sqrt(double)
jerror_t evnt(JEventLoop *loop, uint64_t eventnumber)
Invoked via DEventProcessor virtual method.
jerror_t erun(void)
Invoked via DEventProcessor virtual method.
void GapIntegration(TH1F *hin, TH1F *hout)
jerror_t brun(JEventLoop *loop, int32_t runnumber)
Invoked via DEventProcessor virtual method.
jerror_t fini(void)
Invoked via DEventProcessor virtual method.
TDirectory * dir
Definition: bcal_hist_eff.C:31
jerror_t init(void)
Invoked via DEventProcessor virtual method.