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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
/**
 * Module which outputs a root file of ADC counts (all three gains)
 *   of user-selected channels (defaults to channel 1) for 
 *   user-selected samples (defaults to samples 1,2,3) for
 *   user-selected supermodules.
 * 
 * \author S. Cooper
 *
 */

#include "CaloOnlineTools/EcalTools/plugins/EcalPedHists.h"

EcalPedHists::EcalPedHists(const edm::ParameterSet& ps)
    : runNum_(-1),
      fileName_(ps.getUntrackedParameter<std::string>("fileName", std::string("ecalPedDigiDump"))),
      barrelDigiCollection_(ps.getParameter<edm::InputTag>("EBdigiCollection")),
      endcapDigiCollection_(ps.getParameter<edm::InputTag>("EEdigiCollection")),
      headerProducer_(ps.getParameter<edm::InputTag>("headerProducer")),
      rawDataToken_(consumes<EcalRawDataCollection>(headerProducer_)),
      ebDigiToken_(consumes<EBDigiCollection>(barrelDigiCollection_)),
      eeDigiToken_(consumes<EEDigiCollection>(endcapDigiCollection_)),
      ecalMappingToken_(esConsumes<edm::Transition::BeginRun>()) {
  using namespace std;

  fedMap_ = new EcalFedMap();
  histsFilled_ = false;
  //for(int i=601; i<655; ++i)
  //{
  //  listDefaults.push_back(i);
  //}
  listFEDs_ = ps.getUntrackedParameter<vector<int> >("listFEDs");
  listEBs_ = ps.getUntrackedParameter<vector<string> >("listEBs");

  if (listFEDs_.empty()) {
    allFEDsSelected_ = false;
    //if "actual" EB id given, then convert to FEDid and put in listFEDs_
    if (!listEBs_.empty()) {
      listFEDs_.clear();
      for (vector<string>::const_iterator itr = listEBs_.begin(); itr != listEBs_.end(); ++itr) {
        listFEDs_.push_back(fedMap_->getFedFromSlice(*itr));
      }
    }
  } else if (listFEDs_[0] == -1) {
    // Apply no selection if -1 is passed in FED list
    allFEDsSelected_ = true;
    //debug
    //cout << "no selection on FEDs!" << endl;
    //inputIsOk_=false;
    //return;
    //listFEDs_ = listDefaults;
  } else {
    //in this case, listFEDs should be populated
    allFEDsSelected_ = false;
  }

  if (!allFEDsSelected_) {
    // Verify FED numbers are valid
    for (vector<int>::const_iterator intIter = listFEDs_.begin(); intIter != listFEDs_.end(); intIter++) {
      if (((*intIter) < 601) || (654 < (*intIter))) {
        cout << "[EcalPedHists] FED value: " << (*intIter) << " found in listFEDs. "
             << " Valid range is 601-654. Returning." << endl;
        inputIsOk_ = false;
        return;
      } else
        theRealFedSet_.insert(*intIter);
    }
  }

  vector<int> listDefaults = vector<int>();
  listDefaults.clear();
  for (int i = 1; i < 1701; ++i) {
    listDefaults.push_back(i);
  }
  listChannels_ = ps.getUntrackedParameter<vector<int> >("listChannels", listDefaults);
  listDefaults.clear();
  // Get samples to plot (default to 1,2,3)
  listDefaults.push_back(0);
  listDefaults.push_back(1);
  listDefaults.push_back(2);
  listSamples_ = ps.getUntrackedParameter<vector<int> >("listSamples", listDefaults);

  inputIsOk_ = true;
  vector<int>::iterator intIter;

  // Verify crystal numbers are valid
  for (intIter = listChannels_.begin(); intIter != listChannels_.end(); ++intIter) {
    //TODO: Fix crystal index checking?
    //if ( ((*intIter) < 1)||(1700 < (*intIter)) )
    //{
    //  cout << "[EcalPedHists] ic value: " << (*intIter) << " found in listChannels. "
    //  	  << " Valid range is 1-1700. Returning." << endl;
    //  inputIsOk_ = false;
    //  return;
    //}
  }
  // Verify sample numbers are valid
  for (intIter = listSamples_.begin(); intIter != listSamples_.end(); intIter++) {
    if (((*intIter) < 1) || (10 < (*intIter))) {
      cout << "[EcalPedHists] sample number: " << (*intIter) << " found in listSamples. "
           << " Valid range is 1-10. Returning." << endl;
      inputIsOk_ = false;
      return;
    }
  }
}

EcalPedHists::~EcalPedHists() {}

void EcalPedHists::beginRun(edm::Run const&, edm::EventSetup const& c) {
  ecalElectronicsMap_ = &c.getData(ecalMappingToken_);
}

void EcalPedHists::endRun(edm::Run const&, edm::EventSetup const& c) {}

void EcalPedHists::endJob(void) {
  using namespace std;
  if (inputIsOk_) {
    //debug
    //cout << "endJob:creating root file!" << endl;

    fileName_ += "-" + intToString(runNum_) + ".graph.root";

    TFile root_file_(fileName_.c_str(), "RECREATE");
    //Loop over FEDs first
    for (set<int>::const_iterator FEDitr = theRealFedSet_.begin(); FEDitr != theRealFedSet_.end(); ++FEDitr) {
      if (!histsFilled_)
        break;
      string dir = fedMap_->getSliceFromFed(*FEDitr);
      TDirectory* FEDdir = gDirectory->mkdir(dir.c_str());
      FEDdir->cd();
      //root_file_.mkdir(dir.c_str());
      //root_file_.cd(dir.c_str());
      map<string, TH1F*> mapHistos = FEDsAndHistMaps_[*FEDitr];

      //Loop over channels; write histos and directory structure
      for (vector<int>::const_iterator itr = listChannels_.begin(); itr != listChannels_.end(); itr++) {
        //debug
        //cout << "loop over channels" << endl;

        TH1F* hist = nullptr;
        string chnl = intToString(*itr);
        string name1 = "Cry";
        name1.append(chnl + "Gain1");
        string name2 = "Cry";
        name2.append(chnl + "Gain6");
        string name3 = "Cry";
        name3.append(chnl + "Gain12");
        hist = mapHistos[name1];
        // This is a sanity check only
        if (hist != nullptr) {
          string cryDirName = "Cry_" + chnl;
          TDirectory* cryDir = FEDdir->mkdir(cryDirName.c_str());
          cryDir->cd();
          hist->SetDirectory(cryDir);
          hist->Write();
          hist = mapHistos[name2];
          hist->SetDirectory(cryDir);
          hist->Write();
          hist = mapHistos[name3];
          hist->SetDirectory(cryDir);
          hist->Write();
          //root_file_.cd(dir.c_str());
          root_file_.cd();
        } else {
          cerr << "EcalPedHists: Error: This shouldn't happen!" << endl;
        }
      }
      root_file_.cd();
    }
    root_file_.Close();
  }
}

void EcalPedHists::analyze(const edm::Event& e, const edm::EventSetup& c) {
  using namespace std;
  using namespace edm;

  if (!inputIsOk_)
    return;

  // loop over the headers, this is to detect missing FEDs if all are selected
  if (allFEDsSelected_) {
    edm::Handle<EcalRawDataCollection> DCCHeaders;
    e.getByToken(rawDataToken_, DCCHeaders);
    if (!DCCHeaders.isValid()) {
      edm::LogError("EcalPedHists") << "Error! can't get the product " << headerProducer_;
      return;
    }

    for (EcalRawDataCollection::const_iterator headerItr = DCCHeaders->begin(); headerItr != DCCHeaders->end();
         ++headerItr) {
      int FEDid = 600 + headerItr->id();
      theRealFedSet_.insert(FEDid);
    }
  }

  // loop over fed list and make sure that there are histo maps
  for (set<int>::const_iterator fedItr = theRealFedSet_.begin(); fedItr != theRealFedSet_.end(); ++fedItr) {
    if (FEDsAndHistMaps_.find(*fedItr) == FEDsAndHistMaps_.end())
      initHists(*fedItr);
  }

  //debug
  //cout << "analyze...input is ok? " << inputIsOk_ << endl;

  bool barrelDigisFound = true;
  bool endcapDigisFound = true;
  // get the barrel digis
  // (one digi for each crystal)
  // TODO; SIC: fix this behavior
  Handle<EBDigiCollection> barrelDigis;
  e.getByToken(ebDigiToken_, barrelDigis);
  if (!barrelDigis.isValid()) {
    edm::LogError("EcalPedOffset") << "Error! can't get the product " << barrelDigiCollection_;
    barrelDigisFound = false;
  }
  // get the endcap digis
  // (one digi for each crystal)
  // TODO; SIC: fix this behavior
  Handle<EEDigiCollection> endcapDigis;
  e.getByToken(eeDigiToken_, endcapDigis);
  if (!endcapDigis.isValid()) {
    edm::LogError("EcalPedOffset") << "Error! can't get the product " << endcapDigiCollection_;
    endcapDigisFound = false;
  }

  if (barrelDigisFound)
    readEBdigis(barrelDigis);
  if (endcapDigisFound)
    readEEdigis(endcapDigis);
  if (!barrelDigisFound && !endcapDigisFound)
    edm::LogError("EcalPedOffset") << "No digis found in the event!";

  if (runNum_ == -1)
    runNum_ = e.id().run();
}

// insert the 3-entry hist map into the map keyed by FED number
void EcalPedHists::initHists(int FED) {
  using namespace std;
  //using namespace edm;

  std::map<string, TH1F*> histMap;
  //debug
  //cout << "Initializing map for FED:" << *FEDitr << endl;
  for (vector<int>::const_iterator intIter = listChannels_.begin(); intIter != listChannels_.end(); ++intIter) {
    //Put 3 histos (1 per gain) for the channel into the map
    string FEDid = intToString(FED);
    string chnl = intToString(*intIter);
    string title1 = "Gain1 ADC Counts for channel ";
    title1.append(chnl);
    string name1 = "Cry";
    name1.append(chnl + "Gain1");
    string title2 = "Gain6 ADC Counts for channel ";
    title2.append(chnl);
    string name2 = "Cry";
    name2.append(chnl + "Gain6");
    string title3 = "Gain12 ADC Counts for channel ";
    title3.append(chnl);
    string name3 = "Cry";
    name3.append(chnl + "Gain12");
    histMap.insert(make_pair(name1, new TH1F(name1.c_str(), title1.c_str(), 75, 175.0, 250.0)));
    histMap[name1]->SetDirectory(nullptr);
    histMap.insert(make_pair(name2, new TH1F(name2.c_str(), title2.c_str(), 75, 175.0, 250.0)));
    histMap[name2]->SetDirectory(nullptr);
    histMap.insert(make_pair(name3, new TH1F(name3.c_str(), title3.c_str(), 75, 175.0, 250.0)));
    histMap[name3]->SetDirectory(nullptr);
  }
  FEDsAndHistMaps_.insert(make_pair(FED, histMap));
}

void EcalPedHists::readEBdigis(edm::Handle<EBDigiCollection> digis) {
  using namespace std;
  using namespace edm;
  //debug
  //cout << "readEBdigis" << endl;

  // Loop over digis
  for (EBDigiCollection::const_iterator digiItr = digis->begin(); digiItr != digis->end(); ++digiItr) {
    EBDetId detId = EBDetId(digiItr->id());
    EcalElectronicsId elecId = ecalElectronicsMap_->getElectronicsId(detId);
    int FEDid = 600 + elecId.dccId();
    int crystalId = detId.ic();

    //debug
    //cout << "FEDid:" << FEDid << " cryId:" << crystalId << endl;
    //cout << "FEDid:" << FEDid << endl;
    //Select desired supermodules only
    set<int>::const_iterator fedIter = find(theRealFedSet_.begin(), theRealFedSet_.end(), FEDid);
    if (fedIter == theRealFedSet_.end())
      continue;

    // Select desired channels only
    vector<int>::iterator icIter;
    icIter = find(listChannels_.begin(), listChannels_.end(), crystalId);
    if (icIter == listChannels_.end())
      continue;

    // Get the adc counts from the selected samples and fill the corresponding histogram
    // Must subtract 1 from user-given sample list (e.g., user's sample 1 -> sample 0)
    for (vector<int>::iterator itr = listSamples_.begin(); itr != listSamples_.end(); itr++) {
      histsFilled_ = true;
      map<string, TH1F*> mapHistos = FEDsAndHistMaps_[FEDid];
      string chnl = intToString(crystalId);
      string name1 = "Cry";
      name1.append(chnl + "Gain1");
      string name2 = "Cry";
      name2.append(chnl + "Gain6");
      string name3 = "Cry";
      name3.append(chnl + "Gain12");
      TH1F* hist = nullptr;
      if (((EBDataFrame)(*digiItr)).sample(*itr - 1).gainId() == 3)
        hist = mapHistos[name1];
      if (((EBDataFrame)(*digiItr)).sample(*itr - 1).gainId() == 2)
        hist = mapHistos[name2];
      if (((EBDataFrame)(*digiItr)).sample(*itr - 1).gainId() == 1)
        hist = mapHistos[name3];
      if (hist != nullptr)
        hist->Fill(((EBDataFrame)(*digiItr)).sample(*itr - 1).adc());
      else
        cerr << "EcalPedHistDumper: Error: This shouldn't happen!" << endl;
    }
  }
}

void EcalPedHists::readEEdigis(edm::Handle<EEDigiCollection> digis) {
  using namespace std;
  using namespace edm;
  //debug
  //cout << "readEEdigis" << endl;

  // Loop over digis
  for (EEDigiCollection::const_iterator digiItr = digis->begin(); digiItr != digis->end(); ++digiItr) {
    EEDetId detId = EEDetId(digiItr->id());
    EcalElectronicsId elecId = ecalElectronicsMap_->getElectronicsId(detId);
    int FEDid = 600 + elecId.dccId();
    int crystalId = 10000 * FEDid + 100 * elecId.towerId() + 5 * (elecId.stripId() - 1) + elecId.xtalId();

    //Select desired FEDs only
    set<int>::const_iterator fedIter = find(theRealFedSet_.begin(), theRealFedSet_.end(), FEDid);
    if (fedIter == theRealFedSet_.end())
      continue;

    // Select desired channels only
    vector<int>::iterator icIter;
    icIter = find(listChannels_.begin(), listChannels_.end(), crystalId);
    if (icIter == listChannels_.end())
      continue;

    // Get the adc counts from the selected samples and fill the corresponding histogram
    // Must subtract 1 from user-given sample list (e.g., user's sample 1 -> sample 0)
    for (vector<int>::iterator itr = listSamples_.begin(); itr != listSamples_.end(); itr++) {
      histsFilled_ = true;
      map<string, TH1F*> mapHistos = FEDsAndHistMaps_[FEDid];
      string chnl = intToString(crystalId);
      string name1 = "Cry";
      name1.append(chnl + "Gain1");
      string name2 = "Cry";
      name2.append(chnl + "Gain6");
      string name3 = "Cry";
      name3.append(chnl + "Gain12");
      TH1F* hist = nullptr;
      if (((EBDataFrame)(*digiItr)).sample(*itr - 1).gainId() == 3)
        hist = mapHistos[name1];
      if (((EBDataFrame)(*digiItr)).sample(*itr - 1).gainId() == 2)
        hist = mapHistos[name2];
      if (((EBDataFrame)(*digiItr)).sample(*itr - 1).gainId() == 1)
        hist = mapHistos[name3];
      if (hist != nullptr)
        hist->Fill(((EBDataFrame)(*digiItr)).sample(*itr - 1).adc());
      else
        cerr << "EcalPedHistDumper: Error: This shouldn't happen!" << endl;
    }
  }
}

std::string EcalPedHists::intToString(int num) {
  using namespace std;
  //
  // outputs the number into the string stream and then flushes
  // the buffer (makes sure the output is put into the stream)
  //
  ostringstream myStream;  //creates an ostringstream object
  myStream << num << flush;
  return (myStream.str());  //returns the string form of the stringstream object
}