CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/DQM/L1TMonitor/src/BxTiming.cc

Go to the documentation of this file.
00001 #include "DQM/L1TMonitor/interface/BxTiming.h"
00002 
00003 #include "DataFormats/L1GlobalTrigger/interface/L1GlobalTriggerReadoutRecord.h"
00004 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00005 #include "DataFormats/FEDRawData/interface/FEDHeader.h"
00006 #include "DataFormats/FEDRawData/interface/FEDTrailer.h"
00007 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
00008 #include <cstdio>
00009 
00010 BxTiming::BxTiming(const edm::ParameterSet& iConfig) {
00011 
00012   verbose_ = iConfig.getUntrackedParameter<int>("VerboseFlag",0);
00013   if(verbose())
00014     std::cout << "BxTiming::BxTiming()...\n" << std::flush;
00015 
00016   fedRef_ = iConfig.getUntrackedParameter<int>("ReferenceFedId",813);
00017   fedSource_ = iConfig.getUntrackedParameter<edm::InputTag>
00018     ("FedSource",edm::InputTag("source"));
00019   gtSource_ = iConfig.getUntrackedParameter<edm::InputTag>
00020     ("GtSource",edm::InputTag("gtUnpack"));
00021   histFile_ = iConfig.getUntrackedParameter<std::string>
00022     ("HistFile","");
00023   histFolder_ = iConfig.getUntrackedParameter<std::string>
00024     ("HistFolder", "L1T/BXSynch/");
00025 
00026   runInFF_ = iConfig.getUntrackedParameter<bool> ("RunInFilterFarm", true);
00027  if(runInFF_) histFolder_ = "L1T/BXSynch_EvF/";
00028   if(verbose())
00029     std::cout << "Filter farm run setting?" << runInFF_
00030               << "\n" << std::flush;
00031 
00032   listGtBits_ = iConfig.getUntrackedParameter<std::vector<int> > ("GtBitList", std::vector<int>(1,0));
00033   if(listGtBits_.size()==1 && listGtBits_.at(0)==-1) {
00034     int ngtbits = 128;
00035     listGtBits_.reserve(ngtbits); 
00036     for(int i=0; i<ngtbits; i++) 
00037       listGtBits_[i]=i;
00038   }
00039 
00040   if(verbose()) {
00041     std::cout << "BxTiming: gt bits set for timing dqm:";
00042     std::cout << "nbits:" << listGtBits_.size() << " list: " ;
00043     for(size_t i=0; i!=listGtBits_.size(); i++) 
00044       std::cout << listGtBits_.at(i) << " " ;
00045     std::cout << "\n" << std::flush;
00046   }
00047 
00048   nfed_ = FEDNumbering::MAXFEDID+1;
00049 
00050   dbe = NULL;
00051   if (iConfig.getUntrackedParameter<bool>("DQMStore", false)) { 
00052     dbe = edm::Service<DQMStore>().operator->();
00053     dbe->setVerbose(0);
00054   }
00055   
00056   if(dbe!=NULL)
00057     dbe->setCurrentFolder(histFolder_);
00058   
00059   nEvt_ = 0;
00060   
00061   if(verbose())
00062     std::cout << "BxTiming::BxTiming constructor...done.\n" << std::flush;
00063 }
00064 
00065 BxTiming::~BxTiming() {}
00066 
00067 void 
00068 BxTiming::beginJob(void) {
00069 
00070   if(verbose())
00071     std::cout << "BxTiming::beginJob()  start\n" << std::flush;
00072 
00073   DQMStore* dbe = 0;
00074   dbe = edm::Service<DQMStore>().operator->();
00075   if(dbe) {
00076     dbe->setCurrentFolder(histFolder_);
00077     //dbe->rmdir(histFolder_);
00078   }
00079 
00081   for(int i=0; i<nfed_;i++) {
00082     nBxDiff[i][0]=0; nBxDiff[i][1]=nbig_; nBxDiff[i][2]=-1*nbig_;
00083     nBxOccy[i][0]=0; nBxOccy[i][1]=nbig_; nBxOccy[i][2]=-1*nbig_;
00084   }
00085 
00086   std::string lbl("");
00087   std::string SysLabel[NSYS] = {
00088     "PreShower", "ECAL", "HCAL", "GCT", "CSCTPG", "CSCTF", "DTTPG", "DTTF", "RPC", "GT"
00089   };
00090 
00091   typedef std::pair<int, int> FEDRange; 
00092  
00093   std::pair<int,int> fedRange[NSYS] = {
00094     FEDRange(FEDNumbering::MINPreShowerFEDID, FEDNumbering::MAXPreShowerFEDID),     //520..575
00095     FEDRange(FEDNumbering::MINECALFEDID, FEDNumbering::MAXECALFEDID),               //600..670
00096     FEDRange(FEDNumbering::MINHCALFEDID, FEDNumbering::MAXHCALFEDID),               //700..731
00097     FEDRange(FEDNumbering::MINTriggerGCTFEDID, FEDNumbering::MAXTriggerEGTPFEDID),  //745..749
00098     FEDRange(FEDNumbering::MINCSCFEDID, FEDNumbering::MAXCSCFEDID),                 //750..757
00099     FEDRange(FEDNumbering::MINCSCTFFEDID, FEDNumbering::MAXCSCTFFEDID),             //760..760
00100     FEDRange(FEDNumbering::MINDTFEDID, FEDNumbering::MAXDTFEDID),                   //770..775
00101     FEDRange(FEDNumbering::MINDTTFFEDID, FEDNumbering::MAXDTTFFEDID),               //780..780
00102     FEDRange(FEDNumbering::MINRPCFEDID, FEDNumbering::MAXRPCFEDID),                 //790..795
00103     FEDRange(FEDNumbering::MINTriggerGTPFEDID, FEDNumbering::MAXTriggerGTPFEDID)    //812..813
00104   };
00105   for(int i=0; i<NSYS; i++) fedRange_[i]=fedRange[i];
00106 
00107 
00108   int fedRefSys=-1;
00109   for(int i=0; i<NSYS; i++)
00110     if(fedRef_>=fedRange_[i].first && fedRef_<=fedRange_[i].second)
00111       {fedRefSys=i; break;}
00112   std::string refName("");
00113   std::string spreadLabel[nspr_] = {"Spread","Min", "Max"};
00114   if(fedRefSys>=0)
00115     refName+=SysLabel[fedRefSys];
00116   else
00117     refName+=fedRef_;
00118 
00120 
00121   const int dbx = nbig_;
00122 
00123   if(dbe) {
00124 
00125     dbe->setCurrentFolder(histFolder_);
00126 
00127     hBxDiffAllFed = dbe->bookProfile("BxDiffAllFed", "BxDiffAllFed", 
00128                                      nfed_ + 1, -0.5, nfed_+0.5, 
00129                                      2*dbx+1, -1*dbx-0.5,dbx+0.5
00130                                      );
00131 
00132     for(int i=0; i<nspr_; i++) {
00133       lbl.clear();lbl+="BxDiffAllFed";lbl+=spreadLabel[i];
00134       hBxDiffAllFedSpread[i] = dbe->book1D(lbl.data(),lbl.data(), nfed_ + 1, -0.5, nfed_+0.5); 
00135       lbl.clear();lbl+="BxOccyAllFed";lbl+=spreadLabel[i];
00136       hBxOccyAllFedSpread[i] = dbe->book1D(lbl.data(),lbl.data(), nfed_ + 1, -0.5, nfed_+0.5); 
00137     }
00138 
00139     lbl.clear();lbl+="BxOccyAllFed";
00140     hBxOccyAllFed = dbe->book1D(lbl.data(),lbl.data(),norb_+1,-0.5,norb_+0.5);
00141 
00142   }
00143 
00144   // following histos defined only when not runing in the ff
00145   if(dbe && !runInFF_) {
00146     
00147     dbe->setCurrentFolder(histFolder_);
00148     
00149     for(int i=0; i<NSYS; i++) {
00150       lbl.clear();lbl+=SysLabel[i];lbl+="FedBxDiff"; 
00151       int nfeds = fedRange_[i].second - fedRange_[i].first + 1;
00152       nfeds = (nfeds>0)? nfeds:1;
00153       hBxDiffSysFed[i] = dbe->bookProfile(lbl.data(),lbl.data(), nfeds, 
00154                                           fedRange_[i].first-0.5, fedRange_[i].second+0.5,
00155                                           2*dbx+1,-1*dbx-0.5,dbx+0.5);
00156                                           
00157     }
00158 
00159 
00160     lbl.clear();lbl+="BxOccyAllFed";
00161     hBxOccyOneFed = new MonitorElement*[nfed_];
00162     dbe->setCurrentFolder(histFolder_+"SingleFed/");
00163     for(int i=0; i<nfed_; i++) {
00164       lbl.clear(); lbl+="BxOccyOneFed";
00165       char *ii = new char[1000]; std::sprintf(ii,"%d",i);lbl+=ii;
00166       hBxOccyOneFed[i] = dbe->book1D(lbl.data(),lbl.data(),norb_+1,-0.5,norb_+0.5);
00167       delete [] ii;
00168     }
00169 
00170     dbe->setCurrentFolder(histFolder_);
00171     for(int i=0; i<nttype_; i++) {
00172       lbl.clear();lbl+="BxOccyGtTrigType";
00173       char *ii = new char[10]; std::sprintf(ii,"%d",i+1);lbl+=ii;
00174       hBxOccyGtTrigType[i] = dbe->book1D(lbl.data(),lbl.data(),norb_+1,-0.5,norb_+0.5);
00175       delete [] ii;
00176     }
00177 
00178     dbe->setCurrentFolder(histFolder_+"SingleBit/");
00179     for(int i=0; i<NSYS; i++) {
00180       hBxOccyTrigBit[i] = new MonitorElement*[listGtBits_.size()];
00181       for(size_t j=0; j<listGtBits_.size(); j++) {
00182         lbl.clear();lbl+=SysLabel[i];lbl+="BxOccyGtBit"; 
00183         char *ii = new char[1000]; std::sprintf(ii,"%d",listGtBits_.at(j)); lbl+=ii;
00184         hBxOccyTrigBit[i][j] = dbe->book1D(lbl.data(),lbl.data(),norb_+1,-0.5,norb_+0.5);
00185         delete [] ii;
00186       }
00187     }
00188 
00189   }
00190   
00192   hBxDiffAllFed->setAxisTitle("FED ID",1);
00193   lbl.clear(); lbl+="BX(fed)-BX("; lbl+=refName; lbl+=")";
00194   hBxDiffAllFed->setAxisTitle(lbl,2);
00195   for(int i=0; i<nspr_; i++) {
00196     lbl.clear(); lbl+="BX(fed)-BX("; lbl+=refName; lbl+=") "+spreadLabel[i];
00197     hBxDiffAllFedSpread[i]->setAxisTitle("FED ID",1);
00198     hBxDiffAllFedSpread[i]->setAxisTitle(lbl,2);
00199     lbl.clear(); lbl+="Bx FED occupancy"; lbl+=" "; lbl+=spreadLabel[i]; 
00200     hBxOccyAllFedSpread[i]->setAxisTitle("FED ID",1); 
00201     hBxOccyAllFedSpread[i]->setAxisTitle(lbl,2);
00202   }
00203 
00204   hBxOccyAllFed->setAxisTitle("bx",1);
00205   lbl.clear(); lbl+="Combined FED occupancy";
00206   hBxOccyAllFed->setAxisTitle(lbl,2);
00207  
00208   // skip next if running in filter farm
00209   if(runInFF_)
00210     return;
00211 
00212   for(int i=0; i<NSYS; i++) {
00213     lbl.clear(); lbl+=SysLabel[i]; lbl+=" FED ID";
00214     hBxDiffSysFed[i]->setAxisTitle(lbl,1);
00215     lbl.clear(); lbl+="BX("; lbl+=SysLabel[i]; lbl+=")-BX(";lbl+=refName; lbl+=")";
00216     hBxDiffSysFed[i]->setAxisTitle(lbl,2);
00217   }
00218 
00219   for(int i=0; i<nfed_; i++) {
00220     hBxOccyOneFed[i] ->setAxisTitle("bx",1);
00221     lbl.clear(); lbl+=" FED "; char *ii = new char[1000]; std::sprintf(ii,"%d",i);lbl+=ii; lbl+=" occupancy";
00222     hBxOccyOneFed[i] ->setAxisTitle(lbl,2);
00223     delete [] ii;
00224   }
00225   for(int i=0; i<nttype_; i++) {
00226     hBxOccyGtTrigType[i]->setAxisTitle("bx",1);
00227     lbl.clear(); lbl+="GT occupancy for trigger type "; char *ii = new char[10]; std::sprintf(ii,"%d",i+1);lbl+=ii;
00228     hBxOccyGtTrigType[i]->setAxisTitle(lbl,2);
00229     delete [] ii;
00230   }
00231   
00232   for(int i=0; i<NSYS; i++) {
00233     for(size_t j=0; j<listGtBits_.size(); j++) {
00234       hBxOccyTrigBit[i][j]->setAxisTitle("bx",1);
00235       lbl.clear();lbl+=SysLabel[i];lbl+=" Bx occupancy for Trigger bit "; 
00236       char *ii = new char[10]; std::sprintf(ii,"%d",listGtBits_.at(j)); lbl+=ii;
00237       hBxOccyTrigBit[i][j]->setAxisTitle(lbl,2);
00238       delete [] ii;
00239     }
00240   }
00241     
00242   if(verbose())
00243     std::cout << "BxTiming::beginJob()  end.\n" << std::flush;
00244 }
00245 
00246 void 
00247 BxTiming::endJob() {
00248 
00249   if(verbose())
00250     std::cout << "BxTiming::endJob Nevents: " << nEvt_ << "\n" << std::flush;
00251 
00252   if(histFile_.size()!=0  && dbe) 
00253     dbe->save(histFile_);
00254   
00255   if(verbose())
00256     std::cout << "BxTiming::endJob()  end.\n" << std::flush;
00257 }
00258 
00259 
00260 // ------------ method called to for each event  ------------
00261 void
00262 BxTiming::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
00263   
00264   if(verbose())
00265     std::cout << "BxTiming::analyze()  start\n" << std::flush;
00266 
00267   nEvt_++;
00268 
00270   edm::Handle<FEDRawDataCollection> rawdata;
00271   iEvent.getByLabel(fedSource_, rawdata);
00272   //iEvent.getByType(rawdata);
00273 
00274   // get the GT bits
00275   edm::Handle<L1GlobalTriggerReadoutRecord> gtdata;
00276   iEvent.getByLabel(gtSource_, gtdata);
00277   std::vector<bool> gtbits;
00278   int ngtbits = 128;
00279   gtbits.reserve(ngtbits); for(int i=0; i<ngtbits; i++) gtbits[i]=false;
00280   if(gtdata.isValid())
00281     gtbits = gtdata->decisionWord();
00282   
00283   if(gtbits.size()==0) {
00284     gtbits.push_back(true); // gtdata->decision();
00285     if(verbose())
00286       std::cout << "BxTiming::analyze() | unexpected empty decision bits!";
00287   }
00288 
00289   if(verbose()) {
00290     std::cout << "BxTiming::analyze()  gt data valid:" << (int)(gtdata.isValid()?0:1)
00291               << " decision word size:" << (int)(gtbits.size()) << "  bits: ";
00292     for(size_t i=0; i!=gtbits.size(); i++) {
00293       int ii = gtbits.at(i)? 1:0;
00294       std::cout << ii;
00295     }
00296     std::cout << ".\n" << std::flush;
00297   }
00298 
00299 
00300   // get reference bx
00301   int bxRef = FEDHeader(rawdata->FEDData(fedRef_).data()).bxID();
00302 
00303   // triggerType
00304   //trigger types: physics (1), calibration (2), random (3), traced physics (5),  test (6) 
00305   int ttype = FEDHeader(rawdata->FEDData(812).data()).triggerType();
00306 
00307   // loop over feds
00308   for (int i = 0; i<FEDNumbering::MAXFEDID; i++){
00309     const FEDRawData& data = rawdata->FEDData(i);
00310     size_t size=data.size();
00311     
00312     if(!size) continue;
00313     FEDHeader header(data.data());
00314     //int lvl1id = header.lvl1ID();//Level-1 event number generated by the TTC system
00315     int bx = header.bxID(); // The bunch crossing number
00316 
00317     int bxDiff = calcBxDiff(bx,bxRef); // deviation from reference bx
00318 
00319     //min
00320     if(nBxDiff[i][1]>bxDiff) nBxDiff[i][1] = bxDiff;
00321     if(nBxOccy[i][1]>bx    ) nBxOccy[i][1] = bx;
00322     //max
00323     if(nBxDiff[i][2]<bxDiff) nBxDiff[i][2] = bxDiff;
00324     if(nBxOccy[i][2]<bx    ) nBxOccy[i][2] = bx;
00325 
00326     if(verbose())
00327       std::cout << " fed:" <<  i 
00328                 << " bx:" << bx 
00329                 << " bxRef:" << bxRef
00330                 << " diff:" << bxDiff 
00331                 << " nBxDiff"<<" del:"<<nBxDiff[i][0]<<" min:"<<nBxDiff[i][1]<<" max:"<<nBxDiff[i][2]
00332                 << " nBxOccy"<<" del:"<<nBxOccy[i][0]<<" min:"<<nBxOccy[i][1]<<" max:"<<nBxOccy[i][2]
00333                 << "\n" << std::flush;
00334 
00335     hBxDiffAllFed->Fill(i,bxDiff);
00336     
00337     //if(ttype==1) //skip if not a physics trigger
00338     hBxOccyAllFed->Fill(bx);
00339 
00340 
00341     // done if running in filter farm
00342     if(runInFF_)
00343       continue;
00344 
00345     for(int j=0; j<NSYS; j++)
00346       if(i>=fedRange_[j].first && i<=fedRange_[j].second)
00347           hBxDiffSysFed[j]->Fill(i,bxDiff);
00348          
00349     for(size_t k=0; k!=listGtBits_.size(); k++) {
00350       if((int)gtbits.size() <= listGtBits_.at(k)) {
00351         if(verbose()) 
00352           std::cout << "BxTiming analyze | problem with vector size!\n" << std::endl;
00353         continue;
00354       }
00355       else if(!gtbits.at(listGtBits_.at(k))) 
00356         continue;
00357       for(int j=0; j<NSYS; j++) {
00358         if(i>=fedRange_[j].first && i<=fedRange_[j].second) {
00359           hBxOccyTrigBit[j][k]->Fill(bx);
00360         }
00361       }
00362     }
00363 
00364     if(i>=fedRange_[GLT].first && i<=fedRange_[GLT].second) //GT fed
00365       if(ttype<nttype_)
00366         hBxOccyGtTrigType[ttype-1]->Fill(bx);
00367 
00368     if(ttype!=1) continue; //skip if not a physics trigger
00369     //hBxOccyAllFed->Fill(bx);
00370     hBxOccyOneFed[i]->Fill(bx);
00371 
00372   }
00373 
00374   for(int i=0; i<nfed_;i++) {
00375     nBxDiff[i][0]=nBxDiff[i][2]-nBxDiff[i][1]; 
00376     nBxOccy[i][0]=nBxOccy[i][2]-nBxOccy[i][1];
00377     if(nBxDiff[i][0]<0 || nBxOccy[i][0]<0) continue;
00378     for(int j=0; j<nspr_; j++) {
00379       hBxDiffAllFedSpread[j]->setBinContent(i,nBxDiff[i][j]);      
00380       hBxOccyAllFedSpread[j]->setBinContent(i,nBxOccy[i][j]);      
00381     }
00382     if(verbose())
00383       std::cout << "BxTiming fed:" << i 
00384                 << " Bx-Bx(" << fedRef_ << ")::" 
00385                 << " del:" << nBxDiff[i][0]
00386                 << " min:" << nBxDiff[i][1]
00387                 << " max:" << nBxDiff[i][2]
00388                 << " Occy: "
00389                 << " del:" << nBxOccy[i][0]
00390                 << " min:" << nBxOccy[i][1]
00391                 << " max:" << nBxOccy[i][2]
00392                 <<"\n" << std::flush;
00393 
00394   }
00395 
00396 
00397   if(verbose())
00398     std::cout << "BxTiming::analyze() end.\n" << std::flush;
00399 }
00400 
00401 //----------------------------------------------------------------------
00402 
00403 int 
00404 BxTiming::calcBxDiff(int bx1, int bx2)
00405 {
00406   int diff = bx1 - bx2;
00407   
00408   while (diff < -half_norb_)
00409     diff += norb_;
00410   
00411   while (diff > half_norb_)
00412     diff -= norb_;
00413 
00414   return diff;
00415 }
00416 
00417 //----------------------------------------------------------------------