00001 #include "FileGap.h" 00002 00003 #include <Validity/VldContext.h> 00004 #include <DatabaseInterface/DbiSqlContext.h> 00005 00006 #include <Conventions/Detector.h> 00007 #include <Conventions/SimFlag.h> 00008 00009 00010 #include <algorithm> 00011 #include <iostream> 00012 using namespace std; 00013 00014 00015 FileGap::FileGap() 00016 : fRes(new DbiResultPtr<BeamMonFileSummary>) 00017 { 00018 00019 } 00020 00021 FileGap::~FileGap() 00022 { 00023 if (fRes) delete fRes; fRes = 0; 00024 } 00025 00026 bool bound_less_than(const FileGap::Bound& a, const FileGap::Bound& b) 00027 { 00028 return a.first<b.first; 00029 } 00030 00031 FileGap::BoundList FileGap::GetFileBounds(VldTimeStamp beg, VldTimeStamp end, double deltas) 00032 { 00033 Detector::Detector_t det = Detector::kNear; 00034 SimFlag::SimFlag_t simflag = SimFlag::kData; 00035 VldContext vc(det,simflag,beg); 00036 00037 const char* sql = Form("(TIMEEND>='%s') and (TIMESTART<='%s')", 00038 beg.AsString("s"),end.AsString("s")); 00039 int nrows = fRes->NewQuery(DbiSqlContext(sql)); 00040 BoundList bounds; 00041 for (int ind=0; ind<nrows; ++ind) { 00042 const BeamMonFileSummary *fs = fRes->GetRow(ind); 00043 if (!fs->fSpillCount) continue; 00044 if (fs->fTclkTriggerEvent != 0xa9) continue; 00045 VldTimeStamp first_spilltime = fs->fFirstSpillTime; 00046 first_spilltime.Add(-1*deltas); 00047 VldTimeStamp last_spilltime = fs->fLastSpillTime; 00048 last_spilltime.Add(deltas); 00049 bounds.push_back(Bound(first_spilltime,last_spilltime)); 00050 } 00051 00052 bounds.sort(bound_less_than); 00053 return bounds; 00054 } 00055 00056 void FileGap::DumpGaps(VldTimeStamp beg, VldTimeStamp end) 00057 { 00058 BoundList bounds = this->GetFileBounds(beg,end); 00059 00060 VldTimeStamp last = beg; 00061 double total_gap = 0, total_data = 0; 00062 for (BoundList::iterator it=bounds.begin(); it != bounds.end(); ++it) { 00063 VldTimeStamp start = it->first; 00064 VldTimeStamp stop = it->second; 00065 00066 double dt_gap = start - last; 00067 double dt_data = stop - start; 00068 if (dt_gap>120.0) { 00069 cerr << last << " - " << start << " = " << dt_gap 00070 << " (" << dt_data << ")\n"; 00071 00072 if (last != beg) total_gap += dt_gap; 00073 } 00074 total_data += dt_data; 00075 00076 last = stop; 00077 } 00078 00079 cerr << "From " << beg << " to " << end <<endl 00080 << "total data = " << total_data 00081 << " total gap = " << total_gap << endl; 00082 } 00083 00084 static void dump_file_summary(const BeamMonFileSummary* fs) 00085 { 00086 cerr << fs->fFileName << " size=" 00087 << fs->fFileSize << " " 00088 << fs->fProtonCount << " prot in " 00089 << fs->fSpillCount << " spills, time range: " 00090 << fs->fFirstSpillTime << " --> " 00091 << fs->fLastSpillTime 00092 << endl; 00093 } 00094 00095 void FileGap::DumpMissing(std::list<std::string> file_list) 00096 { 00097 Detector::Detector_t det = Detector::kNear; 00098 SimFlag::SimFlag_t simflag = SimFlag::kData; 00099 VldContext vc(det,simflag,VldTimeStamp::GetBOT()); 00100 00101 const char* sql = Form("(TIMEEND>='%s') and (TIMESTART<='%s')", 00102 VldTimeStamp::GetBOT().AsString("s"), 00103 VldTimeStamp::GetEOT().AsString("s")); 00104 int nrows = fRes->NewQuery(DbiSqlContext(sql)); 00105 cerr << "There are " << nrows << " files in the DB\n"; 00106 00107 typedef map<string,const BeamMonFileSummary*> DbMap; 00108 DbMap dbmap; 00109 00110 for (int ind=0; ind<nrows; ++ind) { 00111 const BeamMonFileSummary *fs = fRes->GetRow(ind); 00112 if (!fs) { 00113 cerr << "Got zero row at " << ind << " / " << nrows << endl; 00114 continue; 00115 } 00116 dbmap[fs->fFileName] = fs; 00117 } 00118 00119 int checked=0, found=0, missing=0; 00120 list<string>::iterator it, done=file_list.end(); 00121 const BeamMonFileSummary* last=0; 00122 bool ingap = true; 00123 for (it=file_list.begin(); it!=done; ++it) { 00124 ++checked; 00125 const BeamMonFileSummary* fs = dbmap[*it]; 00126 if (fs) { 00127 00128 ++found; 00129 last = fs; 00130 if (ingap) { 00131 dump_file_summary(fs); 00132 ingap = false; 00133 } 00134 continue; 00135 } 00136 if (last) { 00137 dump_file_summary(last); 00138 last = 0; 00139 ingap = true; 00140 } 00141 ++missing; 00142 cerr << *it << " missing from db" << endl; 00143 } 00144 cerr << checked << " checked, " 00145 << found << " found, " 00146 << missing << " missing " 00147 << endl; 00148 00149 } 00150 00151 #include <TSystem.h> 00152 00153 00154 void FileGap::DumpMissing(const char* directory) 00155 { 00156 void* dir = gSystem->OpenDirectory(directory); 00157 if (!dir) { 00158 cerr << "Failed to open " << directory << endl; 00159 return; 00160 } 00161 00162 cerr << "Checking: " << directory << endl; 00163 00164 list<string> file_names; 00165 const char* cptr = 0; 00166 while ( (cptr = gSystem->GetDirEntry(dir))) { 00167 00168 if (cptr[0] == '.') { 00169 cerr << "Skipping (dot file): " << cptr << endl; 00170 continue; 00171 } 00172 if (cptr[0] != 'B') { 00173 continue; 00174 cerr << "Skipping (bad name): " << cptr << endl; 00175 } 00176 string file(cptr); 00177 if (string::npos == file.rfind(".mbeam.root")) { 00178 cerr << "Skipping (bad ext): " << cptr << endl; 00179 continue; 00180 } 00181 00182 file_names.push_back(file); 00183 } 00184 file_names.sort(); 00185 cerr << "Using " << file_names.size() << " files\n"; 00186 this->DumpMissing(file_names); 00187 }