CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_8_patch3/src/CalibTracker/SiPixelTools/plugins/SiPixelFedFillerWordEventNumber.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    SiPixelFedFillerWordEventNumber 
00004 // Class:      SiPixelFedFillerWordEventNumber 
00005 // 
00013 //
00014 // Original Author:  Andres Carlos FLOREZ B
00015 //         Created:  Thu Jun 26 09:02:02 CEST 2008
00016 // $Id: SiPixelFedFillerWordEventNumber.cc,v 1.6 2011/07/01 07:05:03 eulisse Exp $
00017 //
00018 //
00019 
00020 
00021 // system include files
00022 #include <memory>
00023 #include "SiPixelFedFillerWordEventNumber.h"
00024 
00025 //======= constructors and destructor
00026 SiPixelFedFillerWordEventNumber ::SiPixelFedFillerWordEventNumber (const edm::ParameterSet& iConfig)
00027 {
00028   SaveFillerWordsbool = iConfig.getParameter<bool>("SaveFillerWords");
00029   label     =iConfig.getUntrackedParameter<std::string>("InputLabel","source");
00030   instance = iConfig.getUntrackedParameter<std::string>("InputInstance",""); 
00031   produces<std::vector<uint32_t> > ("FillerWordEventNumber1");
00032   produces<std::vector<uint32_t> > ("FillerWordEventNumber2");
00033   if (SaveFillerWordsbool == true){
00034     produces<std::vector<uint32_t> > ("SaveFillerWord");
00035   }
00036 }
00037 
00038 SiPixelFedFillerWordEventNumber ::~SiPixelFedFillerWordEventNumber ()
00039 {
00040   
00041 }
00042 unsigned int SiPixelFedFillerWordEventNumber ::CalibStatFillWord(unsigned int totword, int status){
00043   //===== Variables to get each filler word out of the totword and 
00044   //      to conform the last 16 bit filler word if Filler3 is zero.   
00045   unsigned int Filler1      = (totword)&0x000000ff;
00046   unsigned int Filler2      = ((totword)&0x0000ff00)>>8;
00047   unsigned int Filler3      = ((totword)&0x00ff0000)>>16;
00048   unsigned int Filler4      = ((totword)&0xffff0000)>>16;
00049   unsigned int maskFiller4  = ((totword)&0xff000000)>>16;
00050   unsigned int Filler14     = (Filler1&maskFiller4);
00051   unsigned int Filler24     = (Filler2&maskFiller4);
00052   unsigned int CalibFiller1 = 0;
00053   unsigned int CalibFiller2 = 0;
00054   bool BoolStat = false;
00055   //=====Possible cases for the totword. "CalibFiller1" and "CalibFiller2" take their valur
00056   //     according to the value of the totword.
00057   if ((status == 0x1)||(status == 0x9)){
00058     CalibFiller1 = Filler1;
00059     if((status == 0x9)){CalibFiller2 = Filler14;}
00060   }
00061   if ((status == 0x2)||(status == 0xa)){
00062     CalibFiller1 = Filler2;
00063     if((status == 0xa)){CalibFiller2 = Filler24;}
00064   }
00065   if ((status == 0x4)||(status == 0xc)){
00066     CalibFiller1 = Filler3;
00067     if((status == 0xc)){CalibFiller2 = Filler4;}
00068   }
00069   if ((status == 0x8)){CalibFiller2 = Filler4;}  
00070   if((status == 0x7)||(status == 0xf)){
00071     if((Filler1 == Filler2)&&(Filler1 == Filler3)&&(Filler2 == Filler3)){
00072       CalibFiller1 = Filler1;
00073       BoolStat = true;
00074       if(status == 0xf){CalibFiller2 = Filler4;} 
00075     }else{
00076       edm::LogError("AnazrFedFillerWords")<<"Status: "<<status << "Event ID in Filler words don't match"
00077                                           <<'\t'<<"Filler1: "<<(Filler1%256)
00078                                           <<'\t'<<"Filler2: "<<(Filler2%256)
00079                                           <<'\t'<<"Filler3: "<<(Filler3%256)
00080                                           <<std::endl; 
00081     }
00082   }
00083   if((status == 0x3)||(status == 0xb)){
00084     if((Filler1 == Filler2)){
00085       CalibFiller1 = Filler1;
00086       BoolStat = true;
00087       if(status == 0xb){CalibFiller2 = Filler14;}
00088     }else{
00089       edm::LogError("AnazrFedFillerWords")<<"Status: "<<status << "Event ID in Filler words don't match"
00090                                           <<'\t'<<"Filler1: "<<(Filler1%256)
00091                                           <<'\t'<<"Filler2: "<<(Filler2%256)
00092                                           <<std::endl;
00093     }
00094   }
00095   if((status == 0x5)||(status == 0xd)){
00096     if((Filler1 == Filler3)){
00097       CalibFiller1 = Filler1;
00098       BoolStat = true;
00099       if(status == 0xd){CalibFiller2 = Filler4;}
00100     }else{
00101       edm::LogError("AnazrFedFillerWords")<<"Status: "<<status << "Event ID in Filler words don't match"
00102                                           <<'\t'<<"Filler1: "<<(Filler1%256)
00103                                           <<'\t'<<"Filler3: "<<(Filler3%256) 
00104                                           <<std::endl;
00105     }
00106   }
00107   if((status == 0x6)||(status == 0xe)){
00108     if((Filler2 == Filler3)){
00109       CalibFiller1 = Filler2;
00110       BoolStat = true;
00111       if(status == 0xe){CalibFiller2 = Filler4;}
00112     }else{
00113       edm::LogError("AnazrFedFillerWords")<<"Status: "<<status << "Event ID Filler words don't match"
00114                                           <<'\t'<<"Filler2: "<<(Filler2%256)
00115                                           <<'\t'<<"Filler3: "<<(Filler3%256) 
00116                                           <<std::endl;
00117     }
00118   }
00119   //===== Using the Event number from CMSSW to get a value to compare with the value encoded 
00120   //      in the filler words.
00121   unsigned int CalibEvtNum = ((EventNum -1)/10);
00122   if((CalibFiller1 != 0)&&(CalibEvtNum != CalibFiller1)){
00123     edm::LogError("AnazrFedFillerWords")<<"Error, Event ID Numbers Don't match---->"<<"Filler1 Event ID: "
00124                                         << CalibFiller1 <<'\t'<<"Run Event ID: "
00125                                         <<CalibEvtNum<<'\t'<<std::endl;
00126   }else if((CalibFiller1 != 0)&&(CalibEvtNum == CalibFiller1)){
00127     vecFillerWordsEventNumber1.push_back((CalibFiller1%256));
00128     edm::LogInfo("AnazrFedFillerWords")<<"Filler1 Event ID: "
00129                                        <<(CalibFiller1%256)<<std::endl;
00130   }else if((CalibFiller2 != 0)&&(BoolStat == true)){
00131     vecFillerWordsEventNumber2.push_back((((CalibFiller2%65536)&(0xff00))>>8));
00132     edm::LogInfo("AnazrFedFillerWords")<<"Filler2 Event ID:"
00133                                        <<(((CalibFiller2%65536)&(0xff00))>>8)<<std::endl;
00134   }else if((CalibFiller2 != 0)&&(BoolStat == false)){
00135     if((status == 0x9)||(status == 0xa)||(status == 0xc)){
00136       vecFillerWordsEventNumber2.push_back((((CalibFiller2%65536)&(0xff00))>>8));
00137       edm::LogInfo("AnazrFedFillerWords")<<"Filler2 Event ID:"<<(((CalibFiller2%65536)&(0xff00))>>8)<<std::endl;
00138     }else if((status == 0x8)){
00139       edm::LogError("AnazrFedFillerWords")<<"Status: "<<status 
00140                                           <<" No Filler1 found, is not possible get any Event ID Number"
00141                                           <<std::endl;
00142     }
00143   }
00144   
00145   return 0;
00146 } 
00147 //========== Function to decode data words ================================================== 
00148 int SiPixelFedFillerWordEventNumber ::PwordSlink64(uint64_t * ldata, const int length, uint32_t &totword) {
00149   edm::LogInfo("FedFillerWords") <<"Begin of data"<<std::endl;
00150   
00151   if( (ldata[0]&0xf000000000000000LL) != 0x5000000000000000LL )//header 
00152     {     
00153       return 0;
00154     }
00155   
00156   //========= analyze the data buffer to find private words ================================
00157   int fif2cnt=0;
00158   int dumcnt=0;
00159   int gapcnt=0;
00160   
00161   uint32_t gap[8];
00162   uint32_t dum[8];
00163   uint32_t word1=0;
00164   uint32_t word2=0;
00165   
00166   uint32_t chan=0;
00167   uint32_t roc=0;
00168   
00169   const uint32_t rocmsk  = 0x3e00000;
00170   const uint32_t chnlmsk = 0xfc000000;
00171   
00172   for(int jk=0;jk<8;jk++)gap[jk]=0;
00173   for(int jk=0;jk<8;jk++)dum[jk]=0;
00174   totword=0;
00175   int fifcnt=1;
00176   for(int kk=1;kk<length-1;kk++) {
00177     //======= if statement to make analize just data with the right format ===================   
00178     if((((ldata[kk]&0xff00000000000000LL)>>32) == 0xa0000000) 
00179        && (((ldata[kk]&0xffffff00000000LL)>>32)== (uint64_t)(kk+1))){break;}
00180     
00181     word2 = (uint32_t) ldata[kk];
00182     word1 = (uint32_t) (ldata[kk]>>32);
00183     
00184     //======= 1st word ======================================================================
00185     
00186     chan= ((word1&chnlmsk)>>26);
00187     roc= ((word1&rocmsk)>>21);
00188     
00189     //======count non-error words 
00190     if(roc<25){
00191       if(dumcnt>0){
00192         dumcnt=0;
00193       }//stale dummy!
00194       if((chan<5)&&(fifcnt!=1)){
00195         edm::LogError("FedFillerWords") <<" error in fifo counting!"<<std::endl;
00196       }
00197       if((chan>4)&&(chan<10)&&(fifcnt!=2)) {fif2cnt=0;fifcnt=2;}
00198       if((chan>9)&&(chan<14)&&(fifcnt!=3)) {fif2cnt=0;fifcnt=3;}
00199       if((chan>13)&&(chan<19)&&(fifcnt!=4)){fif2cnt=0;fifcnt=4;}
00200       if((chan>18)&&(chan<23)&&(fifcnt!=5)){fif2cnt=0;fifcnt=5;}
00201       if((chan>22)&&(chan<28)&&(fifcnt!=6)){fif2cnt=0;fifcnt=6;}
00202       if((chan>27)&&(chan<32)&&(fifcnt!=7)){fif2cnt=0;fifcnt=7;}
00203       if((chan>31)&&(fifcnt!=8)){fif2cnt=0;fifcnt=8;}
00204       fif2cnt++;
00205     }
00206     //====== Gap Word 
00207     if(roc==26){
00208       gap[fifcnt-1]=(0x1000+(word1&0xff));
00209       gapcnt++;
00210     }
00211     //====== Dummy Word 
00212     if((roc==27)&&((fif2cnt+dumcnt)<6)){
00213       dum[fifcnt-1]=(0x1000+(word1&0xff));
00214       dumcnt++;
00215     }
00216     else if((roc==27)&&((fif2cnt+dumcnt)>6)){
00217       dumcnt=1;
00218       fif2cnt=0;
00219       fifcnt++;
00220     }
00221     
00222     //======== 2nd word ============================================================
00223     
00224     chan= ((word2&chnlmsk)>>26);
00225     roc= ((word2&rocmsk)>>21);
00226     
00227     if(roc<25)
00228       {
00229         if(dumcnt>0){
00230           dumcnt=0;
00231           edm::LogInfo("FedFillerWords") <<" ***Stale dummy!"<<std::endl;
00232         }//stale dummy!
00233         if((chan<5)&&(fifcnt!=1)){
00234           edm::LogError("FedFillerWords") <<" error in fifo counting!"<<std::endl;
00235         }
00236         if((chan>4)&&(chan<10)&&(fifcnt!=2)) {fif2cnt=0;fifcnt=2;}
00237         if((chan>9)&&(chan<14)&&(fifcnt!=3)) {fif2cnt=0;fifcnt=3;}
00238         if((chan>13)&&(chan<19)&&(fifcnt!=4)){fif2cnt=0;fifcnt=4;}
00239         if((chan>18)&&(chan<23)&&(fifcnt!=5)){fif2cnt=0;fifcnt=5;}
00240         if((chan>22)&&(chan<28)&&(fifcnt!=6)){fif2cnt=0;fifcnt=6;}
00241         if((chan>27)&&(chan<32)&&(fifcnt!=7)){fif2cnt=0;fifcnt=7;}
00242         if((chan>31)&&(fifcnt!=8)){fif2cnt=0;fifcnt=8;}
00243         fif2cnt++;
00244       }
00245     if(roc==26){
00246       gap[fifcnt-1]=(0x1000+(word2&0xff));
00247       gapcnt++;
00248     }
00249     if ((roc==27)&&((fif2cnt+dumcnt)<6)){
00250       dum[fifcnt-1]=(0x1000+(word1&0xff));
00251       dumcnt++;
00252     }
00253     else if((roc==27)&&((fif2cnt+dumcnt)>6)){
00254       dumcnt=1;
00255       fif2cnt=0;
00256       fifcnt++;
00257     }
00258     
00259     //word check complete
00260     if(((fif2cnt+dumcnt)==6)&&(dumcnt>0)){ //done with this fifo 
00261       dumcnt=0;
00262       fif2cnt=0;
00263       fifcnt++;
00264     }
00265     if((gapcnt>0)&&((dumcnt+fif2cnt)>5)){//done with this fifo
00266       gapcnt=0;
00267       fifcnt++;
00268       fif2cnt=0;
00269       dumcnt=0;
00270     }
00271     else if((gapcnt>0)&&((dumcnt+fif2cnt)<6)){ 
00272       gapcnt=0;
00273     }  
00274     
00275   }//==End of fifo-3 word loop
00276   //========== FPGAs Status ==================================================
00277   status = 0;
00278   
00279   if(gap[0]>0) {
00280     totword=(gap[0]&0xff);
00281     status=1;
00282   }
00283   else if(gap[1]>0){
00284     totword=(gap[1]&0xff);
00285     status=1;
00286   }
00287   else if(dum[0]>0){
00288     totword=(dum[0]&0xff);
00289     status=1;
00290   }
00291   else if(dum[1]>0){
00292     totword=(dum[1]&0xff);
00293     status=1;
00294   }
00295   
00296   if(gap[2]>0) {
00297     totword=totword|((gap[2]&0xff)<<8);
00298     status=status|0x2;
00299   }
00300   else if(gap[3]>0){
00301     totword=totword|((gap[3]&0xff)<<8);
00302     status=status|0x2;
00303   }
00304   else if(dum[2]>0){
00305     totword=totword|((dum[2]&0xff)<<8);
00306     status=status|0x2;
00307   }
00308   else if(dum[3]>0){
00309     totword=totword|((dum[3]&0xff)<<8);
00310     status=status|0x2;
00311   }
00312   
00313   if(gap[4]>0) {
00314     totword=totword|((gap[4]&0xff)<<16);
00315     status=status|0x4;
00316   }
00317   else if(gap[5]>0){
00318     totword=totword|((gap[5]&0xff)<<16);
00319     status=status|0x4;
00320   }
00321   else if(dum[4]>0){
00322     totword=totword|((dum[4]&0xff)<<16);
00323     status=status|0x4;
00324   }
00325   else if(dum[5]>0){
00326     totword=totword|((dum[5]&0xff)<<16);
00327     status=status|0x4;
00328   }
00329   
00330   if(gap[6]>0){
00331     totword=totword|((gap[6]&0xff)<<24);
00332     status=status|0x8;
00333   }
00334   else if(gap[7]>0){
00335     totword=totword|((gap[7]&0xff)<<24);
00336     status=status|0x8;
00337   }
00338   else if(dum[6]>0){
00339     totword=totword|((dum[6]&0xff)<<24);
00340     status=status|0x8;
00341   }
00342   else if(dum[7]>0){
00343     totword=totword|((dum[7]&0xff)<<24);
00344     status=status|0x8;
00345   }
00346   vecSaveFillerWords.push_back(totword);
00347   if((EventNum%10) == 0){
00348   CalibStatFill = CalibStatFillWord(totword, status);
00349   }
00350   edm::LogInfo("FedFillerWords") <<"total word = 0x"
00351                                  <<std::hex<<totword
00352                                  <<std::hex<<" Status = 0x"
00353                                  <<status<<std::dec<<std::endl;
00354   return(status);
00355   
00356 }
00357 
00358 void
00359 SiPixelFedFillerWordEventNumber ::produce(edm::Event& iEvent, const edm::EventSetup& iSetup)
00360 { 
00361   EventNum = iEvent.id().event();
00362   edm::Handle<FEDRawDataCollection> buffers;  
00363   iEvent.getByLabel( label, instance, buffers);
00364   std::auto_ptr<std::vector<uint32_t> > FillerWordEventNumbers1(new std::vector<uint32_t>);
00365   std::auto_ptr<std::vector<uint32_t> > FillerWordEventNumbers2(new std::vector<uint32_t>);
00366   std::auto_ptr<std::vector<uint32_t> > SaveFillerWords(new std::vector<uint32_t>);
00367   //===== Loop over all the FEDs ========================================================
00368   FEDNumbering fednum;
00369   std::pair<int,int> fedIds;
00370   fedIds.first  = 0;
00371   fedIds.second = 39; 
00372   
00373   for (int fedId = fedIds.first; fedId <= fedIds.second; fedId++) {
00374     edm::LogInfo("FedFillerWords") << " examining FED: " << fedId << std::endl; 
00375     const FEDRawData& fedRawData = buffers->FEDData( fedId ); //get event data for this fed
00376     //======== Run the fill word finder...
00377     if(fedRawData.size()!= 0){
00378       uint32_t totword;
00379       int value = PwordSlink64((uint64_t*)fedRawData.data(),(int)fedRawData.size(), totword);
00380       if(value!=0){
00381         //====== Verify that the vector is not empty
00382         if(vecSaveFillerWords.size()!=0){
00383           for(vecSaveFillerWords_It = vecSaveFillerWords.begin(); vecSaveFillerWords_It != vecSaveFillerWords.end(); vecSaveFillerWords_It++){
00384             SaveFillerWords->push_back(*vecSaveFillerWords_It);
00385           }
00386         }else{
00387           edm::LogWarning("FedFillerWords") <<"========= Filler Words Vector is empty! ==========" <<std::endl;
00388         }
00389       }
00390       edm::LogInfo("FedFillerWords") << "Found " << value << " filler words in FED " << fedId << std::endl;
00391       for (vecFillerWordsEventNumber1_It = vecFillerWordsEventNumber1.begin(); vecFillerWordsEventNumber1_It != vecFillerWordsEventNumber1.end(); vecFillerWordsEventNumber1_It++){
00392         FillerWordEventNumbers1->push_back(*vecFillerWordsEventNumber1_It);
00393       }
00394       for(vecFillerWordsEventNumber2_It = vecFillerWordsEventNumber2.begin(); vecFillerWordsEventNumber2_It != vecFillerWordsEventNumber2.end(); vecFillerWordsEventNumber2_It++){
00395         FillerWordEventNumbers2->push_back(*vecFillerWordsEventNumber2_It);
00396       }
00397     }
00398   }
00399   iEvent.put(FillerWordEventNumbers1 , "FillerWordEventNumber1");
00400   iEvent.put(FillerWordEventNumbers2 , "FillerWordEventNumber2");
00401   //====== bool variable to be controled in the config file, allows the user to put or
00402   //       the filler words inside the output root file
00403   if(SaveFillerWordsbool == true)
00404     {
00405       iEvent.put(SaveFillerWords, "SaveFillerWord");
00406     }
00407   vecSaveFillerWords.erase(vecSaveFillerWords.begin(), vecSaveFillerWords.end());    
00408   vecFillerWordsEventNumber1.erase(vecFillerWordsEventNumber1.begin(), vecFillerWordsEventNumber1.end());
00409 }
00410 
00411 // ------------ method called once each job just before starting event loop  ------------
00412 void 
00413 SiPixelFedFillerWordEventNumber ::beginJob()
00414 {
00415 }
00416 
00417 // ------------ method called once each job just after ending the event loop  ------------
00418 void 
00419 SiPixelFedFillerWordEventNumber ::endJob() {
00420 }
00421 //===== define this as a plug-in
00422 DEFINE_FWK_MODULE(SiPixelFedFillerWordEventNumber );