Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
#include <fstream>
#include <iostream>

#include "CalibMuon/CSCCalibration/interface/CSCCrosstalkConditions.h"

CSCcrosstalk *CSCCrosstalkConditions::prefillCrosstalk() {
  float mean, min, minchi;
  int seed;
  int old_chamber_id, old_strip, new_chamber_id, new_strip;
  float old_slope_right, old_slope_left, old_intercept_right;
  float old_intercept_left, old_chi2_right, old_chi2_left;
  std::vector<int> old_cham_id;
  std::vector<int> old_strips;
  std::vector<float> old_slope_r;
  std::vector<float> old_intercept_r;
  std::vector<float> old_chi2_r;
  std::vector<float> old_slope_l;
  std::vector<float> old_intercept_l;
  std::vector<float> old_chi2_l;
  float new_slope_right, new_slope_left, new_intercept_right;
  float new_intercept_left, new_chi2_right, new_chi2_left;
  std::vector<int> new_cham_id;
  std::vector<int> new_strips;
  std::vector<float> new_slope_r;
  std::vector<float> new_intercept_r;
  std::vector<float> new_chi2_r;
  std::vector<float> new_slope_l;
  std::vector<float> new_intercept_l;
  std::vector<float> new_chi2_l;

  const CSCDetId &detId = CSCDetId();
  CSCcrosstalk *cncrosstalk = new CSCcrosstalk();

  int max_istrip, id_layer, max_ring, max_cham;
  unsigned int old_nrlines = 0;
  unsigned int new_nrlines = 0;
  seed = 10000;
  srand(seed);
  mean = -0.0009, min = 0.035, minchi = 1.5;

  // endcap=1 to 2,station=1 to 4, ring=1 to 4,chamber=1 to 36,layer=1 to 6
  std::ifstream olddata;
  olddata.open("old_xtalk.dat", std::ios::in);
  if (!olddata) {
    std::cerr << "Error: old_xtalk.dat -> no such file!" << std::endl;
    exit(1);
  }

  while (!olddata.eof()) {
    olddata >> old_chamber_id >> old_strip >> old_slope_right >> old_intercept_right >> old_chi2_right >>
        old_slope_left >> old_intercept_left >> old_chi2_left;
    old_cham_id.push_back(old_chamber_id);
    old_strips.push_back(old_strip);
    old_slope_r.push_back(old_slope_right);
    old_slope_l.push_back(old_slope_left);
    old_intercept_r.push_back(old_intercept_right);
    old_intercept_l.push_back(old_intercept_left);
    old_chi2_r.push_back(old_chi2_right);
    old_chi2_l.push_back(old_chi2_left);
    old_nrlines++;
  }
  olddata.close();

  std::ifstream newdata;
  newdata.open("new_xtalk.txt", std::ios::in);
  if (!newdata) {
    std::cerr << "Error: new_xtalk.dat -> no such file!" << std::endl;
    exit(1);
  }

  while (!newdata.eof()) {
    newdata >> new_chamber_id >> new_strip >> new_slope_right >> new_intercept_right >> new_chi2_right >>
        new_slope_left >> new_intercept_left >> new_chi2_left;
    new_cham_id.push_back(new_chamber_id);
    new_strips.push_back(new_strip);
    new_slope_r.push_back(new_slope_right);
    new_slope_l.push_back(new_slope_left);
    new_intercept_r.push_back(new_intercept_right);
    new_intercept_l.push_back(new_intercept_left);
    new_chi2_r.push_back(new_chi2_right);
    new_chi2_l.push_back(new_chi2_left);
    new_nrlines++;
  }
  newdata.close();

  for (int iendcap = detId.minEndcapId(); iendcap <= detId.maxEndcapId(); iendcap++) {
    for (int istation = detId.minStationId(); istation <= detId.maxStationId(); istation++) {
      max_ring = detId.maxRingId();
      // station 4 ring 4 not there(36 chambers*2 missing)
      // 3 rings max this way of counting (ME1a & b)
      if (istation == 1)
        max_ring = 3;
      if (istation == 2)
        max_ring = 2;
      if (istation == 3)
        max_ring = 2;
      if (istation == 4)
        max_ring = 1;

      for (int iring = detId.minRingId(); iring <= max_ring; iring++) {
        max_istrip = 80;
        max_cham = detId.maxChamberId();
        if (istation == 1 && iring == 1)
          max_cham = 36;
        if (istation == 1 && iring == 2)
          max_cham = 36;
        if (istation == 1 && iring == 3)
          max_cham = 36;
        if (istation == 2 && iring == 1)
          max_cham = 18;
        if (istation == 2 && iring == 2)
          max_cham = 36;
        if (istation == 3 && iring == 1)
          max_cham = 18;
        if (istation == 3 && iring == 2)
          max_cham = 36;
        if (istation == 4 && iring == 1)
          max_cham = 18;
        // station 1 ring 3 has 64 strips per layer instead of 80(minus & plus
        // side!!!)

        for (int ichamber = detId.minChamberId(); ichamber <= max_cham; ichamber++) {
          for (int ilayer = detId.minLayerId(); ilayer <= detId.maxLayerId(); ilayer++) {
            // station 1 ring 3 has 64 strips per layer instead of 80
            if (istation == 1 && iring == 3)
              max_istrip = 64;

            std::vector<CSCcrosstalk::Item> itemvector;
            itemvector.resize(max_istrip);
            id_layer = 100000 * iendcap + 10000 * istation + 1000 * iring + 10 * ichamber + ilayer;

            for (int istrip = 0; istrip < max_istrip; istrip++) {
              // create fake values
              itemvector[istrip].xtalk_slope_right =
                  -((double)rand() / ((double)(RAND_MAX) + (double)(1))) / 10000 + mean;
              itemvector[istrip].xtalk_intercept_right =
                  ((double)rand() / ((double)(RAND_MAX) + (double)(1))) / 100 + min;
              itemvector[istrip].xtalk_chi2_right = ((double)rand() / ((double)(RAND_MAX) + (double)(1))) + minchi;
              itemvector[istrip].xtalk_slope_left =
                  -((double)rand() / ((double)(RAND_MAX) + (double)(1))) / 10000 + mean;
              itemvector[istrip].xtalk_intercept_left =
                  ((double)rand() / ((double)(RAND_MAX) + (double)(1))) / 100 + min;
              itemvector[istrip].xtalk_chi2_left = ((double)rand() / ((double)(RAND_MAX) + (double)(1))) + minchi;
              cncrosstalk->crosstalk[id_layer] = itemvector;

              if (istrip == 0) {
                itemvector[istrip].xtalk_slope_right =
                    -((double)rand() / ((double)(RAND_MAX) + (double)(1))) / 10000 + mean;
                itemvector[istrip].xtalk_intercept_right =
                    ((double)rand() / ((double)(RAND_MAX) + (double)(1))) / 100 + min;
                itemvector[istrip].xtalk_chi2_right = ((double)rand() / ((double)(RAND_MAX) + (double)(1))) + minchi;
                itemvector[istrip].xtalk_slope_left = 0.0;
                itemvector[istrip].xtalk_intercept_left = 0.0;
                itemvector[istrip].xtalk_chi2_left = 0.0;
                cncrosstalk->crosstalk[id_layer] = itemvector;
              }

              if (istrip == 79) {
                itemvector[istrip].xtalk_slope_right = 0.0;
                itemvector[istrip].xtalk_intercept_right = 0.0;
                itemvector[istrip].xtalk_chi2_right = 0.0;
                itemvector[istrip].xtalk_slope_left =
                    -((double)rand() / ((double)(RAND_MAX) + (double)(1))) / 10000 + mean;
                itemvector[istrip].xtalk_intercept_left =
                    ((double)rand() / ((double)(RAND_MAX) + (double)(1))) / 100 + min;
                itemvector[istrip].xtalk_chi2_left = ((double)rand() / ((double)(RAND_MAX) + (double)(1))) + minchi;
                cncrosstalk->crosstalk[id_layer] = itemvector;
              }
            }
          }
        }
      }
    }

    // overwrite fakes with old values from DB
    int istrip = 0;
    std::vector<CSCcrosstalk::Item> itemvector;
    itemvector.resize(80);

    for (unsigned int mystrip = 0; mystrip < old_nrlines - 1; mystrip++) {
      if (old_strips[mystrip] == 0)
        istrip = 0;
      itemvector[istrip].xtalk_slope_right = old_slope_r[mystrip];
      itemvector[istrip].xtalk_intercept_right = old_intercept_r[mystrip];
      itemvector[istrip].xtalk_chi2_right = old_chi2_r[mystrip];
      itemvector[istrip].xtalk_slope_left = old_slope_l[mystrip];
      itemvector[istrip].xtalk_intercept_left = old_intercept_l[mystrip];
      itemvector[istrip].xtalk_chi2_left = old_chi2_l[mystrip];
      cncrosstalk->crosstalk[old_cham_id[mystrip]] = itemvector;
      istrip++;
    }

    itemvector.resize(64);
    for (unsigned int mystrip = 0; mystrip < old_nrlines - 1; mystrip++) {
      if (old_strips[mystrip] == 0)
        istrip = 0;
      if (old_cham_id[mystrip] >= 113000 && old_cham_id[mystrip] <= 113999) {
        itemvector[istrip].xtalk_slope_right = old_slope_r[mystrip];
        itemvector[istrip].xtalk_intercept_right = old_intercept_r[mystrip];
        itemvector[istrip].xtalk_chi2_right = old_chi2_r[mystrip];
        itemvector[istrip].xtalk_slope_left = old_slope_l[mystrip];
        itemvector[istrip].xtalk_intercept_left = old_intercept_l[mystrip];
        itemvector[istrip].xtalk_chi2_left = old_chi2_l[mystrip];
        cncrosstalk->crosstalk[old_cham_id[mystrip]] = itemvector;
        istrip++;
      }
    }

    itemvector.resize(64);
    for (unsigned int mystrip = 0; mystrip < old_nrlines - 1; mystrip++) {
      if (old_strips[mystrip] == 0)
        istrip = 0;
      if (old_cham_id[mystrip] >= 213000 && old_cham_id[mystrip] <= 213999) {
        itemvector[istrip].xtalk_slope_right = old_slope_r[mystrip];
        itemvector[istrip].xtalk_intercept_right = old_intercept_r[mystrip];
        itemvector[istrip].xtalk_chi2_right = old_chi2_r[mystrip];
        itemvector[istrip].xtalk_slope_left = old_slope_l[mystrip];
        itemvector[istrip].xtalk_intercept_left = old_intercept_l[mystrip];
        itemvector[istrip].xtalk_chi2_left = old_chi2_l[mystrip];
        cncrosstalk->crosstalk[old_cham_id[mystrip]] = itemvector;
        istrip++;
      }
    }

    // overwrite old values with ones from new runs
    itemvector.resize(80);
    for (unsigned int mystrip = 0; mystrip < new_nrlines - 1; mystrip++) {
      if (new_strips[mystrip] == 0)
        istrip = 0;
      itemvector[istrip].xtalk_slope_right = new_slope_r[mystrip];
      itemvector[istrip].xtalk_intercept_right = new_intercept_r[mystrip];
      itemvector[istrip].xtalk_chi2_right = new_chi2_r[mystrip];
      itemvector[istrip].xtalk_slope_left = new_slope_l[mystrip];
      itemvector[istrip].xtalk_intercept_left = new_intercept_l[mystrip];
      itemvector[istrip].xtalk_chi2_left = new_chi2_l[mystrip];
      cncrosstalk->crosstalk[new_cham_id[mystrip]] = itemvector;
      istrip++;
    }

    itemvector.resize(64);
    for (unsigned int mystrip = 0; mystrip < new_nrlines - 1; mystrip++) {
      if (new_strips[mystrip] == 0)
        istrip = 0;
      if (new_cham_id[mystrip] >= 113000 && new_cham_id[mystrip] <= 113999) {
        itemvector[istrip].xtalk_slope_right = new_slope_r[mystrip];
        itemvector[istrip].xtalk_intercept_right = new_intercept_r[mystrip];
        itemvector[istrip].xtalk_chi2_right = new_chi2_r[mystrip];
        itemvector[istrip].xtalk_slope_left = new_slope_l[mystrip];
        itemvector[istrip].xtalk_intercept_left = new_intercept_l[mystrip];
        itemvector[istrip].xtalk_chi2_left = new_chi2_l[mystrip];
        cncrosstalk->crosstalk[new_cham_id[mystrip]] = itemvector;
        istrip++;
      }
    }

    itemvector.resize(64);
    for (unsigned int mystrip = 0; mystrip < new_nrlines - 1; mystrip++) {
      if (new_strips[mystrip] == 0)
        istrip = 0;
      if (new_cham_id[mystrip] >= 213000 && new_cham_id[mystrip] <= 213999) {
        itemvector[istrip].xtalk_slope_right = new_slope_r[mystrip];
        itemvector[istrip].xtalk_intercept_right = new_intercept_r[mystrip];
        itemvector[istrip].xtalk_chi2_right = new_chi2_r[mystrip];
        itemvector[istrip].xtalk_slope_left = new_slope_l[mystrip];
        itemvector[istrip].xtalk_intercept_left = new_intercept_l[mystrip];
        itemvector[istrip].xtalk_chi2_left = new_chi2_l[mystrip];
        cncrosstalk->crosstalk[new_cham_id[mystrip]] = itemvector;
        istrip++;
      }
    }
  }

  return cncrosstalk;
}

CSCCrosstalkConditions::CSCCrosstalkConditions(const edm::ParameterSet &iConfig) {
  // the following line is needed to tell the framework what
  // data is being produced
  // added by Zhen (changed since 1_2_0)
  setWhatProduced(this, &CSCCrosstalkConditions::produceCrosstalk);
  findingRecord<CSCcrosstalkRcd>();
  // now do what ever other initialization is needed
}

CSCCrosstalkConditions::~CSCCrosstalkConditions() {
  // do anything here that needs to be done at desctruction time
  // (e.g. close files, deallocate resources etc.)
}

//
// member functions
//

// ------------ method called to produce the data  ------------
CSCCrosstalkConditions::ReturnType CSCCrosstalkConditions::produceCrosstalk(const CSCcrosstalkRcd &iRecord) {
  // Added by Zhen, need a new object so to not be deleted at exit
  return CSCCrosstalkConditions::ReturnType(prefillCrosstalk());
}

void CSCCrosstalkConditions::setIntervalFor(const edm::eventsetup::EventSetupRecordKey &,
                                            const edm::IOVSyncValue &,
                                            edm::ValidityInterval &oValidity) {
  oValidity = edm::ValidityInterval(edm::IOVSyncValue::beginOfTime(), edm::IOVSyncValue::endOfTime());
}