Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:31:44

0001 #ifndef _CSCCCHAMBERTIMECORRECTIONSVALUES_H
0002 #define _CSCCCHAMBERTIMECORRECTIONSVALUES_H
0003 
0004 #include <memory>
0005 #include "FWCore/Framework/interface/SourceFactory.h"
0006 #include "FWCore/Framework/interface/Frameworkfwd.h"
0007 #include "FWCore/Framework/interface/ESProducer.h"
0008 #include "FWCore/Framework/interface/Event.h"
0009 #include "FWCore/Framework/interface/MakerMacros.h"
0010 #include "FWCore/Framework/interface/EventSetupRecordIntervalFinder.h"
0011 #include "FWCore/Framework/interface/ESHandle.h"
0012 #include "FWCore/Framework/interface/EventSetup.h"
0013 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0014 
0015 #include <DataFormats/MuonDetId/interface/CSCDetId.h>
0016 #include "CondFormats/CSCObjects/interface/CSCChamberTimeCorrections.h"
0017 #include "CondFormats/DataRecord/interface/CSCChamberTimeCorrectionsRcd.h"
0018 #include "OnlineDB/CSCCondDB/interface/CSCChamberTimeCorrectionsValues.h"
0019 #include "OnlineDB/CSCCondDB/interface/CSCCableRead.h"
0020 
0021 #include "DataFormats/MuonDetId/interface/CSCIndexer.h"
0022 
0023 class CSCChamberTimeCorrectionsValues : public edm::ESProducer, public edm::EventSetupRecordIntervalFinder {
0024 public:
0025   CSCChamberTimeCorrectionsValues(const edm::ParameterSet &);
0026   ~CSCChamberTimeCorrectionsValues() override;
0027 
0028   typedef std::unique_ptr<CSCChamberTimeCorrections> ReturnType;
0029 
0030   inline static CSCChamberTimeCorrections *prefill(bool isMC, float ME11offset, float nonME11offset);
0031 
0032   ReturnType produceChamberTimeCorrections(const CSCChamberTimeCorrectionsRcd &);
0033 
0034 private:
0035   // ----------member data ---------------------------
0036   void setIntervalFor(const edm::eventsetup::EventSetupRecordKey &,
0037                       const edm::IOVSyncValue &,
0038                       edm::ValidityInterval &) override;
0039 
0040   //Flag for determining if this is for setting MC or data corrections
0041   bool isForMC;
0042   float ME11offsetMC;
0043   float ME11offsetData;
0044   float nonME11offsetMC;
0045   float nonME11offsetData;
0046 };
0047 
0048 #include <fstream>
0049 #include <vector>
0050 #include <iostream>
0051 
0052 // to workaround plugin library
0053 inline CSCChamberTimeCorrections *CSCChamberTimeCorrectionsValues::prefill(bool isMC,
0054                                                                            float ME11offset,
0055                                                                            float nonME11offset) {
0056   if (isMC)
0057     printf("\n Generating fake DB constants for MC\n");
0058   else
0059     printf("\n Getting chamber corrections from the cable data base and possibly other files \n");
0060 
0061   const int FACTOR = 100;
0062   const int MAX_SIZE = 540;
0063   //const int MAX_SHORT= 32767;
0064 
0065   CSCChamberTimeCorrections *chamberObj = new CSCChamberTimeCorrections();
0066 
0067   int i;  //i - chamber index.
0068   int count = 0;
0069   std::string chamber_label, cfeb_rev, alct_rev;
0070   float cfeb_length = 0, alct_length = 0, cfeb_tmb_skew_delay = 0, cfeb_timing_corr = 0;
0071 
0072   // Only the first 481 chambers have interesting cable lengths at present
0073   // The rest of the chambers will be filled with zeros
0074   chamberObj->factor_precision = FACTOR;
0075 
0076   chamberObj->chamberCorrections.resize(MAX_SIZE);
0077   // fill the database with dummy values
0078   for (i = 1; i <= MAX_SIZE; ++i) {
0079     chamberObj->chamberCorrections[i - 1].cfeb_length = 0;
0080     chamberObj->chamberCorrections[i - 1].cfeb_rev = 'X';
0081     chamberObj->chamberCorrections[i - 1].alct_length = 0;
0082     chamberObj->chamberCorrections[i - 1].alct_rev = 'X';
0083     chamberObj->chamberCorrections[i - 1].cfeb_tmb_skew_delay = 0;
0084     chamberObj->chamberCorrections[i - 1].cfeb_timing_corr = 0;
0085     chamberObj->chamberCorrections[i - 1].cfeb_cable_delay = 0;
0086     chamberObj->chamberCorrections[i - 1].anode_bx_offset = 0;
0087   }
0088 
0089   // for MC there will is a different correction for each chamber type
0090   if (isMC) {
0091     float OffsetByType;
0092     float anodeOffset;
0093     for (i = 1; i <= MAX_SIZE; ++i) {
0094       if (i <= 36 || (i >= 235 && i <= 270)) {
0095         OffsetByType = 172.;
0096         anodeOffset = 6.18;
0097       }  // 1/1
0098       else if (i <= 72 || (i >= 271 && i <= 306)) {
0099         OffsetByType = 168.;
0100         anodeOffset = 6.22;
0101       }  // 1/2
0102       else if (i <= 108 || (i >= 307 && i <= 342)) {
0103         OffsetByType = 177.;
0104         anodeOffset = 6.19;
0105       }  // 1/3
0106       else if (i <= 126 || (i >= 343 && i <= 360)) {
0107         OffsetByType = 171.;
0108         anodeOffset = 6.25;
0109       }  // 2/1
0110       else if (i <= 162 || (i >= 361 && i <= 396)) {
0111         OffsetByType = 175.;
0112         anodeOffset = 6.21;
0113       }  // 2/2
0114       else if (i <= 180 || (i >= 397 && i <= 414)) {
0115         OffsetByType = 171.;
0116         anodeOffset = 6.25;
0117       }  // 3/1
0118       else if (i <= 216 || (i >= 415 && i <= 450)) {
0119         OffsetByType = 175.;
0120         anodeOffset = 6.20;
0121       }  // 3/2
0122       else if (i <= 234 || (i >= 451 && i <= 468)) {
0123         OffsetByType = 172.;
0124         anodeOffset = 6.19;
0125       }  // 4/1
0126       else {
0127         OffsetByType = 175;
0128         anodeOffset = 6.21;
0129       }  // 4/2
0130 
0131       chamberObj->chamberCorrections[i - 1].cfeb_timing_corr =
0132           (short int)(-1 * OffsetByType * FACTOR + 0.5 * (-1 * OffsetByType >= 0) - 0.5 * (-1 * OffsetByType < 0));
0133       chamberObj->chamberCorrections[i - 1].anode_bx_offset =
0134           (short int)(anodeOffset * FACTOR + 0.5 * (anodeOffset >= 0) - 0.5 * (anodeOffset < 0));
0135     }
0136 
0137     return chamberObj;
0138   }
0139 
0140   // ***************************************************************************
0141   // Everything below this point is for setting the chamber corrections for data
0142   // ***************************************************************************
0143 
0144   csccableread cable;
0145   for (i = 1; i <= MAX_SIZE; ++i) {
0146     // the anode bx offset is 8.15 bx for chambers in 2/1, 3/1, and 4/1
0147     // and 8.18 bx for all other chambers for early runs (8.20 for runs> 149357)
0148     float anodeOffset;
0149     if (i <= 36 || (i >= 235 && i <= 270)) {
0150       anodeOffset = 8.20;
0151     }  // 1/1
0152     else if (i <= 72 || (i >= 271 && i <= 306)) {
0153       anodeOffset = 8.20;
0154     }  // 1/2
0155     else if (i <= 108 || (i >= 307 && i <= 342)) {
0156       anodeOffset = 8.20;
0157     }  // 1/3
0158     else if (i <= 126 || (i >= 343 && i <= 360)) {
0159       anodeOffset = 8.15;
0160     }  // 2/1
0161     else if (i <= 162 || (i >= 361 && i <= 396)) {
0162       anodeOffset = 8.20;
0163     }  // 2/2
0164     else if (i <= 180 || (i >= 397 && i <= 414)) {
0165       anodeOffset = 8.15;
0166     }  // 3/1
0167     else if (i <= 216 || (i >= 415 && i <= 450)) {
0168       anodeOffset = 8.20;
0169     }  // 3/2
0170     else if (i <= 234 || (i >= 451 && i <= 468)) {
0171       anodeOffset = 8.15;
0172     }  // 4/1
0173     else {
0174       anodeOffset = 8.20;
0175     }  // 4/2
0176 
0177     // for data we will read in from Igor's database
0178     cable.cable_read(
0179         i, &chamber_label, &cfeb_length, &cfeb_rev, &alct_length, &alct_rev, &cfeb_tmb_skew_delay, &cfeb_timing_corr);
0180     // If the read of the cable database is useful (if there is information for the chamber there)
0181     // re-enter the information the cable object
0182     if (!chamber_label.empty() && !(cfeb_length == 0)) {
0183       chamberObj->chamberCorrections[i - 1].cfeb_length = (short int)(cfeb_length * FACTOR + 0.5);
0184       chamberObj->chamberCorrections[i - 1].cfeb_rev = cfeb_rev[0];
0185       chamberObj->chamberCorrections[i - 1].alct_length = (short int)(alct_length * FACTOR + 0.5);
0186       chamberObj->chamberCorrections[i - 1].alct_rev = alct_rev[0];
0187       chamberObj->chamberCorrections[i - 1].cfeb_tmb_skew_delay = (short int)(cfeb_tmb_skew_delay * FACTOR + 0.5);
0188       chamberObj->chamberCorrections[i - 1].cfeb_timing_corr = (short int)(cfeb_timing_corr * FACTOR + 0.5);
0189       chamberObj->chamberCorrections[i - 1].cfeb_cable_delay = 0;
0190       chamberObj->chamberCorrections[i - 1].anode_bx_offset = (short int)(anodeOffset * FACTOR + 0.5);
0191     }
0192     count = count + 1;
0193   }
0194 
0195   //Read in the changes you want to make in the extra chamber variable cfeb_timing_corr
0196   FILE *fin =
0197       fopen("/afs/cern.ch/user/d/deisher/public/TimingCorrections2009/ttcrx_delay_effects_23April_2010.txt", "r");
0198   int chamber;
0199   float corr;
0200   while (!feof(fin)) {
0201     //note space at end of format string to convert last \n
0202     int check = fscanf(fin, "%d %f \n", &chamber, &corr);
0203     if (check != 2) {
0204       printf("cfeb timing corr file has an unexpected format \n");
0205       assert(0);
0206     }
0207     //printf("chamber %d corr %f \n",chamber,corr);
0208     chamberObj->chamberCorrections[chamber - 1].cfeb_timing_corr =
0209         (short int)(corr * FACTOR + 0.5 * (corr >= 0) - 0.5 * (corr < 0));
0210   }
0211   fclose(fin);
0212 
0213   // Remove the offsets inherent to ME11 and non ME11 chambers
0214   for (i = 1; i <= MAX_SIZE; ++i) {
0215     float temp = float(chamberObj->chamberCorrections[i - 1].cfeb_timing_corr) / FACTOR;
0216     if (i <= 36 || (i >= 235 && i <= 270))
0217       chamberObj->chamberCorrections[i - 1].cfeb_timing_corr =
0218           (short int)((temp - 1 * ME11offset) * FACTOR + 0.5 * (temp >= ME11offset) - 0.5 * (temp < ME11offset));
0219     else
0220       chamberObj->chamberCorrections[i - 1].cfeb_timing_corr =
0221           (short int)((temp - 1 * nonME11offset) * FACTOR + 0.5 * (temp >= nonME11offset) -
0222                       0.5 * (temp < nonME11offset));
0223   }
0224 
0225   //Read in the cfeb_cable_delay values (0 or 1) and don't use a precision correction factor
0226   FILE *fdelay =
0227       fopen("/afs/cern.ch/user/d/deisher/public/TimingCorrections2009/cfeb_cable_delay_20100423_both.txt", "r");
0228   char label[1024];
0229   int delay;
0230   CSCIndexer indexer;
0231   while (!feof(fdelay)) {
0232     //note space at end of format string to convert last \n
0233     int check = fscanf(fdelay, "%1024s %d \n", label, &delay);
0234     if (check != 2) {
0235       printf("cfeb cable delay file has an unexpected format \n");
0236       assert(0);
0237     }
0238     int chamberSerial = 0;
0239     int c_endcap = (label[2] == '+' ? 1 : 2);
0240     int c_station = atoi(&label[3]);
0241     int c_ring = atoi(&label[5]);
0242     if (c_station == 1 && c_ring == 4)
0243       c_ring = 1;
0244     int c_chamber = (label[7] == '0' ? atoi(&label[8]) : atoi(&label[7]));
0245     chamberSerial = indexer.chamberIndex(c_endcap, c_station, c_ring, c_chamber);
0246     //printf("chamberLabel %s (%d %d %d %d) chamberSerial %d delay %d \n",label,c_endcap,c_station, c_ring, c_chamber, chamberSerial,delay);
0247     chamberObj->chamberCorrections[chamberSerial - 1].cfeb_cable_delay = (short int)delay;
0248   }
0249   fclose(fdelay);
0250 
0251   //Read in a 2nd order correction for chamber offsets derived from data
0252   FILE *foffset = fopen(
0253       "/afs/cern.ch/user/d/deisher/public/TimingCorrections2009/offset_26July2010_codeOverhaul_slope012.txt", "r");
0254   float offset;
0255   int iE, iS, iR, iC;
0256   while (!feof(foffset)) {
0257     //note space at end of format string to convert last \n
0258     int check = fscanf(foffset, "%d %d %d %d %f \n", &iE, &iS, &iR, &iC, &offset);
0259     if (check != 5) {
0260       printf("offset file has an unexpected format \n");
0261       assert(0);
0262     }
0263     int chamberSerial = 0;
0264     if (iS == 1 && iR == 4)
0265       iR = 1;
0266     chamberSerial = indexer.chamberIndex(iE, iS, iR, iC);
0267     //printf("chamberLabel %s (%d %d %d %d) chamberSerial %d delay %d \n",label,c_endcap,c_station, c_ring, c_chamber, chamberSerial,delay);
0268     float temp = float(chamberObj->chamberCorrections[chamberSerial - 1].cfeb_timing_corr) / FACTOR;
0269     chamberObj->chamberCorrections[chamberSerial - 1].cfeb_timing_corr =
0270         (short int)((temp - offset) * FACTOR + 0.5 * (temp >= offset) - 0.5 * (temp < offset));
0271     printf("Serial %d old corr  %f change %f newcorr %f \n",
0272            chamberSerial,
0273            temp,
0274            offset,
0275            (float)chamberObj->chamberCorrections[chamberSerial - 1].cfeb_timing_corr / FACTOR);
0276   }
0277   fclose(foffset);
0278 
0279   //Read in a 3rd order correction for chamber offsets derived from data
0280   FILE *foffsetAgain =
0281       fopen("/afs/cern.ch/user/d/deisher/public/TimingCorrections2009/CathodeTimingCorrection_DB_12082010.txt", "r");
0282   while (!feof(foffsetAgain)) {
0283     //note space at end of format string to convert last \n
0284     int check = fscanf(foffsetAgain, "%d %d %d %d %f \n", &iE, &iS, &iR, &iC, &offset);
0285     if (check != 5) {
0286       printf("offsetAgain file has an unexpected format \n");
0287       assert(0);
0288     }
0289     int chamberSerial = 0;
0290     if (iS == 1 && iR == 4)
0291       iR = 1;
0292     chamberSerial = indexer.chamberIndex(iE, iS, iR, iC);
0293     //printf("chamberLabel %s (%d %d %d %d) chamberSerial %d delay %d \n",label,c_endcap,c_station, c_ring, c_chamber, chamberSerial,delay);
0294     float temp = float(chamberObj->chamberCorrections[chamberSerial - 1].cfeb_timing_corr) / FACTOR;
0295     chamberObj->chamberCorrections[chamberSerial - 1].cfeb_timing_corr =
0296         (short int)((temp - offset) * FACTOR + 0.5 * (temp >= offset) - 0.5 * (temp < offset));
0297     printf("Serial %d old corr  %f change %f newcorr %f \n",
0298            chamberSerial,
0299            temp,
0300            offset,
0301            (float)chamberObj->chamberCorrections[chamberSerial - 1].cfeb_timing_corr / FACTOR);
0302   }
0303   fclose(foffsetAgain);
0304 
0305   return chamberObj;
0306 }
0307 
0308 #endif