00001 #include "CondFormats/RPCObjects/interface/RPCObCond.h"
00002 #include "CondFormats/RPCObjects/interface/RPCObPVSSmap.h"
00003
00004 #include "CondCore/DBCommon/interface/DbConnection.h"
00005 #include "CondCore/DBCommon/interface/DbConnectionConfiguration.h"
00006
00007 #include "CondCore/DBCommon/interface/DbTransaction.h"
00008
00009
00010
00011
00012 #include "CondCore/Utilities/interface/PayLoadInspector.h"
00013 #include "CondCore/Utilities/interface/InspectorPythonWrapper.h"
00014
00015 #include "DataFormats/MuonDetId/interface/RPCDetId.h"
00016 #include "Geometry/RPCGeometry/interface/RPCGeomServ.h"
00017
00018
00019
00020 #include "CoralBase/TimeStamp.h"
00021 #include <sys/time.h>
00022
00024 #include "TROOT.h"
00025 #include "TCanvas.h"
00026 #include "TStyle.h"
00027 #include "TColor.h"
00028 #include "TLine.h"
00029 #include "TH1D.h"
00030 #include "TH2D.h"
00031 #include "TF1.h"
00032 #include "TGraph.h"
00033 #include "TMultiGraph.h"
00034 #include "TLegend.h"
00035
00036 #include <string>
00037 #include <fstream>
00038 #include <sstream>
00039 #include <algorithm>
00040 #include <numeric>
00041 #include <iterator>
00042 #include <iostream>
00043 #include <fstream>
00044 #include <boost/ref.hpp>
00045 #include <boost/bind.hpp>
00046 #include <boost/function.hpp>
00047 #include <boost/shared_ptr.hpp>
00048 #include <boost/iterator/transform_iterator.hpp>
00049
00050 #include "TH1D.h"
00051 #include "TH2D.h"
00052 #include <boost/algorithm/string.hpp>
00053 #include <utility>
00054 using std::pair;
00055 using std::make_pair;
00056
00057 namespace cond {
00058
00059 namespace rpcobvmon {
00060 enum How { detid, day, time, current};
00061
00062 void extractDetId(RPCObVmon const & pl, std::vector<int> const & which,std::vector<float> & result,const float& starttime, const float& endtime) {
00063 std::vector<RPCObVmon::V_Item> const & vmon = pl.ObVmon_rpc;
00064 for(unsigned int i = 0; i < vmon.size(); ++i ){
00065 if (which[0] == 0){
00066 result.push_back(vmon[i].dpid);
00067 }
00068 else{
00069 if(vmon[i].dpid == which[0])
00070 result.push_back(vmon[i].dpid);
00071 }
00072 }
00073 }
00074
00075 void extractDay(RPCObVmon const & pl, std::vector<int> const & which,std::vector<float> & result,const float& starttime, const float& endtime) {
00076 std::vector<RPCObVmon::V_Item> const & vmon = pl.ObVmon_rpc;
00077 for(unsigned int i = 0; i < vmon.size(); ++i ){
00078 if (which[0] == 0){
00079 result.push_back(vmon[i].day);
00080 }
00081 else{
00082 if(vmon[i].dpid == which[0])
00083 result.push_back(vmon[i].day);
00084 }
00085 }
00086 }
00087
00088 void extractTime(RPCObVmon const & pl, std::vector<int> const & which,std::vector<float> & result,const float& starttime, const float& endtime) {
00089 std::vector<RPCObVmon::V_Item> const & vmon = pl.ObVmon_rpc;
00090 for(unsigned int i = 0; i < vmon.size(); ++i ){
00091 if (which[0] == 0){
00092 result.push_back(vmon[i].time);
00093 }
00094 else{
00095 if(vmon[i].dpid == which[0])
00096 result.push_back(vmon[i].time);
00097 }
00098 }
00099 }
00100
00101 void extractCurrent(RPCObVmon const & pl, std::vector<int> const & which,std::vector<float> & result,const float& starttime,const float& endtime) {
00102 std::vector<RPCObVmon::V_Item> const & vmon = pl.ObVmon_rpc;
00103 for(unsigned int i = 0; i < vmon.size(); ++i ){
00104 if (which[0] == 0){
00105 result.push_back(vmon[i].value);
00106 }
00107 else{
00108 if(vmon[i].dpid == which[0])
00109 result.push_back(vmon[i].value);
00110 }
00111 }
00112 }
00113
00114 typedef boost::function<void(RPCObVmon const & pl, std::vector<int> const & which,std::vector<float> & result,const float& starttime,const float& endtime)> RPCObVmonExtractor;
00115 }
00116
00117 template<>
00118 struct ExtractWhat<RPCObVmon> {
00119
00120 rpcobvmon::How m_how;
00121 std::vector<int> m_which;
00122 float m_starttime;
00123 float m_endtime;
00124
00125 rpcobvmon::How const & how() const { return m_how;}
00126 std::vector<int> const & which() const { return m_which;}
00127 float const & startTime() const {return m_starttime;}
00128 float const & endTime() const {return m_endtime;}
00129
00130 void set_how(rpcobvmon::How i) {m_how=i;}
00131 void set_which(std::vector<int> & i) { m_which.swap(i);}
00132 void set_starttime(float& i){m_starttime = i;}
00133 void set_endtime(float& i){m_endtime = i;}
00134
00135 };
00136
00137
00138 template<>
00139 class ValueExtractor<RPCObVmon>: public BaseValueExtractor<RPCObVmon> {
00140 public:
00141
00142 static rpcobvmon::RPCObVmonExtractor & extractor(rpcobvmon::How how) {
00143 static rpcobvmon::RPCObVmonExtractor fun[4] = {
00144 rpcobvmon::RPCObVmonExtractor(rpcobvmon::extractDetId),
00145 rpcobvmon::RPCObVmonExtractor(rpcobvmon::extractDay),
00146 rpcobvmon::RPCObVmonExtractor(rpcobvmon::extractTime),
00147 rpcobvmon::RPCObVmonExtractor(rpcobvmon::extractCurrent)
00148 };
00149 return fun[how];
00150 }
00151
00152 typedef RPCObVmon Class;
00153 typedef ExtractWhat<Class> What;
00154 static What what() { return What();}
00155
00156 ValueExtractor(){}
00157 ValueExtractor(What const & what)
00158 : m_what(what)
00159 {
00160
00161
00162 }
00163
00164 void compute(Class const & it){
00165 std::vector<float> res;
00166 extractor(m_what.how())(it,m_what.which(),res,m_what.startTime(),m_what.endTime());
00167 swap(res);
00168 }
00169
00170 private:
00171 What m_what;
00172
00173 };
00174
00175
00176
00177
00178
00179
00180
00181 template<>
00182 std::string PayLoadInspector<RPCObVmon>::summary_adv(
00183 std::string const & opt_str,
00184 std::vector<int> const & vInts,
00185 std::vector<float> const & vFloats,
00186 std::vector<std::string> const & vStrings) const {
00187
00188 std::stringstream ss;
00189 std::vector<std::string>::const_iterator iStr;
00190 ss << "Strings got: " << std::endl;
00191 for (iStr = vStrings.begin(); iStr != vStrings.end(); ++ iStr){
00192 ss << *iStr << std::endl;
00193 }
00194 return ss.str();
00195 }
00196
00197
00198
00199 template<>
00200 std::string PayLoadInspector<RPCObVmon>::summary() const{
00201
00202
00203 std::string authPath="/afs/cern.ch/cms/DB/conddb";
00204 std::string conString="oracle://cms_orcoff_prod/CMS_COND_31X_RPC";
00205
00206
00207 std::string token="[DB=00000000-0000-0000-0000-000000000000][CNT=RPCObPVSSmap][CLID=53B2D2D9-1F4E-9CA9-4D71-FFCCA123A454][TECH=00000B01][OID=0000000C-00000000]";
00208
00209
00210 DbConnection dbConn;
00211
00212
00213 dbConn.configuration().setAuthenticationPath(authPath);
00214 dbConn.configure();
00215
00216
00217 DbSession dbSes=dbConn.createSession();
00218
00219
00220 dbSes.open(conString,true);
00221
00222
00223 dbSes.transaction().start(true);
00224
00225
00226 boost::shared_ptr<RPCObPVSSmap> pvssPtr;
00227 pvssPtr=dbSes.getTypedObject<RPCObPVSSmap>(token);
00228
00229
00230 std::vector<RPCObPVSSmap::Item> pvssCont=pvssPtr->ObIDMap_rpc;
00231
00232 std::stringstream ss;
00233
00234 std::vector<RPCObVmon::V_Item> const & vmon = object().ObVmon_rpc;
00235
00236 ss <<"DetID\t\t"<<"V(V)\t"<<"Time\t"<<"Day\n";
00237 for(unsigned int i = 0; i < vmon.size(); ++i ){
00238 for(unsigned int p = 0; p < pvssCont.size(); ++p){
00239 if(vmon[i].dpid!=pvssCont[p].dpid || pvssCont[p].suptype!=0 || pvssCont[p].region!=0)continue;
00240 RPCDetId rpcId(pvssCont[p].region,pvssCont[p].ring,pvssCont[p].station,pvssCont[p].sector,pvssCont[p].layer,pvssCont[p].subsector,1);
00241 RPCGeomServ rGS(rpcId);
00242 std::string chName(rGS.name().substr(0,rGS.name().find("_Backward")));
00243 ss <<chName <<"\t"<<vmon[i].value<<"\t"<<vmon[i].time<<"\t"<<vmon[i].day<<"\n";
00244 }
00245 }
00246
00247 dbSes.close();
00248
00249 return ss.str();
00250 }
00251
00252 Double_t linearF(Double_t *x, Double_t *par){
00253 Double_t y=0.;
00254 y=par[0]*(*x);
00255 return y;
00256 }
00257
00258 unsigned long long toUNIX(int date, int time)
00259 {
00260 int yea_ = (int)date/100;
00261 int yea = 2000 + (date - yea_*100);
00262 int mon_ = (int)yea_/100;
00263 int mon = yea_ - mon_*100;
00264 int day = (int)yea_/100;
00265 int sec_ = (int)time/100;
00266 int sec = time - sec_*100;
00267 int min_ = (int)sec_/100;
00268 int min = sec_ - min_*100;
00269 int hou = (int)sec_/100;
00270 int nan = 0;
00271 coral::TimeStamp TS;
00272 TS = coral::TimeStamp(yea, mon, day, hou, min, sec, nan);
00273
00274 long long UT = (TS.year()-1970)*31536000+static_cast<int>(trunc((TS.year()-1972)/4))*86400+
00275 (((TS.month()-1)*31)*86400)+((TS.day()-1)*86400)+TS.hour()*3600+TS.minute()*60+TS.second();
00276
00277 if (TS.month() == 3) UT = UT - 3*86400;
00278 if (TS.month() == 4) UT = UT - 3*86400;
00279 if (TS.month() == 5) UT = UT - 4*86400;
00280 if (TS.month() == 6) UT = UT - 4*86400;
00281 if (TS.month() == 7) UT = UT - 5*86400;
00282 if (TS.month() == 8) UT = UT - 5*86400;
00283 if (TS.month() == 9) UT = UT - 5*86400;
00284 if (TS.month() == 10) UT = UT - 6*86400;
00285 if (TS.month() == 11) UT = UT - 6*86400;
00286 if (TS.month() == 12) UT = UT - 7*86400;
00287
00288 return UT;
00289 }
00290
00291
00292 template<>
00293 std::string PayLoadInspector<RPCObVmon>::plot(std::string const & filename,
00294 std::string const & str,
00295 std::vector<int> const&,
00296 std::vector<float> const& ) const {
00297
00298
00299
00300
00301
00302
00303 gStyle->SetPalette(1);
00304
00305 TH1D *vDistr=new TH1D("vDistr","IOV-averaged HV Distribution;Average HV (V);Entries/0.2 #muA",100,5000.,10000.);
00306 TH1D *rmsDistr=new TH1D("rmsDistr","RMS over IOV-HV Distribution;HV RMS (V);Entries/0.2 #muA",1000,0.,1000.);
00307
00308
00309
00310 std::string authPath="/afs/cern.ch/cms/DB/conddb";
00311 std::string conString="oracle://cms_orcoff_prod/CMS_COND_31X_RPC";
00312
00313
00314 std::string token="[DB=00000000-0000-0000-0000-000000000000][CNT=RPCObPVSSmap][CLID=53B2D2D9-1F4E-9CA9-4D71-FFCCA123A454][TECH=00000B01][OID=0000000C-00000000]";
00315
00316
00317 DbConnection dbConn;
00318
00319
00320 dbConn.configuration().setAuthenticationPath(authPath);
00321 dbConn.configure();
00322
00323
00324 DbSession dbSes=dbConn.createSession();
00325
00326
00327 dbSes.open(conString,true);
00328
00329
00330 dbSes.transaction().start(true);
00331
00332
00333 boost::shared_ptr<RPCObPVSSmap> pvssPtr;
00334 pvssPtr=dbSes.getTypedObject<RPCObPVSSmap>(token);
00335
00336
00337 std::vector<RPCObPVSSmap::Item> pvssCont=pvssPtr->ObIDMap_rpc;
00338
00339
00340 std::vector<RPCObVmon::V_Item> const & vmon = object().ObVmon_rpc;
00341
00342 std::map<int,std::pair<std::vector<double>,std::vector<double> > > dpidMap;
00343 for(unsigned int i = 0;i < vmon.size(); ++i){
00344 for(unsigned int p = 0; p < pvssCont.size(); ++p){
00345 if(vmon[i].dpid!=pvssCont[p].dpid || pvssCont[p].suptype!=0 || pvssCont[p].region!=0)continue;
00346 if(dpidMap.find(vmon[i].dpid)==dpidMap.end()){
00347 std::vector<double> dumVec1;dumVec1.push_back(vmon[i].value);
00348 std::vector<double> dumVec2;dumVec2.push_back((double)toUNIX(vmon[i].day,vmon[i].time));
00349 dpidMap[vmon[i].dpid]=make_pair(dumVec1,dumVec2);
00350 }
00351 else {
00352 dpidMap[vmon[i].dpid].first.push_back(vmon[i].value);
00353 dpidMap[vmon[i].dpid].second.push_back((double)toUNIX(vmon[i].day,vmon[i].time));
00354 }
00355 }
00356 }
00357
00358 double maxMean(-1.),maxRms(-1.);
00359 double minMean(9999.),minRms(9999.);
00360 std::map<int,std::pair<std::vector<double>,std::vector<double> > >::const_iterator minIt,maxIt;
00361 std::vector<float> means,rmss;
00362 for(std::map<int,std::pair<std::vector<double>,std::vector<double> > >::const_iterator mIt=dpidMap.begin();
00363 mIt!=dpidMap.end();mIt++){
00364 std::pair<double, std::vector<double> > meanAndVals =
00365 make_pair(accumulate(mIt->second.first.begin(),mIt->second.first.end(),0.)/(double)mIt->second.first.size(),mIt->second.first);
00366
00367 vDistr->Fill(meanAndVals.first);
00368 if(meanAndVals.first>maxMean)maxMean=meanAndVals.first;
00369 if(meanAndVals.first<minMean)minMean=meanAndVals.first;
00370 double rms(0.);
00371 for(std::vector<double>::iterator rmsIt=meanAndVals.second.begin();
00372 rmsIt!=meanAndVals.second.end();++rmsIt){
00373 rms+=pow((*rmsIt-meanAndVals.first)/(double)meanAndVals.second.size(),2);
00374 }
00375 rmsDistr->Fill(sqrt(rms));std::cout<<sqrt(rms)<<std::endl;
00376 if(sqrt(rms)>maxRms){
00377 maxRms=sqrt(rms);
00378 maxIt=mIt;
00379 }
00380 if(sqrt(rms)<minRms){
00381 minRms=sqrt(rms);
00382 if(mIt->second.first.size()>5)
00383 minIt=mIt;
00384 }
00385 means.push_back(meanAndVals.first);
00386 rmss.push_back(sqrt(rms));
00387 }
00388
00389 if(maxMean<100.)
00390 vDistr->GetXaxis()->SetRangeUser(minMean-0.00001,maxMean+1.);
00391 if(maxRms<1000.)
00392 rmsDistr->GetXaxis()->SetRangeUser(minRms-0.00001,maxRms+1.);
00393
00394 TCanvas c("Vmon","Vmon",1200,700);
00395 c.Divide(2,2);
00396
00397
00398
00399 c.cd(1);
00400 vDistr->SetFillColor(4);
00401 vDistr->SetLineColor(4);
00402 vDistr->Draw();
00403
00404
00405
00406 c.cd(2);
00407 rmsDistr->SetFillColor(3);
00408 rmsDistr->SetLineColor(3);
00409 rmsDistr->Draw();
00410
00411 c.cd(3);
00412 TGraph *vRmsDistr=new TGraph(means.size(),static_cast<const float *>(&rmss[0]),static_cast<const float *>(&means[0]));
00413 vRmsDistr->SetMarkerStyle(7);
00414 vRmsDistr->SetMarkerColor(2);
00415
00416
00417 vRmsDistr->GetXaxis()->SetTitle("HV RMS (V)");
00418 vRmsDistr->GetYaxis()->SetTitle("HV Means (V)");
00419 vRmsDistr->Draw("AP");
00420
00421 c.cd(4);
00422 TMultiGraph *mProf=new TMultiGraph();
00423 TLegend *leg=new TLegend(0.65,0.91,0.99,0.99);
00424 TGraph *minProf=new TGraph(minIt->second.first.size(),&minIt->second.second[0],&minIt->second.first[0]);
00425 TGraph *maxProf=new TGraph(maxIt->second.first.size(),&maxIt->second.second[0],&maxIt->second.first[0]);
00426 minProf->SetMarkerStyle(20);
00427 maxProf->SetMarkerStyle(20);
00428 minProf->SetMarkerColor(2);
00429 maxProf->SetMarkerColor(4);
00430 mProf->Add(minProf);
00431 leg->AddEntry(minProf,"V vs IOV for Min RMS case","lpf");
00432 mProf->Add(maxProf);
00433 leg->AddEntry(maxProf,"V vs IOV for Max RMS case","lpf");
00434 leg->Draw();
00435 mProf->Draw("AP");
00436 mProf->GetXaxis()->SetTitle("IOV");
00437 mProf->GetYaxis()->SetTitle("I (V)");
00438
00439 c.SaveAs(filename.c_str());
00440
00441 return filename.c_str();
00442
00443 }
00444
00445
00446 template <>
00447 std::string PayLoadInspector<RPCObVmon>::trend_plot(std::string const & filename,
00448 std::string const & opt_string,
00449 std::vector<int> const& nts,
00450 std::vector<float> const& floats,
00451 std::vector<std::string> const& strings) const {
00452
00453 std::stringstream ss("");
00454
00455 if(strings.size()<2)
00456 return ("Error!Not enough data for initializing connection for making plots!(from template<> std::string PayLoadInspector<BeamSpotObjects>::trend_plot)");
00457
00458 std::vector<std::string>::const_iterator strIt=strings.begin();
00459
00460 std::string conString=(*strIt);strIt++;
00461 std::string authPath=(*strIt);strIt++;
00462
00463
00464
00465
00466 DbConnection dbConn;
00467
00468
00469 dbConn.configuration().setAuthenticationPath(authPath);
00470 dbConn.configure();
00471
00472
00473 DbSession dbSes=dbConn.createSession();
00474
00475
00476 dbSes.open(conString,true);
00477
00478
00479 dbSes.transaction().start(true);
00480
00481 std::string pvssToken="[DB=00000000-0000-0000-0000-000000000000][CNT=RPCObPVSSmap][CLID=53B2D2D9-1F4E-9CA9-4D71-FFCCA123A454][TECH=00000B01][OID=0000000C-00000000]";
00482
00483 boost::shared_ptr<RPCObVmon> vmonPtr;
00484 boost::shared_ptr<RPCObPVSSmap> pvssPtr;
00485 pvssPtr=dbSes.getTypedObject<RPCObPVSSmap>(pvssToken);
00486
00487
00488 std::vector<RPCObPVSSmap::Item> pvssCont=pvssPtr->ObIDMap_rpc,pvssZero;
00489
00490 for(unsigned int p=0;p<pvssCont.size();p++){
00491 if(pvssCont[p].suptype!=0)
00492 pvssZero.push_back(pvssCont[p]);
00493 }
00494
00495 std::string token;
00496 std::vector<float> vecI;vecI.assign(floats.size(),0.);
00497 int elCount(0);
00498
00499 for(;strIt!=strings.end();++strIt){
00500
00501 token=(*strIt);
00502 vmonPtr=dbSes.getTypedObject<RPCObVmon>(token);
00503 std::vector<RPCObVmon::V_Item> const & vmon = vmonPtr->ObVmon_rpc;
00504
00505 float iCount(0.);
00506 for(unsigned int i=0;i<vmon.size();i++){
00507 for(unsigned int p=0;p<pvssZero.size();p++){
00508 if(vmon[i].dpid!=pvssZero[p].dpid || pvssZero[p].region!=0)continue;
00509 iCount++;
00510 vecI[elCount]+=vmon[i].value;
00511 }
00512 }
00513 if(iCount!=0)
00514 vecI[elCount]/=iCount;
00515 elCount++;
00516 }
00517
00518
00519
00520 dbSes.close();
00521
00522 TGraph trend(vecI.size(),static_cast<const float *>(&floats[0]),static_cast<const float *>(&vecI[0]));
00523 trend.SetLineColor(2);
00524 trend.SetLineWidth(2);
00525 trend.GetYaxis()->SetTitle("<V> (V)");
00526
00527 float min(*(floats.begin())),max(*(floats.end()-1));
00528 float scaleToggle((max-min)/max);
00529 TCanvas c("Itrend","Itrend",1200,700);
00530
00531 if(scaleToggle>=0.1)
00532 c.SetLogx(1);
00533
00534 trend.Draw("LA*");
00535
00536 TLegend leg(0.65,0.91,0.99,0.99);
00537 leg.AddEntry(&trend,"Vmon trend","lpf");
00538
00539 leg.Draw("");
00540
00541 ss.str("");
00542 ss<<filename<<".C";
00543 c.SaveAs((ss.str()).c_str());
00544
00545 return ss.str();
00546
00547 }
00548
00549 }
00550
00551
00552 namespace condPython {
00553 template<>
00554 void defineWhat<RPCObVmon>() {
00555 using namespace boost::python;
00556 enum_<cond::rpcobvmon::How>("How")
00557 .value("detid",cond::rpcobvmon::detid)
00558 .value("day",cond::rpcobvmon::day)
00559 .value("time",cond::rpcobvmon::time)
00560 .value("current",cond::rpcobvmon::current)
00561 ;
00562
00563 typedef cond::ExtractWhat<RPCObVmon> What;
00564 class_<What>("What",init<>())
00565 .def("set_how",&What::set_how)
00566 .def("set_which",&What::set_which)
00567 .def("how",&What::how, return_value_policy<copy_const_reference>())
00568 .def("which",&What::which, return_value_policy<copy_const_reference>())
00569 .def("set_starttime",&What::set_starttime)
00570 .def("set_endtime",&What::set_endtime)
00571 .def("startTime",&What::startTime, return_value_policy<copy_const_reference>())
00572 .def("endTime",&What::endTime, return_value_policy<copy_const_reference>())
00573 ;
00574 }
00575 }
00576
00577 PYTHON_WRAPPER(RPCObVmon,RPCObVmon);
00578
00579
00580