BuildMetricsTree.C
Go to the documentation of this file.
1 #include <TSQLServer.h>
2 #include <TSQLResult.h>
3 #include <iostream.h>
4 #include <fstream>
5 #include <iomanip.h>
6 #include <sstream>
7 #include <string>
8 #include <streambuf>
9 #include <cstring>
10 
11 void BuildMetricsTree(int ttype=2, int ftype=4){
12 
13  gStyle->SetTimeOffset(0);
14 
15  //Choose File Type
16 
17  Int_t file_type = ftype; //1 will return cana files; 2 will return reco files; 3 will return ndos files; 4 will return fardet files
18  Int_t trig_type = ttype; //0 will return numi files; 1 will return booster files; 2 will return cosmic files
19 
20  cout<<"trigger type="<<trig_type<<endl;
21 
22  //Choose Software release
23 
24  const char* software_release;
25  const char* DBsoftware_release;
26 
27  if(file_type<4) {software_release = "S13-09-04";
28  DBsoftware_release = "S13-02-16";}
29 // DBsoftware_release = "S12-12-12";}
30  else {
31  DBsoftware_release = "S13-09-17";
32  software_release = "S13-09-04";
33  }
34 
35 
36  Int_t apply_runlen = 1; //1 if want to apply run length cut; 0 if do not
37  Int_t apply_year = 1; //1 if want to apply year cut; 0 if do not (time of first/last non-empty events are in 2010 or 2011)
38  Int_t apply_empty = 1; //1 if want to apply empty cut; 0 if do not
39  Int_t apply_noise = 1; //1 if want to apply noise cut; 0 if do not
40  Int_t apply_hits = 1; //1 if want to apply hits cut; 0 if do not
41 
42 
43  //Cut Parameter Values
44 
45  Double_t run_len = 1000.0;//Minimum length of run in events.
46  Double_t empty_cut = 100.0; //The percentage of empty events less than this value. This only cuts out files with ONLY empty events.
47  Double_t noise_cut = 10.0; //The percentage of noise-only events less than this value.
48  Double_t hits_low = 0.0; //Hits per event per channel less than this parameter
49  Double_t hits_up = 0.0605; //Hits per event per channel greater than this parameter
50 
51  if(trig_type==0){
52  run_len = 30;
53  hits_up = 0.12;
54  }
55 
56  //Name of Output File
58  if( file_type ==4) filename = "goodsyncdaqfiles_fardet_";
59  else filename = "goodsyncdaqfiles_ndos_";
60 
61  if( trig_type ==0) filename += "t00_numi_";
62  if( trig_type ==1) filename += "t01_bnb_";
63  if( trig_type ==2) filename += "t02_cosmic_";
64 
65  if( file_type ==4) filename += "gt_10326_";
66  else filename += "gt_11570_";
67 
68  filename += string(DBsoftware_release) + ".txt";
69 
70  cout<<"test1"<<endl;
71 
72  // **************END OF TYPICAL USER'S NEED TO ALTER THIS SCRIPT*******************************************
73 
74 
75 
76  std::ifstream t;
77  t.open("/nova/app/db/nova_reader_pwd"); // open input file
79  getline(t,buffer);
80  cout<<buffer<<endl;
81  char * dbpass;
82  dbpass = new char [8];
83  dbpass = buffer.c_str();
84  cout<<dbpass<<endl;
85 
86  std::ofstream runfile;
87  runfile.open(filename.c_str());
88 
89  UInt_t run, FirstTimeSec, LastTimeSec, Diff;
90  Int_t subrun, nevents, nactivechannels, FirstYear, LastYear,
91  pass_runlen, pass_year, pass_empty, pass_noise, pass_hits,
92  pass_sync, pass_all, nemptyevents, nnoiseevents, nrawactivechannels;
93  Double_t nslices, sliceduration, hitsperslice, hitsperevent,
94  occupancympv, occupancysigma, emptypercentage, noisepercentage,
95  hitsperslicenonoise;
96 
97  TFile *outfile;
98  if(file_type==4) outfile = new TFile(TString::Format("MetricHists_FarDet_t0%d_%s.root",trig_type,DBsoftware_release),"recreate");
99  else outfile = new TFile(TString::Format("MetricHists_NDOS_t0%d_%s.root",trig_type,DBsoftware_release),"recreate");
100 
101  TTree *mytree = new TTree("evts","");
102  mytree->Branch("run",&run,"run/i");
103  mytree->Branch("FirstTimeSec",&FirstTimeSec,"FirstTimeSec/i");
104  mytree->Branch("LastTimeSec",&LastTimeSec,"LastTimeSec/i");
105  mytree->Branch("Diff",&Diff,"Diff/i");
106  mytree->Branch("subrun",&subrun,"subrun/I");
107  mytree->Branch("nevents",&nevents,"nevents/I");
108  mytree->Branch("nemptyevents",&nemptyevents,"nemptyevents/I");
109  mytree->Branch("nnoiseevents",&nnoiseevents,"nnoiseevents/I");
110  mytree->Branch("nactivechannels",&nactivechannels,"nactivechannels/I");
111  mytree->Branch("nrawactivechannels",&nrawactivechannels,"nrawactivechannels/I");
112  mytree->Branch("FirstYear",&FirstYear,"FirstYear/I");
113  mytree->Branch("LastYear",&LastYear,"LastYear/I");
114  mytree->Branch("pass_runlen",&pass_runlen,"pass_runlen/I");
115  mytree->Branch("pass_year",&pass_year,"pass_year/I");
116  mytree->Branch("pass_empty",&pass_empty,"pass_empty/I");
117  mytree->Branch("pass_noise",&pass_noise,"pass_noise/I");
118  mytree->Branch("pass_hits",&pass_hits,"pass_hits/I");
119 // mytree->Branch("pass_sync",&pass_sync,"pass_sync/I");
120  mytree->Branch("pass_all",&pass_all,"pass_all/I");
121  mytree->Branch("nslices",&nslices,"nslices/D");
122  mytree->Branch("sliceduration",&sliceduration,"sliceduration/D");
123  mytree->Branch("hitsperslice",&hitsperslice,"hitsperslice/D");
124  mytree->Branch("hitsperslicenonoise",&hitsperslicenonoise,"hitsperslicenonoise/D");
125  mytree->Branch("hitsperevent",&hitsperevent,"hitsperevent/D");
126  mytree->Branch("occupancympv",&occupancympv,"occupancympv/D");
127  mytree->Branch("occupancysigma",&occupancysigma,"occupancysigma/D");
128  mytree->Branch("emptypercentage",&emptypercentage,"emptypercentage/D");
129  mytree->Branch("noisepercentage",&noisepercentage,"noisepercentage/D");
130 
131  std::stringstream dbstream;
132  std::string dbstring;
133 
134  if(file_type<4) dbstream << "select * from ndos.nearline_metrics where run>11570 and triggertype="
135 // if(file_type<4) dbstream << "select * from ndos.nearline_metrics where run>14789 and triggertype="
136  << trig_type << " and softwarerelease='"
137  << DBsoftware_release << "' order by run asc;";
138  else dbstream << "select * from fardet.nearline_metrics where run>10326 and triggertype="
139  << trig_type << " and softwarerelease='"
140  << DBsoftware_release << "' order by run asc;";
141 
142  dbstring = dbstream.str();
143  char * dbcstr;
144  dbcstr = new char [dbstring.size()+1];
145  strcpy(dbcstr, dbstring.c_str());
146 
147  TSQLServer *db;
148  if(file_type<4) db = TSQLServer::Connect("pgsql://ifdbprod.fnal.gov:5433/nova_prod", "nova_reader", dbpass);
149  else db = TSQLServer::Connect("pgsql://ifdbprod.fnal.gov:5433/nova_prod", "nova_reader", dbpass);
150  TSQLStatement *stmt = db->Statement(dbcstr,1000);
151  if(stmt->Process()){
152  // store result of statement in buffer
153  stmt->StoreResult();
154 
155  //...........................................................................................................................
156 
157  cout<<"test2"<<endl;
158 
159  while (stmt->NextResultRow()){
160 
161  // Extract rows one after another
162 
163  run = stmt->GetUInt(0);
164  subrun = stmt->GetInt(1);
165  Int_t triggertype = stmt->GetInt(2);
166  nevents = stmt->GetInt(3);
167  nemptyevents = stmt->GetInt(4);
168  nnoiseevents = stmt->GetInt(5);
169  nactivechannels = stmt->GetInt(6);
170  nrawactivechannels = stmt->GetInt(7);
171  const char* firsteventtime = stmt->GetString(8);
172  const char* firstnonemptyeventtime = stmt->GetString(9);
173  const char* lastnonemptyeventtime = stmt->GetString(10);
174  const char* lasteventtime = stmt->GetString(11);
175  nslices = stmt->GetDouble(12);
176  sliceduration = stmt->GetDouble(13);
177  hitsperslice = stmt->GetDouble(14);
178  hitsperslicenonoise = stmt->GetDouble(15);
179  hitsperevent = stmt->GetDouble(16);
180  occupancympv = stmt->GetDouble (17);
181  occupancysigma = stmt->GetDouble(18);
182  const char* detector = stmt->GetString(19);
183  const char* softwarerelease = stmt->GetString(20);
184  const char* inserttime = stmt->GetString(21);
185  const char* insertuser = stmt->GetString(22);
186 // cout<<"run/subrun="<<run<<"/"<<subrun<<endl;
187  //...................................................................................
188  //Extract percentage of empty and noise events; define small run for file name convention
189 
190  emptypercentage = 0;
191  noisepercentage = 0;
192 
193  if (nevents !=0){
194  emptypercentage=((double) nemptyevents*100)/((double) nevents);
195  noisepercentage=((double) nnoiseevents*100)/((double) nevents);
196  }
197 
198  Int_t smallrun=run/100;
199  //...................................................................................
200 
201  //Extract times of first and last nonempty events
202  TDatime *FirstTime = new TDatime(firstnonemptyeventtime);
203  TDatime *LastTime = new TDatime(lastnonemptyeventtime);
204  FirstTimeSec = FirstTime->Convert();
205  FirstYear = FirstTime->GetYear();
206  LastTimeSec = LastTime->Convert();
207  LastYear = LastTime->GetYear();
208  Diff = LastTimeSec - FirstTimeSec;
209 
210  if (apply_runlen) pass_runlen = ( nevents > run_len && LastTimeSec > FirstTimeSec );
211  else pass_runlen = 1;
212 
213 // if (apply_year) pass_year = ( (FirstYear == 2010 || FirstYear == 2011 || FirstYear == 2012) &&
214 // (LastYear == 2010 || LastYear == 2011 || LastYear == 2012) );
215  if (apply_year) pass_year = ( FirstYear >= 2010 && LastYear <= 2013 && FirstYear <= LastYear );
216  else pass_year = 1;
217 
218  if (apply_empty) pass_empty = (emptypercentage < empty_cut);
219  else pass_empty = 1;
220 
221  if (apply_noise) pass_noise = (noisepercentage < noise_cut);
222  else pass_noise = 1;
223 
224  if (apply_hits) pass_hits = ((hitsperevent > hits_low)&&(hitsperevent < hits_up));
225  else pass_hits = 1;
226 
227 // if(Diff<0 || run==10490 || run==10492) cout << run << "." << subrun << ": " << Diff << " ; " << firstnonemptyeventtime << " ; " << lastnonemptyeventtime << endl;
228 
229  pass_sync = 1; pass_all = 0;
230 
231  std::string filetypestr, trigtypestr, filedir, fileversion;
232 
233  filedir = "/nova/data/novaroot/NDOS";
234  fileversion = "v1.data.daq";
235 
236  switch(file_type){
237  case 1: filetypestr = "cana"; break;
238  case 4:
239  filetypestr = "fardet";
240  filedir = "/nova/data/novaroot/FarDet";
241  fileversion = "v1.data.daq";
242  break;
243  default: filetypestr = "ndos"; break;
244  }
245 
246  switch(trig_type){
247  case 0: trigtypestr = "numi"; break;
248  case 1: trigtypestr = "bnb"; break;
249  default: trigtypestr = "cosmic"; break;
250  }
251 
252  TString fileformat = TString::Format("%s/%s/%06d/%d/%s/%s_r%08d_s%02d_t%02d_%s_%s_%s.root",
253  filedir.c_str(), software_release, smallrun, run, trigtypestr.c_str(), filetypestr.c_str(),
254  run, subrun, trig_type, trigtypestr.c_str(), software_release, fileversion.c_str());
255 
256  if(gSystem->AccessPathName(fileformat)){
257 
258  //cout << "No file: " << fileformat << endl;
259 
260  continue;
261  }
262 
263 /*
264  bool syncstatus=true;
265 
266  if(file_type<4){
267 
268  TFile syncfile(fileformat);
269  syncfile.cd("TrackPlotsAna");
270 
271  for(int xbin=1; xbin < 5; xbin++){
272  for(int ybin=1; ybin<4; ybin++){
273  if( HTracksOtherDCMRatio_0_1->GetBinContent(xbin, ybin) > 0 && HTracksOtherDCMRatio_0_1->GetBinContent(xbin, ybin) < 0.2 )
274  syncstatus=false;
275  }
276  }
277  }
278 
279  if(!syncstatus) continue;
280 
281 */
282  if ( pass_runlen && pass_empty && pass_noise && pass_hits && pass_year ){
283 
284  pass_all = 1;
285 
286  }
287 
288  if ( !pass_all ){
289 
290  cout << "Run " << run << "." << subrun << " has failed: ";
291  if(!pass_runlen) cout << "Run length = " << nevents << " events; ";
292  if(!pass_empty) cout << "Empty percentage = " << emptypercentage << "%; ";
293  if(!pass_noise) cout << "Noise percentage = " << noisepercentage << "%; ";
294  if(!pass_hits) cout << "Hits/event = " << hitsperevent << "; ";
295  if(!pass_year) { cout << "Year = " << FirstYear << " - " << LastYear << "; ";
296  cout << firstnonemptyeventtime << "; "; }
297  if(!pass_sync) cout << "Failed Sync Test; ";
298  cout << endl;
299 
300  }
301  else{
302  std::runfile << fileformat << std::endl;
303  }
304 
305  mytree->Fill();
306 
307  } //End of stmt while loop
308 
309  delete stmt;
310 
311  }//End of if stmt process
312 
313  delete db;
314 
315  runfile.close();
316 
317  outfile->cd();
318  mytree->Write("mytree");
319  outfile->Close();
320 
321 } //End of Good Runs
Int_t pass_all
Definition: SimpleIterate.C:42
Double_t nactivechannels
Definition: SimpleIterate.C:52
string filename
Definition: shutoffs.py:106
Int_t nevents
Definition: SimpleIterate.C:25
::xsd::cxx::tree::buffer< char > buffer
Definition: Database.h:179
Int_t pass_empty
Definition: SimpleIterate.C:38
void BuildMetricsTree(int ttype=2, int ftype=4)
Double_t emptypercentage
Definition: SimpleIterate.C:53
TTree * mytree
Definition: SimpleIterate.C:18
OStream cout
Definition: OStream.cxx:6
file_type
Definition: Constants.h:150
void Format(TGraph *gr, int lcol, int lsty, int lwid, int mcol, int msty, double msiz)
Definition: Style.cxx:154
Int_t pass_runlen
Definition: SimpleIterate.C:37
FILE * outfile
Definition: dump_event.C:13
Int_t pass_hits
Definition: SimpleIterate.C:39
enum BeamMode string