Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:19:13

0001 #include "HtrXmlPatternTool.h"
0002 #include "HtrXmlPatternToolParameters.h"
0003 #include "HtrXmlPatternSet.h"
0004 #include "HtrXmlPatternWriter.h"
0005 #include "TFile.h"
0006 #include "TH1.h"
0007 #include <filesystem>
0008 #include <fstream>
0009 #include <iostream>
0010 
0011 HtrXmlPatternTool::HtrXmlPatternTool(HtrXmlPatternToolParameters* params) {
0012   m_params = params;
0013 
0014   //slotsOn array index corresponds to physical number (e.g., slotsOn[1] for slot 1)
0015   int slotsOn[ChannelPattern::NUM_SLOTS];
0016   int cratesOn[ChannelPattern::NUM_CRATES];
0017 
0018   //turn off all slots
0019   for (int i = 0; i < ChannelPattern::NUM_SLOTS; i++)
0020     slotsOn[i] = 0;
0021   //turn on slots 2,3,4,5,6,7,8
0022   for (int i = 2; i < 9; i++)
0023     slotsOn[i] = 1;
0024   //turn on slots 13,14,15,16,17,18
0025   for (int i = 13; i < 19; i++)
0026     slotsOn[i] = 1;
0027 
0028   //turn on all crates
0029   for (int i = 0; i < ChannelPattern::NUM_CRATES; i++)
0030     cratesOn[i] = 1;
0031   //turn off two unused crates
0032   cratesOn[8] = 0;
0033   cratesOn[16] = 0;
0034 
0035   m_patternSet = new HtrXmlPatternSet(cratesOn, slotsOn);
0036   m_xmlWriter.setTagName(m_params->m_file_tag);
0037 }
0038 
0039 HtrXmlPatternTool::~HtrXmlPatternTool() { delete m_patternSet; }
0040 
0041 void HtrXmlPatternTool::Fill(const HcalElectronicsId HEID, HBHEDigiCollection::const_iterator data) {
0042   CrateData* cd = m_patternSet->getCrate(HEID.readoutVMECrateId());
0043   HalfHtrData* hd = nullptr;
0044   ChannelPattern* cp = nullptr;
0045 
0046   if (cd) {
0047     hd = cd->getHalfHtrData(HEID.htrSlot(), HEID.htrTopBottom());
0048     if (hd) {
0049       hd->setSpigot(HEID.spigot());
0050       hd->setDCC(HEID.dccid());
0051 
0052       cp = hd->getPattern(HEID.htrChanId());
0053       if (cp)
0054         cp->Fill(m_params, data);
0055       else if (m_params->m_show_errors)
0056         std::cerr << "Bad (crate,slot,channel): (" << HEID.readoutVMECrateId() << "," << HEID.htrSlot() << ","
0057                   << HEID.htrChanId() << ")" << std::endl;
0058     } else if (m_params->m_show_errors)
0059       std::cerr << "Bad (crate,slot): (" << HEID.readoutVMECrateId() << "," << HEID.htrSlot() << ")" << std::endl;
0060   } else if (m_params->m_show_errors)
0061     std::cerr << "Bad (crate): (" << HEID.readoutVMECrateId() << ")" << std::endl;
0062 }
0063 
0064 void HtrXmlPatternTool::Fill(const HcalElectronicsId HEID, HFDigiCollection::const_iterator data) {
0065   CrateData* cd = m_patternSet->getCrate(HEID.readoutVMECrateId());
0066   HalfHtrData* hd = nullptr;
0067   ChannelPattern* cp = nullptr;
0068 
0069   if (cd) {
0070     hd = cd->getHalfHtrData(HEID.htrSlot(), HEID.htrTopBottom());
0071     if (hd) {
0072       hd->setSpigot(HEID.spigot());
0073       hd->setDCC(HEID.dccid());
0074 
0075       cp = hd->getPattern(HEID.htrChanId());
0076       if (cp)
0077         cp->Fill(m_params, data);
0078       else if (m_params->m_show_errors)
0079         std::cerr << "Bad (crate,slot,channel): (" << HEID.readoutVMECrateId() << "," << HEID.htrSlot() << ","
0080                   << HEID.htrChanId() << ")" << std::endl;
0081     } else if (m_params->m_show_errors)
0082       std::cerr << "Bad (crate,slot): (" << HEID.readoutVMECrateId() << "," << HEID.htrSlot() << ")" << std::endl;
0083   } else if (m_params->m_show_errors)
0084     std::cerr << "Bad (crate): (" << HEID.readoutVMECrateId() << ")" << std::endl;
0085 }
0086 
0087 void HtrXmlPatternTool::Fill(const HcalElectronicsId HEID, HODigiCollection::const_iterator data) {
0088   CrateData* cd = m_patternSet->getCrate(HEID.readoutVMECrateId());
0089   HalfHtrData* hd = nullptr;
0090   ChannelPattern* cp = nullptr;
0091 
0092   if (cd) {
0093     hd = cd->getHalfHtrData(HEID.htrSlot(), HEID.htrTopBottom());
0094     if (hd) {
0095       hd->setSpigot(HEID.spigot());
0096       hd->setDCC(HEID.dccid());
0097 
0098       cp = hd->getPattern(HEID.htrChanId());
0099       if (cp)
0100         cp->Fill(m_params, data);
0101       else if (m_params->m_show_errors)
0102         std::cerr << "Bad (crate,slot,channel): (" << HEID.readoutVMECrateId() << "," << HEID.htrSlot() << ","
0103                   << HEID.htrChanId() << ")" << std::endl;
0104     } else if (m_params->m_show_errors)
0105       std::cerr << "Bad (crate,slot): (" << HEID.readoutVMECrateId() << "," << HEID.htrSlot() << ")" << std::endl;
0106   } else if (m_params->m_show_errors)
0107     std::cerr << "Bad (crate): (" << HEID.readoutVMECrateId() << ")" << std::endl;
0108 }
0109 
0110 void HtrXmlPatternTool::prepareDirs() { std::filesystem::create_directory(m_params->m_output_directory); }
0111 
0112 void HtrXmlPatternTool::writeXML() {
0113   std::cout << "Writing XML..." << std::endl;
0114   std::ofstream* of = nullptr;
0115 
0116   if (m_params->m_XML_file_mode == 1) {
0117     std::string name = m_params->m_output_directory + (m_params->m_file_tag) + "_all.xml";
0118     of = new std::ofstream(name.c_str(), std::ios_base::out | std::ios_base::trunc);
0119     if (!of->good()) {
0120       std::cerr << "XML output file " << name << " is bad." << std::endl;
0121       return;
0122     }
0123     (*of) << "<?xml version='1.0' encoding='UTF-8'?>" << std::endl;
0124     (*of) << "<CFGBrickSet name='" << m_params->m_file_tag << "'>" << std::endl;
0125   }
0126 
0127   for (int crate = 0; crate < ChannelPattern::NUM_CRATES; crate++) {
0128     CrateData* cd = m_patternSet->getCrate(crate);
0129     if (cd == nullptr)
0130       continue;
0131 
0132     if (m_params->m_XML_file_mode == 2) {
0133       std::string name = m_params->m_output_directory + (m_params->m_file_tag);
0134       char cr_name[256];
0135       snprintf(cr_name, 256, "_crate_%d.xml", crate);
0136       name += cr_name;
0137       of = new std::ofstream(name.c_str(), std::ios_base::out | std::ios_base::trunc);
0138       if (!of->good()) {
0139         std::cerr << "XML output file " << name << " is bad." << std::endl;
0140         delete of;
0141         return;
0142       }
0143       (*of) << "<?xml version='1.0' encoding='UTF-8'?>" << std::endl;
0144       (*of) << "<CFGBrickSet name='" << m_params->m_file_tag << "'>" << std::endl;
0145     }
0146 
0147     for (int slot = 0; slot < ChannelPattern::NUM_SLOTS; slot++) {
0148       for (int tb = 0; tb <= 1; tb++) {
0149         HalfHtrData* hd = cd->getHalfHtrData(slot, tb);
0150         if (hd == nullptr)
0151           continue;
0152         for (int fiber = 1; fiber <= 8; fiber++) {
0153           if (m_params->m_XML_file_mode == 3) {
0154             std::string name = m_params->m_output_directory + (m_params->m_file_tag);
0155             char cr_name[256];
0156             snprintf(cr_name, 256, "_crate_%d_slot_%d_tb_%d_fiber_%d.xml", crate, slot, tb, fiber);
0157             name += cr_name;
0158             of = new std::ofstream(name.c_str(), std::ios_base::out | std::ios_base::trunc);
0159             if (!of->good()) {
0160               std::cerr << "XML output file " << name << " is bad." << std::endl;
0161               delete of;
0162               return;
0163             }
0164             (*of) << "<?xml version='1.0' encoding='UTF-8'?>" << std::endl;
0165           }
0166           m_xmlWriter.writePattern(hd, fiber, *of, 1);
0167           if (m_params->m_XML_file_mode == 3) {
0168             of->close();
0169             delete of;
0170             of = nullptr;
0171           }
0172 
0173         }  //end fiber loop
0174       }    // end tb loop
0175     }      //end slot loop
0176 
0177     if (m_params->m_XML_file_mode == 2) {
0178       (*of) << "</CFGBrickSet>" << std::endl;
0179       of->close();
0180       delete of;
0181       of = nullptr;
0182     }
0183 
0184   }  //end crate loop
0185 
0186   if (m_params->m_XML_file_mode == 1) {
0187     (*of) << "</CFGBrickSet>" << std::endl;
0188     of->close();
0189     delete of;
0190     of = nullptr;
0191   }
0192 }
0193 
0194 void HtrXmlPatternTool::createHists() {
0195   std::cout << "Writing root file..." << std::endl;
0196   std::string name = m_params->m_output_directory + "/" + (m_params->m_file_tag) + ".root";
0197   TFile of(name.c_str(), "RECREATE");
0198   if (of.IsZombie()) {
0199     std::cerr << "root output file " << name << " is bad." << std::endl;
0200     return;
0201   }
0202 
0203   of.mkdir("adc");
0204 
0205   for (int crate = 0; crate < ChannelPattern::NUM_CRATES; crate++) {
0206     CrateData* cd = m_patternSet->getCrate(crate);
0207     if (cd == nullptr)
0208       continue;
0209     for (int slot = 0; slot < ChannelPattern::NUM_SLOTS; slot++) {
0210       for (int tb = 0; tb <= 1; tb++) {
0211         HalfHtrData* hd = cd->getHalfHtrData(slot, tb);
0212         if (hd == nullptr)
0213           continue;
0214         for (int chan = 1; chan <= 24; chan++) {
0215           ChannelPattern* cp = hd->getPattern(chan);
0216           char hname[128];
0217           sprintf(hname, "Exact fC Cr%d,%d%s-%d", crate, slot, ((tb == 1) ? ("t") : ("b")), chan);
0218           TH1* hp = new TH1F(hname, hname, ChannelPattern::SAMPLES, -0.5, ChannelPattern::SAMPLES - 0.5);
0219           hp->SetDirectory(nullptr);
0220           sprintf(hname, "Quantized fC Cr%d,%d%s-%d", crate, slot, ((tb == 1) ? ("t") : ("b")), chan);
0221           TH1* hq = new TH1F(hname, hname, ChannelPattern::SAMPLES, -0.5, ChannelPattern::SAMPLES - 0.5);
0222           hp->SetDirectory(nullptr);
0223           sprintf(hname, "Encoded fC Cr%d,%d%s-%d", crate, slot, ((tb == 1) ? ("t") : ("b")), chan);
0224           TH1* ha = new TH1F(hname, hname, ChannelPattern::SAMPLES, -0.5, ChannelPattern::SAMPLES - 0.5);
0225           ha->SetDirectory(nullptr);
0226           for (int i = 0; i < ChannelPattern::SAMPLES; i++) {
0227             hp->Fill(i * 1.0, (*cp)[i]);
0228             hq->Fill(i * 1.0, cp->getQuantized(i));
0229             ha->Fill(i * 1.0, cp->getCoded(i));
0230           }
0231           //of.cd("perfect");   hp->Write();
0232           //of.cd("quantized"); hq->Write();
0233           of.cd("adc");
0234           ha->Write();
0235           delete hp;
0236           delete hq;
0237           delete ha;
0238         }
0239       }
0240     }
0241   }
0242   of.Close();
0243 }