00001
00005 #include <iostream>
00006 #include <fstream>
00007 #include <vector>
00008 #include "string"
00009
00010 #include <FWCore/Framework/interface/Frameworkfwd.h>
00011 #include <FWCore/Framework/interface/MakerMacros.h>
00012 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00013 #include "FWCore/Framework/interface/EDAnalyzer.h"
00014 #include "FWCore/Framework/interface/Event.h"
00015 #include "DataFormats/Common/interface/Handle.h"
00016 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00017 #include "FWCore/Framework/interface/EventSetup.h"
00018 #include "FWCore/Framework/interface/ESHandle.h"
00019 #include "DataFormats/CSCDigi/interface/CSCStripDigi.h"
00020 #include "DataFormats/CSCDigi/interface/CSCStripDigiCollection.h"
00021 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
00022 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
00023 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00024 #include "IORawData/CSCCommissioning/src/FileReaderDDU.h"
00025 #include "EventFilter/CSCRawToDigi/interface/CSCDDUEventData.h"
00026 #include "EventFilter/CSCRawToDigi/interface/CSCDCCEventData.h"
00027 #include "EventFilter/CSCRawToDigi/interface/CSCEventData.h"
00028 #include "EventFilter/CSCRawToDigi/interface/CSCDMBHeader.h"
00029 #include "OnlineDB/CSCCondDB/interface/CSCSaturationAnalyzer.h"
00030 #include "OnlineDB/CSCCondDB/interface/SaturationFit.h"
00032
00034 #include "OnlineDB/CSCCondDB/interface/CSCMap1.h"
00035
00036 CSCSaturationAnalyzer::CSCSaturationAnalyzer(edm::ParameterSet const& conf) {
00037 debug = conf.getUntrackedParameter<bool>("debug",false);
00038 eventNumber=0,evt=0,counterzero=0,Nddu=0,myIndex=0,myNcham=-999;
00039 strip=0,misMatch=0,NChambers=0;
00040 i_chamber=0,i_layer=0,reportedChambers=0;
00041 length=1;
00042 aVar=0.0,bVar=0.0;
00043
00044 adc_vs_charge = TH2F("Saturation" ,"ADC_vs_charge", 100,0,700,100,0,4000);
00045 adc00_vs_charge = TH2F("Saturation01","ADC_vs_charge", 100,0,700,100,0,4000);
00046 adc01_vs_charge = TH2F("Saturation02","ADC_vs_charge", 100,0,700,100,0,4000);
00047 adc02_vs_charge = TH2F("Saturation03","ADC_vs_charge", 100,0,700,100,0,4000);
00048 adc03_vs_charge = TH2F("Saturation04","ADC_vs_charge", 100,0,700,100,0,4000);
00049 adc04_vs_charge = TH2F("Saturation05","ADC_vs_charge", 100,0,700,100,0,4000);
00050 adc05_vs_charge = TH2F("Saturation06","ADC_vs_charge", 100,0,700,100,0,4000);
00051 adc06_vs_charge = TH2F("Saturation07","ADC_vs_charge", 100,0,700,100,0,4000);
00052 adc07_vs_charge = TH2F("Saturation08","ADC_vs_charge", 100,0,700,100,0,4000);
00053 adc08_vs_charge = TH2F("Saturation09","ADC_vs_charge", 100,0,700,100,0,4000);
00054 adc09_vs_charge = TH2F("Saturation10","ADC_vs_charge", 100,0,700,100,0,4000);
00055 adc10_vs_charge = TH2F("Saturation11","ADC_vs_charge", 100,0,700,100,0,4000);
00056 adc11_vs_charge = TH2F("Saturation12","ADC_vs_charge", 100,0,700,100,0,4000);
00057 adc12_vs_charge = TH2F("Saturation13","ADC_vs_charge", 100,0,700,100,0,4000);
00058
00059 for (int i=0; i<NUMMODTEN_sat; i++){
00060 for (int j=0; j<CHAMBERS_sat; j++){
00061 for (int k=0; k<LAYERS_sat; k++){
00062 for (int l=0; l<STRIPS_sat; l++){
00063 maxmodten[i][j][k][l] = 0.0;
00064 }
00065 }
00066 }
00067 }
00068
00069
00070 for (int i=0; i<CHAMBERS_sat; i++){
00071 size[i] = 0;
00072 }
00073
00074 for (int iii=0;iii<DDU_sat;iii++){
00075 for (int i=0; i<CHAMBERS_sat; i++){
00076 for (int j=0; j<LAYERS_sat; j++){
00077 for (int k=0; k<STRIPS_sat; k++){
00078 adcMax[iii][i][j][k] = -999.0;
00079 adcMean_max[iii][i][j][k] = -999.0;
00080 }
00081 }
00082 }
00083 }
00084 }
00085
00086 void CSCSaturationAnalyzer::analyze(edm::Event const& e, edm::EventSetup const& iSetup) {
00087
00088 edm::Handle<CSCStripDigiCollection> strips;
00089 e.getByLabel("cscunpacker","MuonCSCStripDigi",strips);
00090
00091 edm::Handle<FEDRawDataCollection> rawdata;
00092 e.getByType(rawdata);
00093
00094
00095
00096 for (int id=FEDNumbering::getCSCFEDIds().first;
00097 id<=FEDNumbering::getCSCFEDIds().second; ++id){
00098
00100 const FEDRawData& fedData = rawdata->FEDData(id);
00101 if (fedData.size()){
00102
00104 CSCDCCEventData dccData((short unsigned int *) fedData.data());
00105
00106 const std::vector<CSCDDUEventData> & dduData = dccData.dduData();
00107
00108 for (unsigned int iDDU=0; iDDU<dduData.size(); ++iDDU) {
00109
00111 const std::vector<CSCEventData> & cscData = dduData[iDDU].cscData();
00112
00113
00114
00115
00116 Nddu = dduData.size();
00117 reportedChambers += dduData[iDDU].header().ncsc();
00118 NChambers = cscData.size();
00119 int repChambers = dduData[iDDU].header().ncsc();
00120 std::cout << " Reported Chambers = " << repChambers <<" "<<NChambers<< std::endl;
00121 if (NChambers!=repChambers) { std::cout<< "misMatched size!!!" << std::endl; misMatch++;}
00122 if(NChambers > myNcham){
00123 myNcham=NChambers;
00124 }
00125
00126 if (NChambers !=0){
00127 evt++;
00128 }
00129
00130
00131 for (int i_chamber=0; i_chamber<NChambers; i_chamber++) {
00132
00133 for (int i_layer = 1; i_layer <=6; ++i_layer) {
00134
00135 std::vector<CSCStripDigi> digis = cscData[i_chamber].stripDigis(i_layer) ;
00136 const CSCDMBHeader * thisDMBheader = cscData[i_chamber].dmbHeader();
00137
00138 if (cscData[i_chamber].dmbHeader() && thisDMBheader->cfebAvailable()){
00139 dmbID[i_chamber] = cscData[i_chamber].dmbHeader()->dmbID();
00140 crateID[i_chamber] = cscData[i_chamber].dmbHeader()->crateID();
00141 if(crateID[i_chamber] == 255) continue;
00142
00143 for (unsigned int i=0; i<digis.size(); i++){
00144 size[i_chamber] = digis.size();
00145 std::vector<int> adc = digis[i].getADCCounts();
00146 strip = digis[i].getStrip();
00147 adcMax[iDDU][i_chamber][i_layer-1][strip-1]=-99.0;
00148 for(unsigned int k=0; k<adc.size(); k++){
00149 float ped=(adc[0]+adc[1])/2.;
00150 if(adc[k]-ped > adcMax[iDDU][i_chamber][i_layer-1][strip-1]) {
00151 adcMax[iDDU][i_chamber][i_layer-1][strip-1]=adc[k]-ped;
00152 }
00153 }
00154 adcMean_max[iDDU][i_chamber][i_layer-1][strip-1] += adcMax[iDDU][i_chamber][i_layer-1][strip-1]/PULSES_sat;
00155
00156
00157 if (evt%PULSES_sat == 0 && (strip-1)%16 == (evt-1)/NUMMODTEN_sat){
00158
00159
00160
00161
00162 int pulsesNr = int((evt-1)/PULSES_sat)%NUMBERPLOTTED_sat ;
00163 maxmodten[pulsesNr][i_chamber][i_layer-1][strip-1] = adcMean_max[iDDU][i_chamber][i_layer-1][strip-1];
00164 }
00165 }
00166 }
00167 }
00168 }
00169
00170 if((evt-1)%PULSES_sat==0){
00171 for(int iii=0;iii<DDU_sat;iii++){
00172 for(int ii=0; ii<CHAMBERS_sat; ii++){
00173 for(int jj=0; jj<LAYERS_sat; jj++){
00174 for(int kk=0; kk<STRIPS_sat; kk++){
00175 adcMean_max[iii][ii][jj][kk]=0.0;
00176 }
00177 }
00178 }
00179 }
00180 }
00181
00182 eventNumber++;
00183 edm::LogInfo ("CSCSaturationAnalyzer") << "end of event number " << eventNumber<<" and non-zero event "<<evt ;
00184 }
00185 }
00186 }
00187 }
00188
00189 CSCSaturationAnalyzer::~CSCSaturationAnalyzer(){
00190
00191
00192
00193 filein.open("../test/CSCsaturation.cfg");
00194 filein.ignore(1000,'\n');
00195
00196 while(filein != NULL){
00197 lines++;
00198 getline(filein,PSet);
00199
00200 if (lines==2){
00201 name=PSet;
00202 std::cout<<name<<std::endl;
00203 }
00204 }
00205 std::string::size_type runNameStart = name.find("\"",0);
00206 std::string::size_type runNameEnd = name.find("raw",0);
00207 std::string::size_type rootStart = name.find("Gains",0);
00208 int nameSize = runNameEnd+2-runNameStart;
00209 int myRootSize = rootStart-runNameStart+8;
00210 std::string myname= name.substr(runNameStart+1,nameSize);
00211 std::string myRootName= name.substr(runNameStart+1,myRootSize);
00212 std::string myRootEnd = "Sat.root";
00213 std::string myASCIIFileEnd = "Sat.dat";
00214 std::string runFile= myRootName;
00215 std::string myRootFileName = runFile+myRootEnd;
00216 std::string myASCIIFileName= runFile+myASCIIFileEnd;
00217 const char *myNewName=myRootFileName.c_str();
00218 const char *myFileName=myASCIIFileName.c_str();
00219
00220 struct tm* clock;
00221 struct stat attrib;
00222 stat(myname.c_str(), &attrib);
00223 clock = localtime(&(attrib.st_mtime));
00224 std::string myTime=asctime(clock);
00225 std::ofstream mySatFile(myFileName,std::ios::out);
00226
00228
00230 CSCMapItem::MapItem mapitem;
00231 cscmap1 *map = new cscmap1();
00232 CSCobject *cn = new CSCobject();
00233
00234
00235
00236 TCalibSaturationEvt calib_evt;
00237 TFile calibfile(myNewName, "RECREATE");
00238 TTree calibtree("Calibration","Saturation");
00239 calibtree.Branch("EVENT", &calib_evt, "strip/I:layer/I:cham/I:id/I:N/F:a/F:b/F:c/F");
00240
00241 for (int dduiter=0;dduiter<Nddu;dduiter++){
00242 for(int chamberiter=0; chamberiter<myNcham; chamberiter++){
00243 for (int cham=0;cham<myNcham;cham++){
00244 if (cham !=chamberiter) continue;
00245
00246
00247 int new_crateID = crateID[cham];
00248 int new_dmbID = dmbID[cham];
00249 int counter=0;
00250 std::cout<<" Crate: "<<new_crateID<<" and DMB: "<<new_dmbID<<std::endl;
00252
00254 map->cratedmb(new_crateID,new_dmbID,&mapitem);
00255 chamber_num=mapitem.chamberId;
00256 sector= mapitem.sector;
00257 first_strip_index=mapitem.stripIndex;
00258 strips_per_layer=mapitem.strips;
00259 chamber_index=mapitem.chamberId;
00260 chamber_type = mapitem.chamberLabel;
00261
00262 std::cout<<"Data is for chamber:: "<<chamber_type<<" "<<chamber_id<<" in sector: "<<sector<<" index "<<first_strip_index<<std::endl;
00263
00264 calib_evt.id=chamber_num;
00265
00266 for (int layeriter=0; layeriter<LAYERS_sat; layeriter++){
00267 for (int stripiter=0; stripiter<STRIPS_sat; stripiter++){
00268
00269 for (int j=0; j<LAYERS_sat; j++){
00270 if (j != layeriter) continue;
00271
00272 int layer_id=chamber_num+j+1;
00273 if(sector==-100)continue;
00274 cn->obj[layer_id].resize(size[cham]);
00275
00276 for (int k=0; k<size[cham]; k++){
00277 if (k != stripiter) continue;
00278
00279 for (int st=0;st<NUMBERPLOTTED_sat;st++){
00280 myCharge[st]=0.0;
00281 mySatADC[st]=0.0;
00282 myCharge_for_plots[st]=0.0;
00283 mySatADC_for_plots[st]=0.0;
00284 }
00285
00286 for(int ii=0; ii<NUMBERPLOTTED_sat; ii++){
00287 myCharge[ii] = 11.2 +(28.0*ii);
00288 mySatADC[ii] = maxmodten[ii][cham][j][k];
00289 myCharge_for_plots[ii] = 11.2 +(28.0*ii);
00290 mySatADC_for_plots[ii] = maxmodten[ii][cham][j][k];
00291
00292
00293 adc_vs_charge.Fill(myCharge_for_plots[ii],maxmodten[ii][cham][j][k]);
00294
00295 if(cham==0) adc00_vs_charge.Fill(myCharge_for_plots[ii] ,maxmodten[ii][cham][j][k]);
00296 if(cham==1) adc01_vs_charge.Fill(myCharge_for_plots[ii] ,maxmodten[ii][cham][j][k]);
00297 if(cham==2) adc02_vs_charge.Fill(myCharge_for_plots[ii] ,maxmodten[ii][cham][j][k]);
00298 if(cham==3) adc03_vs_charge.Fill(myCharge_for_plots[ii] ,maxmodten[ii][cham][j][k]);
00299 if(cham==4) adc04_vs_charge.Fill(myCharge_for_plots[ii] ,maxmodten[ii][cham][j][k]);
00300 if(cham==5) adc05_vs_charge.Fill(myCharge_for_plots[ii] ,maxmodten[ii][cham][j][k]);
00301 if(cham==6) adc06_vs_charge.Fill(myCharge_for_plots[ii] ,maxmodten[ii][cham][j][k]);
00302 if(cham==7) adc07_vs_charge.Fill(myCharge_for_plots[ii] ,maxmodten[ii][cham][j][k]);
00303 if(cham==8) adc08_vs_charge.Fill(myCharge_for_plots[ii] ,maxmodten[ii][cham][j][k]);
00304 if(cham==9) adc09_vs_charge.Fill(myCharge_for_plots[ii] ,maxmodten[ii][cham][j][k]);
00305 if(cham==10) adc10_vs_charge.Fill(myCharge_for_plots[ii] ,maxmodten[ii][cham][j][k]);
00306 if(cham==11) adc11_vs_charge.Fill(myCharge_for_plots[ii] ,maxmodten[ii][cham][j][k]);
00307 if(cham==12) adc12_vs_charge.Fill(myCharge_for_plots[ii] ,maxmodten[ii][cham][j][k]);
00308
00309 }
00310
00311
00312 float u0_ptr=0.0, u1_ptr=0.0, u2_ptr=0.0,u3_ptr=0.0;
00313 SaturationFit s(NUMBERPLOTTED_sat,myCharge,mySatADC,&u0_ptr, &u1_ptr, &u2_ptr, &u3_ptr);
00314
00315
00316 counter++;
00317 myIndex = first_strip_index+counter-1;
00318 if (counter>size[cham]*LAYERS_sat) counter=0;
00319 mySatFile <<" "<<myIndex-1<<" "<<u0_ptr<<" "<<u1_ptr<<" "<<u2_ptr<<" "<<u3_ptr<<std::endl;
00320
00321 calib_evt.strip = k;
00322 calib_evt.layer = j;
00323 calib_evt.cham = cham;
00324 calib_evt.N = u0_ptr;
00325 calib_evt.a = u1_ptr;
00326 calib_evt.b = u2_ptr;
00327 calib_evt.c = u3_ptr;
00328
00329 calibtree.Fill();
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339 }
00340 }
00341 }
00342 }
00343 }
00344 }
00345 }
00346
00347
00348
00349
00350
00351
00352
00353 adc_vs_charge.Write();
00354 adc00_vs_charge.Write();
00355 adc01_vs_charge.Write();
00356 adc02_vs_charge.Write();
00357 adc03_vs_charge.Write();
00358 adc04_vs_charge.Write();
00359 adc05_vs_charge.Write();
00360 adc06_vs_charge.Write();
00361 adc07_vs_charge.Write();
00362 adc08_vs_charge.Write();
00363 adc09_vs_charge.Write();
00364 adc10_vs_charge.Write();
00365 adc11_vs_charge.Write();
00366 adc12_vs_charge.Write();
00367
00368 calibfile.Write();
00369 calibfile.Close();
00370 }