CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/DQM/RPCMonitorClient/src/RPCLinkSynchroStat.cc

Go to the documentation of this file.
00001 #include "DQM/RPCMonitorClient/interface/RPCLinkSynchroStat.h"
00002 #include "CondFormats/RPCObjects/interface/RPCReadOutMapping.h"
00003 #include "CondFormats/RPCObjects/interface/LinkBoardSpec.h"
00004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00005 
00006 #include <algorithm>
00007 #include <sstream>
00008 #include <iostream>
00009 #include <iomanip>
00010 
00011 bool RPCLinkSynchroStat::LessLinkName::operator()(const BoardAndCounts& o1, const BoardAndCounts& o2) { return o1.first < o2.first; }
00012 bool RPCLinkSynchroStat::LessCountSum::operator()(const BoardAndCounts& o1, const BoardAndCounts& o2) { return o1.second.sum()< o2.second.sum(); }
00013 
00014 void RPCLinkSynchroStat::add(const std::string & lbName, const unsigned int *hits)
00015 {
00016   LinkBoard lb(lbName);
00017   SynchroCounts counts(hits);
00018   for (std::vector<BoardAndCounts>::iterator it = theLinkStatMap.begin(); it != theLinkStatMap.end(); ++it)  if (it->first == lb) it->second += counts;
00019 }
00020 
00021 int RPCLinkSynchroStat::LinkBoard::add(const ChamberAndPartition & part) 
00022 {
00023   for (std::vector<ChamberAndPartition>::const_iterator it=theChamberAndPartitions.begin(); it != theChamberAndPartitions.end(); ++it) {
00024     if( (*it)==part) return 1;
00025   }
00026   theChamberAndPartitions.push_back(part);
00027   return 0;
00028 }
00029 
00030 int RPCLinkSynchroStat::LinkBoard::add(const LinkBoardElectronicIndex & ele) 
00031 {
00032   for (std::vector<LinkBoardElectronicIndex>::const_iterator it=theElePaths.begin(); it != theElePaths.end(); ++it) {
00033     if (    it->dccId == ele.dccId
00034          && it->dccInputChannelNum == ele.dccInputChannelNum
00035          && it->tbLinkInputNum == ele.tbLinkInputNum
00036          && it->lbNumInLink == ele.lbNumInLink) return 1; 
00037   }
00038   theElePaths.push_back(ele);
00039   return 0;
00040 }
00041 
00042 unsigned int RPCLinkSynchroStat::SynchroCounts::firstHit() const
00043 {
00044   for (unsigned int i=0; i <8 ; ++i) if(theCounts[i]) return i;
00045   return 8;
00046 }
00047 
00048 void  RPCLinkSynchroStat::SynchroCounts::set(unsigned int bxDiff)
00049 {
00050   if (bxDiff < 8) theCounts[bxDiff]=1;
00051 }
00052 
00053 void  RPCLinkSynchroStat::SynchroCounts::increment(unsigned int bxDiff)
00054 {
00055   if (bxDiff < 8) theCounts[bxDiff]++;
00056 }
00057 
00058 RPCLinkSynchroStat::SynchroCounts & RPCLinkSynchroStat::SynchroCounts::operator+=(const SynchroCounts &rhs)
00059 {
00060   for (unsigned int i=0; i<8; ++i) theCounts[i]+=rhs.theCounts[i];
00061   return *this;
00062 }
00063 
00064 unsigned int  RPCLinkSynchroStat::SynchroCounts::mom0() const
00065 { unsigned int result = 0; for (unsigned int i=0; i<8; ++i) result += theCounts[i]; return result; }
00066 
00067 double  RPCLinkSynchroStat::SynchroCounts::mom1() const
00068 { double result = 0.; for (unsigned int i=0; i<8; ++i) result += i*theCounts[i]; return result; }
00069 
00070 double  RPCLinkSynchroStat::SynchroCounts::mean() const
00071 { unsigned int sum = mom0(); return sum==0 ? 0. : mom1()/sum; }
00072 
00073 double  RPCLinkSynchroStat::SynchroCounts::rms() const
00074 {
00075   double result = 0.;
00076   int      sum = mom0();
00077   if (sum==0) return 0.;
00078   double mean = mom1()/sum;
00079   for (int i=0; i<8; ++i) result += theCounts[i]*(mean-i)*(mean-i);
00080   result /= sum;
00081   return sqrt(result);
00082 }
00083 
00084 std::string  RPCLinkSynchroStat::SynchroCounts::print() const
00085 {
00086   std::ostringstream str;
00087   str<<" mean: "<<std::setw(8)<<mean();
00088   str<<" rms: "<<std::setw(8)<<rms();
00089   str<<" counts:"; for (int i=0; i<8; ++i) str<<" "<<std::setw(4)<<theCounts[i];
00090   return str.str();
00091 }
00092 
00093 bool RPCLinkSynchroStat::SynchroCounts::operator==(const SynchroCounts &o) const
00094 {
00095   for (unsigned int idx=0; idx <8; ++idx) if (theCounts[idx] != o.theCounts[idx]) return false;
00096   return true;
00097 }
00098 
00099 
00100 
00101 
00102 
00103 RPCLinkSynchroStat::RPCLinkSynchroStat(bool useFirstFitOnly)
00104   : theUseFirstHitOnly(useFirstFitOnly)
00105 {
00106   for (unsigned int i1=0; i1<=MAXDCCINDEX; ++i1) {
00107     for (unsigned int i2=0; i2<=MAXRBCINDEX; i2++) {
00108       for (unsigned int i3=0; i3 <=MAXLINKINDEX; ++i3) {
00109         for (unsigned int i4=0; i4<=MAXLBINDEX; ++i4) {
00110           theLinkStatNavi[i1][i2][i3][i4]=0;
00111         }
00112       }
00113     }
00114   }
00115   theLinkStatMap.push_back( std::make_pair(LinkBoard("Dummy"), SynchroCounts()) );
00116 }
00117 
00118 void RPCLinkSynchroStat::init(const RPCReadOutMapping* theCabling, bool addChamberInfo)
00119 {
00120   if (!theCabling) return;
00121   std::vector<const DccSpec*> dccs = theCabling->dccList();
00122   for (std::vector<const DccSpec*>::const_iterator it1= dccs.begin(); it1!= dccs.end(); ++it1) {
00123     const std::vector<TriggerBoardSpec> & rmbs = (*it1)->triggerBoards();
00124     for (std::vector<TriggerBoardSpec>::const_iterator it2 = rmbs.begin(); it2 != rmbs.end(); ++it2) {
00125       const  std::vector<LinkConnSpec> & links = it2->linkConns();
00126       for (std::vector<LinkConnSpec>::const_iterator it3 = links.begin(); it3 != links.end(); ++it3) {
00127         const  std::vector<LinkBoardSpec> & lbs = it3->linkBoards();
00128         for (std::vector<LinkBoardSpec>::const_iterator it4=lbs.begin(); it4 != lbs.end(); ++it4) {
00129           LinkBoardElectronicIndex ele = { (*it1)->id(), it2->dccInputChannelNum(), it3->triggerBoardInputNumber(), it4->linkBoardNumInLink()}; 
00130           LinkBoard linkBoard(it4->linkBoardName());
00131           BoardAndCounts candid = std::make_pair(linkBoard,SynchroCounts());              
00132           std::vector<BoardAndCounts>::iterator candid_place = lower_bound(theLinkStatMap.begin(), theLinkStatMap.end(), candid, LessLinkName());
00133           if (candid_place != theLinkStatMap.end() && candid.first == candid_place->first) {
00134             candid_place->first.add(ele);
00135           } 
00136           else {
00137             candid_place = theLinkStatMap.insert(candid_place,candid);
00138             candid_place->first.add(ele);
00139             if (addChamberInfo) {
00140             const  std::vector<FebConnectorSpec> & febs = it4->febs(); 
00141             for (std::vector<FebConnectorSpec>::const_iterator it5=febs.begin(); it5!= febs.end(); ++it5) {
00142               std::string chamberName = it5->chamber().chamberLocationName();
00143               std::string partitionName = it5->feb().localEtaPartitionName();
00144               LinkBoard::ChamberAndPartition chamberAndPartition = std::make_pair(chamberName, partitionName);
00145               candid_place->first.add(chamberAndPartition);
00146             }
00147             } 
00148           }
00149         }
00150       } 
00151     }
00152   }
00153   for (unsigned int idx=0; idx<theLinkStatMap.size(); ++idx) {
00154     const std::vector<LinkBoardElectronicIndex> &paths= theLinkStatMap[idx].first.paths();
00155     for  (std::vector<LinkBoardElectronicIndex>::const_iterator it=paths.begin(); it!= paths.end();++it) {
00156       theLinkStatNavi[it->dccId-DCCINDEXSHIFT][it->dccInputChannelNum][it->tbLinkInputNum][it->lbNumInLink]=idx;
00157     }
00158   }
00159 //  LogTrace("RPCLinkSynchroStat") <<" SIZE OF LINKS IS: " << theLinkStatMap.size() << endl;
00160 }
00161 
00162 void RPCLinkSynchroStat::add(const RPCRawSynchro::ProdItem & vItem, std::vector<LinkBoardElectronicIndex> & problems)
00163 {
00164   std::vector< int > hits(theLinkStatMap.size(),0);
00165   std::vector<ShortLinkInfo> slis;
00166   for ( RPCRawSynchro::ProdItem::const_iterator it = vItem.begin(); it != vItem.end(); ++it) {
00167     const LinkBoardElectronicIndex & path = it->first;
00168     unsigned int bxDiff = it->second;
00169     unsigned int eleCode = (path.dccId-DCCINDEXSHIFT)*100000 + path.dccInputChannelNum*1000 + path.tbLinkInputNum*10+path.lbNumInLink;
00170     unsigned int idx = theLinkStatNavi[path.dccId-DCCINDEXSHIFT][path.dccInputChannelNum][path.tbLinkInputNum][path.lbNumInLink];
00171     if  (hits[idx]==0) {
00172       ShortLinkInfo sli = { idx, std::vector<unsigned int>(1, eleCode), SynchroCounts() };
00173       slis.push_back( sli );
00174       hits[idx]=slis.size();
00175     } 
00176     else {
00177       std::vector<unsigned int> & v = slis[hits[idx]-1].hit_paths;
00178       std::vector<unsigned int>::iterator iv = lower_bound (v.begin(), v.end(), eleCode);
00179       if (iv == v.end() || (*iv) != eleCode) v.insert(iv,eleCode);
00180     } 
00181     slis[hits[idx]-1].counts.set(bxDiff); // ensure one count per LB per BX
00182   }
00183 
00184   for (std::vector<ShortLinkInfo>::const_iterator ic = slis.begin(); ic !=slis.end(); ++ic) {
00185     if (theUseFirstHitOnly) {
00186       theLinkStatMap[ic->idx].second.increment( ic->counts.firstHit() );  // first hit only  
00187     } else  {
00188       theLinkStatMap[ic->idx].second += ic->counts;
00189     }
00190     if (theLinkStatMap[ ic->idx].first.paths().size() != ic->hit_paths.size()) {
00191        const std::vector<LinkBoardElectronicIndex> & paths =  theLinkStatMap[ ic->idx].first.paths();
00192        problems.insert(problems.end(),paths.begin(),paths.end());
00193     }
00194   }
00195 
00196 }
00197 
00198 std::string RPCLinkSynchroStat::dumpDelays() 
00199 {
00200   std::ostringstream str;
00201   std::vector<BoardAndCounts> sortedStat = theLinkStatMap;
00202   stable_sort(sortedStat.begin(),sortedStat.end(),LessCountSum()); 
00203   for (unsigned int idx=0; idx<sortedStat.size(); ++idx) {
00204     const LinkBoard & board = sortedStat[idx].first;
00205     const SynchroCounts & counts = sortedStat[idx].second;
00206 
00207     // DUMP LINKNAME
00208     str << std::setw(20) << board.name();
00209 
00210     // DUMP COUNTS
00211     str <<" "<<counts.print();
00212 
00213     //PATHS
00214     str <<" paths: ";
00215     const std::vector<LinkBoardElectronicIndex> & paths=board.paths();
00216     for (std::vector<LinkBoardElectronicIndex>::const_iterator ip=paths.begin(); ip!=paths.end();++ip) 
00217       str<<"{"<<ip->dccId<<","<<std::setw(2)<<ip->dccInputChannelNum<<","<<std::setw(2)<<ip->tbLinkInputNum<<","<<ip->lbNumInLink<<"}";
00218 
00219     // DUMP CHAMBERS
00220     std::map<std::string,std::vector<std::string> > chMap;
00221     const std::vector<LinkBoard::ChamberAndPartition> & chamberAndPartitions = board.chamberAndPartitions();
00222     for (std::vector<LinkBoard::ChamberAndPartition>::const_iterator it=chamberAndPartitions.begin(); it!=chamberAndPartitions.end();++it) {
00223       std::vector<std::string> & partitions = chMap[it->first];
00224       if (find(partitions.begin(), partitions.end(), it->second) == partitions.end()) partitions.push_back(it->second);
00225     }
00226     str << " chambers: ";
00227     for (std::map<std::string,std::vector<std::string> >::const_iterator im=chMap.begin(); im != chMap.end(); ++im) {
00228       str <<im->first<<"(";
00229       for (std::vector<std::string>::const_iterator ip=im->second.begin(); ip != im->second.end(); ++ip) { str << *ip; if ((ip+1)!= (im->second.end()) ) str<<","; else str <<")"; }
00230     }
00231 
00232     
00233     str <<std::endl;
00234   }
00235   LogTrace("RPCLinkSynchroStat") <<"RPCLinkSynchroStat::dumpDelays,  SIZE OF LINKS IS: " << theLinkStatMap.size() << std::endl;
00236   return str.str();
00237 }