HighLAND
BeamBunching.cxx
1 #include "BeamBunching.hxx"
2 #include <TVector3.h>
3 #include <stdio.h>
4 #include <iostream>
5 #include <math.h>
6 
7 //*****************************************************************************
8 void BeamBunching::ReadBeamBunchStructure(const std::string& filename){
9 //*****************************************************************************
10 
11  FILE *pFile = fopen (filename.c_str(),"r");
12 
13  if( pFile == NULL ) {
14  std::cout << "Cannot open file Bunch File '" << filename << "'" << std::endl;;
15  exit(1);
16  }
17 
18  // Read the numbers of bunches from the first line in the file
19  fscanf(pFile,"%d",&_nbunches);
20 
21  Int_t runini;
22  Int_t runfin;
23  std::vector<Float_t> bunch(_nbunches);
24 
25  while ( fscanf(pFile,"%d%d%f%f%f%f%f%f%f%f",&runini, &runfin, &bunch[0], &bunch[1], &bunch[2], &bunch[3], &bunch[4], &bunch[5], &bunch[6], &bunch[7]) == (Int_t)_nbunches+2 ){
26 
27  if( runini == -1 ){ // This is MC
28  bunch_time_mc = bunch;
29  }
30  else {
31  bunchrunperiod.push_back(std::pair<int,int>(runini,runfin));
32  std::vector<Float_t> local;
33  bunch_time_data.push_back(bunch);
34  }
35 
36  }
37 
38  fclose(pFile);
39 }
40 
41 //*****************************************************************************
43 //*****************************************************************************
44 
45  //define start time as latest "edge" of bunch time window
46  Int_t bunchperiod = -1;
47 
48  if( ! event.GetIsMC() ) {
49  for(UInt_t j = 0 ; j < bunchrunperiod.size(); j++ ) {
50  if( event.EventInfo.Run <= bunchrunperiod[j].second && event.EventInfo.Run >= bunchrunperiod[j].first ){
51  bunchperiod = j;
52  break;
53  }
54  }
55  }
56 
57  return bunchperiod;
58 }
59 
60 //*****************************************************************************
61 UInt_t BeamBunching::GetNBunches(const AnaEventB& event){
62 //*****************************************************************************
63 
64  if( event.GetIsMC() )
65  return bunch_time_mc.size();
66  else {
67  Int_t bunchperiod = GetBunchPeriod(event);
68  if( bunchperiod > -1 ) {
69  return bunch_time_data[bunchperiod].size();
70  }
71  }
72 
73  return 0;
74 }
75 
76 //*****************************************************************************
77 Float_t BeamBunching::GetBunchCentralTime(const AnaEventB& event, Int_t ibunch){
78 //*****************************************************************************
79 
80  Float_t selTime = -99999.;
81 
82  if(event.GetIsMC())
83  selTime = bunch_time_mc[ibunch];
84  else {
85  Int_t bunchperiod = GetBunchPeriod(event);
86  if( bunchperiod > -1 ) {
87  selTime = bunch_time_data[bunchperiod][ibunch];
88  }
89  }
90 
91  return selTime;
92 }
93 
94 //*****************************************************************************
95 Int_t BeamBunching::GetBunch(Float_t tTrack, Int_t run, bool isMC, bool cosmic_mode, Float_t sigma, Float_t NSigmas) const {
96 //*****************************************************************************
97 
98  if (cosmic_mode) {
99  // In cosmics mode, everything gets put into one bunch.
100  return 0;
101  }
102 
103  Int_t bunch = -1;
104  // Float_t sigma;
105  Float_t time;
106 
107  // Find Bunch Period
108 
109  Int_t bunchperiod = -1;
110 
111  if( !isMC ) {
112  for(UInt_t j = 0 ; j < bunchrunperiod.size(); j++ ) {
113  if( run <= bunchrunperiod[j].second && run >= bunchrunperiod[j].first ){
114  bunchperiod = j;
115  break;
116  }
117  }
118  if( bunchperiod < 0 ) return bunch;
119  }
120 
121  for (UInt_t i=0;i<_nbunches;i++){
122 
123 
124  if (isMC){
125  time = bunch_time_mc[i];
126  }
127  else{
128  time = bunch_time_data[bunchperiod][i];
129  }
130 
131  if( time < 0 ) return bunch;
132  if (fabs(tTrack-time)<sigma*NSigmas){
133  bunch = i;
134  break;
135  }
136  }
137 
138  return bunch;
139 }
140 
AnaEventInfoB EventInfo
Run, sunrun, event, time stamp, etc.
UInt_t GetNBunches(const AnaEventB &event)
Number of bunches in the run period for the current run.
bool GetIsMC() const
Return whether this event is from Monte Carlo or not.
Int_t GetBunchPeriod(const AnaEventB &event)
Define the Bunch Period from the run number.
Float_t GetBunchCentralTime(const AnaEventB &event, Int_t ibunch)
Get the central time for bunch ibunch.
void ReadBeamBunchStructure(const std::string &filename)
Read the bunch structure for analysis usage.
Definition: BeamBunching.cxx:8
Int_t GetBunch(Float_t tTrack, Int_t run, bool isMC, bool cosmic_mode, Float_t sigma=15., Float_t NSigmas=4.0) const
The run number for a given time.
Int_t Run
The ND280 run number.