00001 #include "BadHardwareTableMaker.h"
00002 #include "DatabaseInterface/DbiWriter.h"
00003 #include "DatabaseInterface/DbiTableProxyRegistry.h"
00004 #include "DatabaseInterface/DbiCascader.h"
00005
00006 CVSID(" $Id: BadHardwareTableMaker.cxx,v 1.2 2005/05/19 17:44:12 tagg Exp $");
00007
00008 void CreateTables(const std::string& tablename)
00009 {
00010 MSG("Morgue",Msg::kWarning) << "Creating " << tablename << " BadHardware table." << endl;
00011 int dbNo = 0;
00012 DbiStatement* s = DbiTableProxyRegistry::Instance()
00013 .GetCascader()
00014 .CreateStatement(dbNo);
00015
00016
00017 s->ExecuteUpdate(Form("create table if not exists %sVLD ("
00018 " SEQNO int not null primary key auto_increment,"
00019 " TIMESTART datetime not null,"
00020 " TIMEEND datetime not null,"
00021 " DETECTORMASK tinyint,"
00022 " SIMMASK tinyint,"
00023 " TASK int,"
00024 " AGGREGATENO int,"
00025 " CREATIONDATE datetime not null,"
00026 " INSERTDATE datetime not null )",
00027 tablename.c_str()));
00028
00029 s->ExecuteUpdate(Form("create table if not exists %s ("
00030 " SEQNO integer,"
00031 " TYPE integer,"
00032 " ID integer, "
00033 " START integer, "
00034 " STOP integer, "
00035 " BADNESS integer, "
00036 " REASON text,"
00037 " index (SEQNO))",
00038 tablename.c_str()));
00039 }
00040
00041
00042
00043 BadHardwareTableMaker::BadHardwareTableMaker(Int_t det,
00044 Int_t sim,
00045 Int_t task,
00046 const std::string& comment,
00047 const std::string& name) :
00048 fDetector(det),
00049 fSimFlag(sim),
00050 fTask(task),
00051 fName(name),
00052 fComment(comment)
00053 {
00054 }
00055
00056
00057 int BadHardwareTableMaker::Add(BadHardware* bh)
00058 {
00062 fBadStuff.insert(std::pair<VldTimeStamp,BadHardware*>
00063 (bh->GetStart(),bh)
00064 );
00065
00066 return 0;
00067 }
00068
00069
00070 int BadHardwareTableMaker::Write(Double_t split_time,
00071 VldTimeStamp beg_vld,
00072 VldTimeStamp end_vld)
00073 {
00083
00084
00085 CreateTables(fName);
00086
00087
00088 if(beg_vld.GetSec() == 0) beg_vld = fBadStuff.begin()->second->GetStart();
00089 if(end_vld.GetSec() == 0) {
00090 for(BadStuff_t::iterator i=fBadStuff.begin(); i!=fBadStuff.end(); ++i)
00091 if(i->second->GetStop() > end_vld) end_vld = i->second->GetStop();
00092 }
00093
00094
00095 VldTimeStamp beg_chunk = beg_vld;
00096 VldTimeStamp end_chunk;
00097
00098 while(beg_chunk < end_vld) {
00099
00100 int chunk_rows = 0;
00101
00102
00103 end_chunk = beg_chunk;
00104 end_chunk.Add(split_time);
00105 if(end_chunk > end_vld) end_chunk = end_vld;
00106
00107 VldRange chunk_range(fDetector,fSimFlag,beg_chunk, end_chunk, fComment.c_str());
00108 DbiWriter<BadHardware> writer( chunk_range,
00109 -1,
00110 fTask,
00111 VldTimeStamp(),
00112 0,
00113 fComment,
00114 fName);
00115
00116
00117
00118 BadStuff_t::iterator it = fBadStuff.begin();
00119 while(it != fBadStuff.end()) {
00120 if((it->second->GetStart() <= end_chunk) && (it->second->GetStop() >= beg_chunk)) {
00121
00122 writer << *(it->second);
00123 chunk_rows++;
00124 }
00125
00126 if(it->second->GetStop() < end_chunk) {
00127 delete it->second;
00128 fBadStuff.erase(it);
00129 }
00130 it++;
00131 }
00132
00133 writer.Close();
00134 MSG("Morgue",Msg::kInfo) << "Wrote " << chunk_rows << " rows starting time "
00135 << beg_chunk.AsString() << std::endl;
00136
00137 beg_chunk = end_chunk;
00138 }
00139
00140
00141 return 0;
00142 }