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