Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hdbeam_current.cc
Go to the documentation of this file.
1 // $Id$
2 //
3 // File: hdbeam_current.cc
4 // Created: Tue Feb 21 20:51:51 EST 2017
5 // Creator: davidl (on Linux gluon48.jlab.org 2.6.32-431.20.3.el6.x86_64 x86_64)
6 //
7 
8 #include <iostream>
9 #include <iomanip>
10 #include <string>
11 using namespace std;
12 
13 #include <stdlib.h>
14 
15 #include <DANA/DApplication.h>
16 #include <JANA/JParameterManager.h>
17 
19 
20 
21 bool QUIET = false;
22 bool PRINT_FIDUCIAL = false;
23 bool PRINT_TOTAL = false;
24 bool PRINT_TRIP_TIME = false;
25 bool PRINT_NTRIPS = false;
26 bool PRINT_TRIPS = false;
27 double BEAM_TRIP_MIN_T = 3.0; // seconds
28 double BEAM_ON_MIN_nA = 10.0; // nA
29 double T_MIN = 0.0;
30 double T_MAX = 0.0;
31 int32_t RUN=0;
32 
33 
34 void Usage(void);
35 void ParseCommandLineArgs(int narg, char* argv[]);
36 
37 //-----------
38 // main
39 //-----------
40 int main(int narg, char *argv[])
41 {
42 
43  // Parse command line arguments and then create a DApplication
44  ParseCommandLineArgs(narg, argv);
45  DApplication *dapp = new DApplication(0, NULL);
46  //dapp->Init();
47 
48  // Create a JEventLoop so we'll have a DBeamCurrent_factory
49  JEventLoop *loop = new JEventLoop(dapp);
50  loop->GetJEvent().SetRunNumber(RUN);
51 
52  // Get factory
53  DBeamCurrent_factory *fac = (DBeamCurrent_factory*)loop->GetFactory("DBeamCurrent");
54  if(!fac){
55  cerr << "Unable to find DBeamCurrent_factory!" << endl;
56  return -1;
57  }
58 
59  // Read in trip map from CCDB
60  gPARMS->SetParameter("BEAM_ON_MIN_nA", BEAM_ON_MIN_nA);
61  gPARMS->SetParameter("BEAM_TRIP_MIN_T", BEAM_TRIP_MIN_T);
62  fac->init();
63  fac->brun(loop, RUN);
64 
65  // Get some values from factory
66  double t_total = fac->IntegratedTime();
67  if(T_MAX==0.0) T_MAX = t_total;
68  double t_fiducial = fac->IntegratedFiducialTime(T_MIN, T_MAX);
69  uint32_t Nboundaries = fac->boundaries.size();
70  uint32_t Ntrips = fac->trip.size();
71 
72  // Get avg. beam current as well as fraction of time beam was on
73  double last_t = 0.0;
74  double last_Ibeam = 0.0;
75  double Ion_sum = 0.0;
76  double Ion_t = 0.0;
77  double Ioff_t = 0.0;
78  for(auto &b : fac->boundaries){
79  double delta_t = b.t - last_t;
80  if(last_Ibeam >= BEAM_ON_MIN_nA){
81  Ion_t += delta_t;
82  Ion_sum += delta_t*last_Ibeam;
83  }else{
84  Ioff_t += delta_t;
85  }
86  last_t = b.t;
87  last_Ibeam = b.Ibeam;
88  }
89  double Ibeam_avg = Ion_sum/Ion_t;
90 
91  cout << endl;
92  cout << "--------------------------------------------" << endl;
93  cout << "Electron Beam Trip Report" << endl;
94  cout << endl;
95  cout << " Run: " << RUN << endl;
96  cout << "total time of run: " << t_total << " sec" << endl;
97  cout << " beam ON time: " << Ion_t << " sec (" << 100.0*Ion_t/t_total << "%)" << endl;
98  cout << " fiducial time: " << t_fiducial << " sec (" << 100.0*t_fiducial/t_total << "%)" << endl;
99  cout << " trip buffer: " << BEAM_TRIP_MIN_T << " sec" <<endl;
100  cout << " beam ON thresh: " << BEAM_ON_MIN_nA << " nA" << endl;
101  cout << " t range: " << T_MIN << " - " << T_MAX << " sec (" << 100.0*(T_MAX-T_MIN)/t_total << "%)" << endl;
102  cout << " Nboundaries: " << Nboundaries << " (current changed by >3nA)" << endl;
103  cout << " Ntrips: " << Ntrips << endl;
104  cout << " trips/hr: " << (float)Ntrips/(float)(T_MAX - T_MIN)*3600.0 << endl;
105  cout << " avg. Ibeam ON: " << Ibeam_avg << " nA" << endl;
106 
107  if(PRINT_TRIPS){
108  cout << endl;
109  cout << "Trip/recover times relative to start of run in seconds:" << endl;
110  cout << "tripped recovered off" << endl;
111  cout << "------- -------- -------" << endl;
112  uint32_t i=1; // First recover time is start of run
113  for(auto t : fac->trip){
114  char str[256];
115  if( i < fac->recover.size())
116  sprintf(str, "%7.0f %7.0f %7.0f", t, fac->recover[i], fac->recover[i]-t);
117  else
118  sprintf(str, "%6.0f", t);
119  cout << str << endl;
120  i++;
121  }
122  cout << endl;
123  }
124 
125  cout << "--------------------------------------------" << endl;
126  cout << endl;
127 
128  return 0;
129 }
130 
131 //-----------------------
132 // Usage
133 //-----------------------
134 void Usage(void)
135 {
136  cout<<endl;
137  cout<<"Usage:"<<endl;
138  cout<<" hdbeam_current [options] run"<<endl;
139  cout<<endl;
140  cout<<" options:"<<endl;
141  cout<<" -h, --help Show this Usage statement"<<endl;
142 // cout<<" -tfiducial Report the fiducial time (sec)"<<endl;
143 // cout<<" -ttotal Report the total time (sec)"<<endl;
144 // cout<<" -ttrips Report the total tripped time (sec)"<<endl;
145 // cout<<" -Ntrips Report the total number of beam trips" << endl;
146  cout<<" -trips Report times of all identified trips" << endl;
147  cout<<" -tbuffer # Set the buffer time to exclude near trips (sec)"<<endl;
148  cout<<" -tmin # Set the minimum of the time range (sec)"<<endl;
149  cout<<" -tmax # Set the maximum of the time range (sec)"<<endl;
150  cout<<" -Ion # Set the beam current threshold for \"ON\" (nA)"<<endl;
151  cout<<endl;
152  cout<<" "
153  "This will query the CCDB for electron the beam trip map for the \n"
154  "given run number. The CCDB is filled from the EPICS archive using \n"
155  "the mySampler program to interpolate values from the IBCAD00CRCUR6 \n"
156  "current monitor. Changes in the beam current greater than 3nA are \n"
157  "written to the archive as \"boundaries\". The above options can be\n"
158  "used to interpret these boundaries in terms of beam trips. A \n"
159  "fiducial time range is made from the regions between the trips with \n"
160  "excluding a buffer just before trip and just after a recovery. This \n"
161  "is intended to cut out regions where the beam may be slightly astray. \n"
162  "\n"
163 // "By default, all numbers are reported. If any of -tfiducial, -ttotal,\n"
164 // "or -ttrips or -Ntrips is given, then only that number is printed and\n"
165 // "nothing else. This is to make it easier to use this in scripts.\n"
166 // "\n"
167  "The -tmin and -tmax options allow one to specify a time range in which\n"
168  "to integrate fiducial time. If no minimum or maximum time are specified\n"
169  "then the full time range of the run is used.\n"
170  "\n"
171  "Note that this functionality also exists within sim-recon via the\n"
172  "DBeamCurrent objects and their \"is_fudicial\" flag. The \n"
173  "DBeamCurrent_factory class can also be used to integrate the fiducial\n"
174  "time in a run or given time range via the IntegratedFiducialTime()\n"
175  "method.\n"
176  "\n"
177  "This utility is meant to give quick, easy access to the same information\n"
178  "without having to write a special sim-recon program.\n" << endl;
179 
180 }
181 
182 //-----------------------
183 // ParseCommandLineArgs
184 //-----------------------
185 void ParseCommandLineArgs(int narg, char* argv[])
186 {
187  for(int i=1; i<narg; i++){
188  string arg(argv[i]);
189  string next(i<narg-1 ? argv[i+1]:"");
190  float argf = atof(next.c_str());
191  bool used_next = false; // keep track if "next" is used so we can have a single error check below
192 
193 // if(arg=="-tfiducial"){PRINT_FIDUCIAL=true; QUIET=true;}
194 // if(arg=="-ttotal" ){PRINT_TOTAL=true; QUIET=true;}
195 // if(arg=="-ttrips" ){PRINT_TRIP_TIME=true; QUIET=true;;}
196 // if(arg=="-Ntrips" ){PRINT_NTRIPS=true; QUIET=true;}
197  if(arg=="-trips" ){PRINT_TRIPS=true; QUIET=true;}
198  if(arg=="-tbuffer" ){used_next=true; BEAM_TRIP_MIN_T = argf;}
199  if(arg=="-tmin" ){used_next=true; T_MIN = argf;}
200  if(arg=="-tmax" ){used_next=true; T_MAX = argf;}
201  if(arg=="-Ion" ){used_next=true; BEAM_ON_MIN_nA = argf;}
202 
203  if(arg=="-h" || arg=="--help"){Usage(); exit(0);}
204 
205  if(used_next){
206  i++;
207  continue;
208  }
209 
210  if(arg[0] != '-') RUN =atoi(argv[i]);
211  }
212 
213  if(RUN == 0){
214  Usage();
215  exit(0);
216  }
217 }
218 
DApplication * dapp
char str[256]
bool PRINT_TOTAL
sprintf(text,"Post KinFit Cut")
double BEAM_ON_MIN_nA
bool PRINT_NTRIPS
vector< Boundary > boundaries
bool PRINT_TRIPS
jerror_t brun(jana::JEventLoop *eventLoop, int32_t runnumber)
Called everytime a new run number is detected.
bool PRINT_FIDUCIAL
double IntegratedFiducialTime(double t_start=0.0, double t_end=0.0)
bool PRINT_TRIP_TIME
double T_MIN
int32_t RUN
void ParseCommandLineArgs(int narg, char *argv[], vector< char * > &unused_args)
Definition: bfield2root.cc:298
double T_MAX
vector< double > recover
jerror_t init(void)
Called once at program start.
bool QUIET
void Usage(JApplication &app)
Definition: hd_ana.cc:33
double BEAM_TRIP_MIN_T
int main(int argc, char *argv[])
Definition: gendoc.cc:6