CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/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 
00273   if (!rawdata.isValid()) {
00274 
00275         if (verbose())
00276             std::cout
00277                     << "BxTiming::analyze() | FEDRawDataCollection with input tag "
00278                     << fedSource_ << " not found.";
00279 
00280         return;
00281   }
00282 
00283   // get the GT bits
00284   edm::Handle<L1GlobalTriggerReadoutRecord> gtdata;
00285   iEvent.getByLabel(gtSource_, gtdata);
00286   std::vector<bool> gtbits;
00287   int ngtbits = 128;
00288   gtbits.reserve(ngtbits); for(int i=0; i<ngtbits; i++) gtbits[i]=false;
00289   if(gtdata.isValid())
00290     gtbits = gtdata->decisionWord();
00291   
00292   if(gtbits.size()==0) {
00293     gtbits.push_back(true); // gtdata->decision();
00294     if(verbose())
00295       std::cout << "BxTiming::analyze() | unexpected empty decision bits!";
00296   }
00297 
00298   if(verbose()) {
00299     std::cout << "BxTiming::analyze()  gt data valid:" << (int)(gtdata.isValid()?0:1)
00300               << " decision word size:" << (int)(gtbits.size()) << "  bits: ";
00301     for(size_t i=0; i!=gtbits.size(); i++) {
00302       int ii = gtbits.at(i)? 1:0;
00303       std::cout << ii;
00304     }
00305     std::cout << ".\n" << std::flush;
00306   }
00307 
00308 
00309   // get reference bx
00310   int bxRef = FEDHeader(rawdata->FEDData(fedRef_).data()).bxID();
00311 
00312   // triggerType
00313   //trigger types: physics (1), calibration (2), random (3), traced physics (5),  test (6) 
00314   int ttype = FEDHeader(rawdata->FEDData(812).data()).triggerType();
00315 
00316   // loop over feds
00317   for (int i = 0; i<FEDNumbering::MAXFEDID+1; i++){
00318     const FEDRawData& data = rawdata->FEDData(i);
00319     size_t size=data.size();
00320     
00321     if(!size) continue;
00322     FEDHeader header(data.data());
00323     //int lvl1id = header.lvl1ID();//Level-1 event number generated by the TTC system
00324     int bx = header.bxID(); // The bunch crossing number
00325 
00326     int bxDiff = calcBxDiff(bx,bxRef); // deviation from reference bx
00327 
00328     //min
00329     if(nBxDiff[i][1]>bxDiff) nBxDiff[i][1] = bxDiff;
00330     if(nBxOccy[i][1]>bx    ) nBxOccy[i][1] = bx;
00331     //max
00332     if(nBxDiff[i][2]<bxDiff) nBxDiff[i][2] = bxDiff;
00333     if(nBxOccy[i][2]<bx    ) nBxOccy[i][2] = bx;
00334 
00335     if(verbose())
00336       std::cout << " fed:" <<  i 
00337                 << " bx:" << bx 
00338                 << " bxRef:" << bxRef
00339                 << " diff:" << bxDiff 
00340                 << " nBxDiff"<<" del:"<<nBxDiff[i][0]<<" min:"<<nBxDiff[i][1]<<" max:"<<nBxDiff[i][2]
00341                 << " nBxOccy"<<" del:"<<nBxOccy[i][0]<<" min:"<<nBxOccy[i][1]<<" max:"<<nBxOccy[i][2]
00342                 << "\n" << std::flush;
00343 
00344     hBxDiffAllFed->Fill(i,bxDiff);
00345     
00346     //if(ttype==1) //skip if not a physics trigger
00347     hBxOccyAllFed->Fill(bx);
00348 
00349 
00350     // done if running in filter farm
00351     if(runInFF_)
00352       continue;
00353 
00354     for(int j=0; j<NSYS; j++)
00355       if(i>=fedRange_[j].first && i<=fedRange_[j].second)
00356           hBxDiffSysFed[j]->Fill(i,bxDiff);
00357          
00358     for(size_t k=0; k!=listGtBits_.size(); k++) {
00359       if((int)gtbits.size() <= listGtBits_.at(k)) {
00360         if(verbose()) 
00361           std::cout << "BxTiming analyze | problem with vector size!\n" << std::endl;
00362         continue;
00363       }
00364       else if(!gtbits.at(listGtBits_.at(k))) 
00365         continue;
00366       for(int j=0; j<NSYS; j++) {
00367         if(i>=fedRange_[j].first && i<=fedRange_[j].second) {
00368           hBxOccyTrigBit[j][k]->Fill(bx);
00369         }
00370       }
00371     }
00372 
00373     if(i>=fedRange_[GLT].first && i<=fedRange_[GLT].second) //GT fed
00374       if(ttype<nttype_)
00375         hBxOccyGtTrigType[ttype-1]->Fill(bx);
00376 
00377     if(ttype!=1) continue; //skip if not a physics trigger
00378     //hBxOccyAllFed->Fill(bx);
00379     hBxOccyOneFed[i]->Fill(bx);
00380 
00381   }
00382 
00383   for(int i=0; i<nfed_;i++) {
00384     nBxDiff[i][0]=nBxDiff[i][2]-nBxDiff[i][1]; 
00385     nBxOccy[i][0]=nBxOccy[i][2]-nBxOccy[i][1];
00386     if(nBxDiff[i][0]<0 || nBxOccy[i][0]<0) continue;
00387     for(int j=0; j<nspr_; j++) {
00388       hBxDiffAllFedSpread[j]->setBinContent(i,nBxDiff[i][j]);      
00389       hBxOccyAllFedSpread[j]->setBinContent(i,nBxOccy[i][j]);      
00390     }
00391     if(verbose())
00392       std::cout << "BxTiming fed:" << i 
00393                 << " Bx-Bx(" << fedRef_ << ")::" 
00394                 << " del:" << nBxDiff[i][0]
00395                 << " min:" << nBxDiff[i][1]
00396                 << " max:" << nBxDiff[i][2]
00397                 << " Occy: "
00398                 << " del:" << nBxOccy[i][0]
00399                 << " min:" << nBxOccy[i][1]
00400                 << " max:" << nBxOccy[i][2]
00401                 <<"\n" << std::flush;
00402 
00403   }
00404 
00405 
00406   if(verbose())
00407     std::cout << "BxTiming::analyze() end.\n" << std::flush;
00408 }
00409 
00410 //----------------------------------------------------------------------
00411 
00412 int 
00413 BxTiming::calcBxDiff(int bx1, int bx2)
00414 {
00415   int diff = bx1 - bx2;
00416   
00417   while (diff < -half_norb_)
00418     diff += norb_;
00419   
00420   while (diff > half_norb_)
00421     diff -= norb_;
00422 
00423   return diff;
00424 }
00425 
00426 //----------------------------------------------------------------------