GoodRuns.C
Go to the documentation of this file.
1 //To run the script, one should do:
2 // root -b -q -l GoodRuns.C
3 //This creates an output file that contains the full path length of the files that pass the cuts. It can be used with the -S nova notation
4 //
5 //If you run and get messages like:"Error in <TDatime::Set>: year must be >= 1995" don't worry - these files are filtered out by the year cut
6 
7 //To alter this script:
8 
9 //You only need to deal with the top section of this script. First, there is a section that most people need to alter. You need to choose what file type you want (cana, reco, or ndos files) and what trigger type (numi, bosster, or cosmic).
10 
11 //Then, you might want to not make the standard DataCheck cuts. In this case, you should alter the next section, "Choice of Cuts to Make". By switching from 1 to 0, you can turn off the cut. Currently, we have five cuts that we make. We make a cut for the length of the run; currently this cut is not implemented precisely and instead just cuts out subrun 0 files that are too short. We also make a cut to remove files who do not begin and end in the years 2010/2011. Then, we make a cut on the percentage of empty events, the percentage of noise-only events, and the range of hits/active channel/event.
12 
13 //Perhaps besides choosing which cuts to use, you want to actually alter the cut values. This is under "Cut Parameter Values." Currently, we cut subrun 0 files that are shorter than 30 minutes. We remove files that are 100% empty events. We remove files with more than 10% noise-only events. Lastly, we remove files that do not have hits/active channel/event greater than 0 and less than 0.06.
14 
15 //Another thing one should want to possibly alter is the runs from which to take this data. Currently, we have this set as all runs greater than 11570. This is the forward horn current period. We have not currently set the cuts for runs less than 11570 so one could include / only use those runs at their discretion only. IF you feel ready for the power, you can change the run range by altering the dbstream (line 55).
16 
17 //Lastly, the user can alter the name of the output file to one of their liking.
18 
19 #include <TSQLServer.h>
20 #include <TSQLResult.h>
21 #include <iostream.h>
22 #include <fstream>
23 #include <iomanip.h>
24 #include <sstream>
25 #include <string>
26 #include <streambuf>
27 #include <cstring>
28 
29 void GoodRuns(){
30 
31  //Choose File Type
32 
33  Int_t file_type = 4; //1 will return cana files; 2 will return reco files; 3 will return ndos files; 4 will return fardet files
34  Int_t trig_type = 2; //0 will return numi files; 1 will return booster files; 2 will return cosmic files
35 
36  cout<<"trigger type="<<trig_type<<endl;
37 
38  //Choose Software release
39 
40  const char* software_release;
41  const char* DBsoftware_release;
42 
43  if(file_type<4) {software_release = "S12-12-12";
44  DBsoftware_release = "S12-12-12";}
45  else { DBsoftware_release = "S13-03-18";
46  software_release = "development";}
47 
48 
49  Int_t apply_runlen = 1; //1 if want to apply run length cut; 0 if do not
50  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)
51  Int_t apply_empty = 1; //1 if want to apply empty cut; 0 if do not
52  Int_t apply_noise = 1; //1 if want to apply noise cut; 0 if do not
53  Int_t apply_hits = 1; //1 if want to apply hits cut; 0 if do not
54 
55 
56  //Cut Parameter Values
57 
58  Double_t run_len = 1800.0;//Minimum length of run in seconds. 1800 sec is 30 minutes. This cut currently isn't implemented quite right and instead only cuts subrun 0 files shorter than this length.
59  Double_t empty_cut = 100.0; //The percentage of empty events less than this value. This only cuts out files with ONLY empty events.
60  Double_t noise_cut = 10.0; //The percentage of noise-only events less than this value.
61  Double_t hits_low = 0.0; //Hits per event per channel less than this parameter
62  Double_t hits_up = 0.05; //Hits per event per channel greater than this parameter
63 
64  //Name of Output File
66  if( trig_type ==0) filename = "goodsyncrecofiles_t00_numi_gt_11570.txt";
67  if( trig_type ==1) filename = "goodsyncrecofiles_t01_bnb_gt_11570.txt";
68  if( trig_type ==2) filename = "goodsyncrecofiles_t02_cosmic_gt_11570.txt";
69  if( file_type ==4) filename = "goodsyncdaqfiles_t02_cosmic_gt_10326.txt";
70 
71  cout<<"test1"<<endl;
72 
73  //**************END OF TYPICAL USER'S NEED TO ALTER THIS SCRIPT*******************************************
74 
75 
76 
77  std::ifstream t;
78  t.open("/nova/app/db/nova_reader_pwd"); // open input file
80  getline(t,buffer);
81  cout<<buffer<<endl;
82  char * dbpass;
83  dbpass = new char [8];
84  dbpass = buffer.c_str();
85  cout<<dbpass<<endl;
86 
87  std::ofstream runfile;
88  runfile.open(filename.c_str());
89 
90 
91  std::stringstream dbstream;
92  std::string dbstring;
93 
94  if(file_type<4) dbstream << "select * from ndos.subrun_metrics where run>11570 and triggertype=2 and softwarerelease='"
95  << DBsoftware_release << "' order by run asc;";
96  else dbstream << "select * from fardet.nearline_metrics where run>10326 and triggertype=2 and softwarerelease='"
97  << DBsoftware_release << "' order by run asc;";
98 
99  dbstring = dbstream.str();
100  char * dbcstr;
101  dbcstr = new char [dbstring.size()+1];
102  strcpy(dbcstr, dbstring.c_str());
103 
104  TSQLServer *db;
105  if(file_type<4) db = TSQLServer::Connect("pgsql://ifdbprod.fnal.gov:5433/nova_prod", "nova_reader", dbpass);
106  else db = TSQLServer::Connect("pgsql://ifdbdev.fnal.gov:5433/nova_dev", "nova_reader", dbpass);
107  TSQLStatement *stmt = db->Statement(dbcstr,1000);
108  if(stmt->Process()){
109  // store result of statement in buffer
110  stmt->StoreResult();
111 
112  //...........................................................................................................................
113 
114  cout<<"test2"<<endl;
115 
116 
117  while (stmt->NextResultRow()){
118 
119  // Extract rows one after another
120 
121  UInt_t run = stmt->GetUInt(0);
122  Int_t subrun = stmt->GetInt(1);
123  Int_t triggertype = stmt->GetInt(2);
124  Int_t nevents = stmt->GetInt(3);
125  Int_t nemptyevents = stmt->GetInt(4);
126  Int_t nnoiseevents = stmt->GetInt(5);
127  Int_t nactivechannels = stmt->GetInt(6);
128  Int_t nrawactivechannels = stmt->GetInt(7);
129  const char* firsteventtime = stmt->GetString(8);
130  const char* firstnonemptyeventtime = stmt->GetString(9);
131  const char* lastnonemptyeventtime = stmt->GetString(10);
132  const char* lasteventtime = stmt->GetString(11);
133  Double_t nslices = stmt->GetDouble(12);
134  Double_t sliceduration = stmt->GetDouble(13);
135  Double_t hitsperslice = stmt->GetDouble(14);
136  Double_t hitsperslicenonoise = stmt->GetDouble(15);
137  Double_t hitsperevent = stmt->GetDouble(16);
138  Double_t occupancympv = stmt->GetDouble (17);
139  Double_t occupancysigma = stmt->GetDouble(18);
140  const char* detector = stmt->GetString(19);
141  const char* softwarerelease = stmt->GetString(20);
142  const char* inserttime = stmt->GetString(21);
143  const char* insertuser = stmt->GetString(22);
144 // cout<<"run/subrun="<<run<<"/"<<subrun<<endl;
145  //...................................................................................
146  //Extract percentage of empty and noise events; define small run for file name convention
147 
148  Double_t emptypercentage = 0;
149  Double_t noisepercentage = 0;
150 
151  if (nevents !=0){
152  emptypercentage=((double) nemptyevents*100)/((double) nevents);
153  noisepercentage=((double) nnoiseevents*100)/((double) nevents);
154  }
155 
156  Int_t smallrun=run/100;
157  //...................................................................................
158 
159 
160  //Extract times of first and last nonempty events
161  TDatime *FirstTime = new TDatime(firstnonemptyeventtime);
162  TDatime *LastTime = new TDatime(lastnonemptyeventtime);
163  UInt_t FirstTimeSec = FirstTime->Convert();
164  Int_t FirstYear = FirstTime->GetYear();
165  UInt_t LastTimeSec = LastTime->Convert();
166  Int_t LastYear = LastTime->GetYear();
167  UInt_t Diff = LastTimeSec - FirstTimeSec;
168 
169 
170  //Apply cuts based on choices made at top of the script
171  Int_t pass_runlen;
172  Int_t pass_year;
173  Int_t pass_empty;
174  Int_t pass_noise;
175  Int_t pass_hits;
176 
177 
178  if (apply_runlen) pass_runlen = ((subrun != 0) || (Diff > run_len));
179  else pass_runlen = 1;
180 
181 // if (apply_year) pass_year = ( (FirstYear == 2010 || FirstYear == 2011 || FirstYear == 2012) &&
182 // (LastYear == 2010 || LastYear == 2011 || LastYear == 2012) );
183  if (apply_year) pass_year = ( FirstYear >= 2010 && LastYear <= 2013 );
184  else pass_year = 1;
185 
186  if (apply_empty) pass_empty = (emptypercentage < empty_cut);
187  else pass_empty = 1;
188 
189  if (apply_noise) pass_noise = (noisepercentage < noise_cut);
190  else pass_noise = 1;
191 
192  if (apply_hits) pass_hits = ((hitsperevent > hits_low)&&(hitsperevent < hits_up));
193  else pass_hits = 1;
194 
195 // cout << "Before = " << pass_runlen << pass_empty << pass_noise << pass_hits << pass_year << endl;
196  if ( pass_runlen && pass_empty && pass_noise && pass_hits && pass_year ){
197 
198  //cout<<"PASSED"<<endl;
199  //**********************************************
200  //SYNC
201 
202  //cout<<smallrun<<"\t"<<run<<"\t"<<subrun<<endl;
203 
204  bool syncfilestatus=false;
205  bool syncstatus=true;
206 
207  std::string filetypestr, trigtypestr, filedir, fileversion;
208 
209  filedir = "/nova/data/novaroot/NDOS";
210  fileversion = "v2.data.reco.hists";
211 
212  switch(file_type){
213  case 1: filetypestr = "cana"; break;
214  case 4:
215  filetypestr = "fardet";
216  filedir = "/nova/data/novaroot/FarDet";
217  fileversion = "hist_v1.data.daq";
218  break;
219  default: filetypestr = "ndos"; break;
220  }
221 
222  switch(trig_type){
223  case 0: trigtypestr = "numi"; break;
224  case 1: trigtypestr = "bnb"; break;
225  default: trigtypestr = "cosmic"; break;
226  }
227 
228  TString fileformat = TString::Format("%s/%s/%06d/%d/cosmic/%s_r%08d_s%02d_t02_cosmic_%s_%s.root",
229  filedir.c_str(), software_release, smallrun, run, filetypestr.c_str(), run,
230  subrun, software_release, fileversion.c_str());
231 
232  //cout << fileformat << endl;
233  if(gSystem->AccessPathName(fileformat)) continue;
234 
235  TFile syncfile(fileformat);
236  syncfilestatus=true;
237 
238  if(syncfilestatus && file_type<4){
239 
240  syncfile.cd("TrackPlotsAna");
241 
242 
243  //cout<<run<<"."<<subrun<<"\t";
244  for(int xbin=1; xbin < 5; xbin++){
245  for(int ybin=1; ybin<4; ybin++){
246 
247  cout<<HTracksOtherDCMRatio_0_1->GetBinContent(xbin, ybin)<<"\t";
248  if( HTracksOtherDCMRatio_0_1->GetBinContent(xbin, ybin) > 0 && HTracksOtherDCMRatio_0_1->GetBinContent(xbin, ybin) < 0.2 )
249  syncstatus=false;
250 
251  }
252  }
253  cout<<"\n";
254 
255  }
256 
257  if(!syncstatus){
258  cout<<"Failed Sync"<<endl;
259  continue;
260  }
261 
262  fileformat = TString::Format("%s/%s/%06d/%d/%s/%s_r%08d_s%02d_t%02d_%s_%s_%s.root",
263  filedir.c_str(), software_release, smallrun, run, trigtypestr.c_str(),
264  filetypestr.c_str(), run, subrun, trig_type, trigtypestr.c_str(),
265  software_release, fileversion.c_str());
266 
267 
268  std::runfile << fileformat << std::endl;
269 
270  }//end of if over cuts
271  else{
272  cout << "Run " << run << "." << subrun << " has failed: ";
273  if(!pass_runlen) cout << "Run length = " << int(Diff/60) << "m" << Diff%60 << "s; ";
274  if(!pass_empty) cout << "Empty percentage = " << emptypercentage << "%; ";
275  if(!pass_noise) cout << "Noise percentage = " << noisepercentage << "%; ";
276  if(!pass_hits) cout << "Hits/event = " << hitsperevent << "; ";
277  if(!pass_year) cout << "Year = " << FirstYear << " - " << LastYear << "; ";
278  cout << endl;
279  }
280 
281  } //End of stmt while loop
282 
283  delete stmt;
284 
285  }//End of if stmt process
286 
287  delete db;
288 
289  runfile.close();
290 
291 } //End of Good Runs
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
Double_t emptypercentage
Definition: SimpleIterate.C:53
Definition: run.py:1
OStream cout
Definition: OStream.cxx:6
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
void GoodRuns()
Definition: GoodRuns.C:29
file_type
Definition: Constants.h:367
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
Int_t pass_hits
Definition: SimpleIterate.C:39