CMS 3D CMS Logo

CSCCFEBConnectivityAnalyzer.cc

Go to the documentation of this file.
00001 
00007 #include <iostream>
00008 #include <fstream>
00009 #include <vector>
00010 #include <string>
00011 #include <math.h>
00012 
00013 #include <FWCore/Framework/interface/Frameworkfwd.h>
00014 #include <FWCore/Framework/interface/MakerMacros.h>
00015 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00016 #include "FWCore/Framework/interface/EDAnalyzer.h"
00017 #include "FWCore/Framework/interface/Event.h"
00018 #include "DataFormats/Common/interface/Handle.h"
00019 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00020 #include "FWCore/Framework/interface/EventSetup.h"
00021 #include "FWCore/Framework/interface/ESHandle.h"
00022 #include "DataFormats/CSCDigi/interface/CSCStripDigi.h"
00023 #include "DataFormats/CSCDigi/interface/CSCStripDigiCollection.h"
00024 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
00025 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
00026 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00027 #include "IORawData/CSCCommissioning/src/FileReaderDDU.h"
00028 #include "EventFilter/CSCRawToDigi/interface/CSCDDUEventData.h"
00029 #include "EventFilter/CSCRawToDigi/interface/CSCDCCEventData.h"
00030 #include "EventFilter/CSCRawToDigi/interface/CSCEventData.h"
00031 #include "EventFilter/CSCRawToDigi/interface/CSCDMBHeader.h"
00032 #include "OnlineDB/CSCCondDB/interface/CSCCFEBConnectivityAnalyzer.h"
00033 
00034 CSCCFEBConnectivityAnalyzer::CSCCFEBConnectivityAnalyzer(edm::ParameterSet const& conf) {
00035 
00036   debug = conf.getUntrackedParameter<bool>("debug",false);
00037   eventNumber = 0,strip=0,flagConnect=-9;
00038   evt = 0,Nddu=0,misMatch=0,myIndex=0,myNcham=-999;
00039   chamber=0,layer=0,reportedChambers =0;
00040   length = 1, NChambers=0;
00041    
00042   //initialize arrays
00043   for (int ii=0;ii<DDU_con;ii++){
00044     for (int i=0; i<CHAMBERS_con; i++){
00045       for (int j=0; j<LAYERS_con; j++){
00046         for (int k=0; k<STRIPS_con;k++){
00047           adcMin[ii][i][j][k]    = 9999999.0;
00048           adcMax[ii][i][j][k]    = -9999999.0;
00049           adcMean_max[ii][i][j][k]=0.0;
00050           adcMean_min[ii][i][j][k]=0.0;
00051           diff[ii][i][j][k]      = 0.0;
00052         }
00053       }
00054     }
00055   }
00056 }
00057 
00058 void CSCCFEBConnectivityAnalyzer::analyze(edm::Event const& e, edm::EventSetup const& iSetup) {
00059   
00060   // These declarations create handles to the types of records that you want
00061   // to retrieve from event "e".
00062   //
00063   edm::Handle<CSCStripDigiCollection> strips;
00064   
00065   e.getByLabel("cscunpacker","MuonCSCStripDigi",strips);
00066   
00067   edm::Handle<FEDRawDataCollection> rawdata;
00068   e.getByType(rawdata);
00069   //event =e.id().event();
00070   for (int id=FEDNumbering::getCSCFEDIds().first;
00071        id<=FEDNumbering::getCSCFEDIds().second; ++id){ //for each of our DCCs
00072     
00074     const FEDRawData& fedData = rawdata->FEDData(id);
00075     
00076     if (fedData.size()){ 
00077       
00079       CSCDCCEventData dccData((short unsigned int *) fedData.data()); 
00080       
00081       const std::vector<CSCDDUEventData> & dduData = dccData.dduData(); 
00082       //evt++;
00083       
00084       for (unsigned int iDDU=0; iDDU<dduData.size(); ++iDDU) {  
00085 
00086         const std::vector<CSCEventData> & cscData = dduData[iDDU].cscData();
00087         Nddu=dduData.size();
00088         reportedChambers += dduData[iDDU].header().ncsc();
00089         NChambers = cscData.size();
00090         int repChambers = dduData[iDDU].header().ncsc();
00091         std::cout << " Reported Chambers = " << repChambers <<"   "<<NChambers<< std::endl;
00092         if (NChambers!=repChambers) {std::cout<< "misMatched size!!!" << std::endl; misMatch++;continue;}
00093         if(NChambers > myNcham){
00094           myNcham=NChambers;
00095         }
00096         
00097         if (NChambers !=0){
00098           evt++;  
00099         }
00100 
00101         for (chamber=0; chamber<NChambers; chamber++) {//loop over all DMBs  
00102           
00103           for(layer = 1; layer <= 6; ++layer) {//loop over all layers in chambers
00104                     
00105             std::vector<CSCStripDigi> digis = cscData[chamber].stripDigis(layer) ;
00106             const CSCDMBHeader * thisDMBheader = cscData[chamber].dmbHeader();
00107              
00108             if (cscData[chamber].dmbHeader() && thisDMBheader->cfebAvailable()){//check that CFEB data exists
00109             
00110               dmbID[chamber]   = cscData[chamber].dmbHeader()->dmbID(); //get DMB ID
00111               crateID[chamber] = cscData[chamber].dmbHeader()->crateID(); //get crate ID
00112               if(crateID[chamber] == 255) continue; //255 doesn't exist
00113               
00114               for (unsigned int i=0; i<digis.size(); i++){
00115                 size[chamber] = digis.size();
00116                 strip = digis[i].getStrip();
00117                 std::vector<int> adc = digis[i].getADCCounts();
00118                 
00119                 for(unsigned int k=0; k<adc.size(); k++){
00120                   
00121                   if(adc[k] > adcMax[iDDU][chamber][layer-1][strip-1]) {
00122                     adcMax[iDDU][chamber][layer-1][strip-1]= adc[k];
00123                   }
00124                   
00125                   if(adc[k] < adcMin[iDDU][chamber][layer-1][strip-1]){
00126                     adcMin[iDDU][chamber][layer-1][strip-1]= adc[k];
00127                   }
00128                   
00129                 }//end timebins loop
00130 
00131                 adcMean_max[iDDU][chamber][layer-1][strip-1] += adcMax[iDDU][chamber][layer-1][strip-1]/25.;
00132                 adcMean_min[iDDU][chamber][layer-1][strip-1] += adcMin[iDDU][chamber][layer-1][strip-1]/25.;
00133               
00134               }//end digis size
00135             }//end if cfeb.available 
00136           }//end loop over layers
00137         }//end loop over chambers
00138         
00139         if((evt-1)%25==0){
00140           for(int iii=0;iii<DDU_con;iii++){
00141             for(int ii=0; ii<CHAMBERS_con; ii++){
00142               for(int jj=0; jj<LAYERS_con; jj++){
00143                 for(int kk=0; kk<STRIPS_con; kk++){
00144                   adcMean_max[iii][ii][jj][kk]=0.0;
00145                   adcMean_min[iii][ii][jj][kk]=0.0;
00146                 }
00147               }
00148             }
00149           }
00150         }
00151         
00152         eventNumber++;
00153         edm::LogInfo ("CSCCFEBConnectivityAnalyzer")  << "end of event number " << eventNumber;
00154         
00155       }
00156     }
00157   }
00158 }
00159 
00160 CSCCFEBConnectivityAnalyzer::~CSCCFEBConnectivityAnalyzer(){
00161 
00162   //get time of Run file for DB transfer
00163   filein.open("../test/CSCCFEBconnect.cfg");
00164   filein.ignore(1000,'\n');
00165   
00166   while(filein != NULL){
00167     lines++;
00168     getline(filein,PSet);
00169     
00170     if (lines==2){
00171       name=PSet;  
00172     }
00173   }
00174   
00175   //get name of run file from .cfg and name root output after that
00176   std::string::size_type runNameStart = name.find("\"",0);
00177   std::string::size_type runNameEnd   = name.find("raw",0);
00178   std::string::size_type rootStart    = name.find("Calib",0);
00179   int nameSize = runNameEnd+2-runNameStart;
00180   int myRootSize = rootStart-runNameStart+8;
00181   std::string myname= name.substr(runNameStart+1,nameSize);
00182   std::string myRootName= name.substr(runNameStart+1,myRootSize+1);
00183   std::string myRootEnd = "Connectivity.root";
00184   std::string myASCIIFileEnd = "Connectivity.dat";
00185   std::string runFile= myRootName;
00186   std::string myRootFileName = runFile+myRootEnd;
00187   std::string myASCIIFileName= runFile+myASCIIFileEnd;
00188   const char *myNewName=myRootFileName.c_str();
00189   const char *myFileName=myASCIIFileName.c_str();
00190 
00191   struct tm* clock;                         
00192   struct stat attrib;                       
00193   stat(myname.c_str(), &attrib);          
00194   clock = localtime(&(attrib.st_mtime));  
00195   std::string myTime=asctime(clock);
00196   std::ofstream myConnectFile(myFileName,std::ios::out);
00197 
00198   //old DB map
00199   //cscmap *map = new cscmap();
00200   //condbon *dbon = new condbon();
00201   CSCMapItem::MapItem mapitem;
00202   cscmap1 *map = new cscmap1(); 
00203   CSCobject *cn = new CSCobject();
00204   
00205   //root ntuple
00206   TCalibCFEBConnectEvt calib_evt;
00207   TFile calibfile(myNewName, "RECREATE");
00208   TTree calibtree("Calibration","Connectivity");
00209   calibtree.Branch("EVENT", &calib_evt, "strip/I:layer/I:cham/I:ddu/I:adcMax/F:adcMin/F:diff/F:RMS/F:id/I:flagConnect/I");
00210   
00211   for (int iii=0; iii<Nddu; iii++){
00212     for (int i=0; i<myNcham; i++){
00213       theRMS      =0.0;
00214       my_diffSquare=0.0;
00215       
00216       //get chamber ID from DB mapping        
00217       int new_crateID = crateID[i];
00218       int new_dmbID   = dmbID[i];
00219       int counter=0;
00220       std::cout<<" Crate: "<<new_crateID<<" and DMB:  "<<new_dmbID<<std::endl;
00221       //old map call
00222       // map->crate_chamber(new_crateID,new_dmbID,&chamber_id,&chamber_num,&sector,&first_strip_index,&strips_per_layer,&chamber_index);
00224         map->cratedmb(new_crateID,new_dmbID,&mapitem);
00225         chamber_num=mapitem.chamberId;
00226         sector= mapitem.sector;
00227         first_strip_index=mapitem.stripIndex;
00228         strips_per_layer=mapitem.strips;
00229         chamber_index=mapitem.chamberId;
00230         chamber_type = mapitem.chamberLabel;
00231 
00232         std::cout<<"Data is for chamber:: "<< chamber_type<<"  "<<chamber_id<<"  "<<mapitem.chamberId<<" in sector:  "<<" index "<<first_strip_index<<sector<<std::endl;
00233       
00234       calib_evt.id =chamber_num ;
00235 
00236       for (int j=0; j<LAYERS_con; j++){
00237         int layer_id=chamber_num+j+1;
00238         if(sector==-100)continue;
00239         cn->obj[layer_id].resize(size[i]);
00240         for (int k=0; k<size[i]; k++){
00241           
00242           my_diff =  adcMean_max[iii][i][j][k]- adcMean_min[iii][i][j][k];
00243           my_diffSquare = my_diff*my_diff;
00244           //std::cout<<"Chamber "<<i<<" Layer "<<j<<" Strip "<<k<<" diff "<<my_diff<<" RMS "<<theRMS<<std::endl;
00245           
00246           counter++; 
00247           myIndex = first_strip_index+counter-1;
00248           if (counter>size[i]*LAYERS_con) counter=0;
00249           myConnectFile <<"  "<<myIndex-1<<"  "<<my_diff<<"  "<<theRMS<<std::endl;
00250           //flags for RMS and baseline
00251           if (my_diff > 100.0)      flagConnect = 1; // ok
00252           if (my_diff > 0.0 && my_diff<100.0) flagConnect = 2; //intermidiate state...
00253           if (my_diff == 0.0 )      flagConnect = 3; //strip dead
00254           if (my_diff > 1500.0)     flagConnect = 4; //strip noisy
00255           
00256           theRMS       = sqrt(fabs(my_diffSquare - my_diff*my_diff));
00257           calib_evt.strip=k;
00258           calib_evt.layer=j;
00259           calib_evt.cham=i;
00260           calib_evt.ddu=iii;
00261           calib_evt.adcMin = adcMean_min[iii][i][j][k];
00262           calib_evt.adcMax = adcMean_max[iii][i][j][k];
00263           calib_evt.diff=my_diff;
00264           calib_evt.RMS=theRMS;
00265           calib_evt.flagConnect = flagConnect;
00266           calibtree.Fill();
00267           
00268           //send constants to DB
00269           /*
00270             cn->obj[layer_id][k].resize(2);
00271             cn->obj[layer_id][k][0] = my_diff;
00272             cn->obj[layer_id][k][1] = theRMS;
00273           */
00274         }
00275       }
00276     }
00277   }
00278   calibfile.Write();    
00279   calibfile.Close();  
00280 }

Generated on Tue Jun 9 17:40:41 2009 for CMSSW by  doxygen 1.5.4