CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/IORawData/CaloPatterns/src/HtrXmlPatternTool.cc

Go to the documentation of this file.
00001 #include "HtrXmlPatternTool.h"
00002 #include "HtrXmlPatternToolParameters.h"
00003 #include "HtrXmlPatternSet.h"
00004 #include "HtrXmlPatternWriter.h"
00005 #include "TFile.h"
00006 #include "TH1.h"
00007 #include <iostream>
00008 #include <fstream>
00009 #include "boost/filesystem/operations.hpp"
00010 
00011 HtrXmlPatternTool::HtrXmlPatternTool(HtrXmlPatternToolParameters* params) {
00012   m_params = params;
00013 
00014   //slotsOn array index corresponds to physical number (e.g., slotsOn[1] for slot 1)
00015   int slotsOn [ChannelPattern::NUM_SLOTS];
00016   int cratesOn[ChannelPattern::NUM_CRATES];
00017 
00018   //turn off all slots
00019   for (int i=0; i<ChannelPattern::NUM_SLOTS; i++) slotsOn[i]=0;
00020   //turn on slots 2,3,4,5,6,7,8
00021   for (int i=2; i<9; i++)   slotsOn[i]=1;
00022   //turn on slots 13,14,15,16,17,18
00023   for (int i=13; i<19; i++) slotsOn[i]=1;
00024 
00025   //turn on all crates
00026   for (int i=0; i<ChannelPattern::NUM_CRATES; i++) cratesOn[i]=1;
00027   //turn off two unused crates
00028   cratesOn[ 8] = 0;
00029   cratesOn[16] = 0;
00030 
00031   m_patternSet=new HtrXmlPatternSet(cratesOn,slotsOn);
00032   m_xmlWriter.setTagName(m_params->m_file_tag);
00033 
00034 }
00035 
00036 HtrXmlPatternTool::~HtrXmlPatternTool() {
00037   delete m_patternSet;
00038 }
00039 
00040 void HtrXmlPatternTool::Fill(const HcalElectronicsId HEID,HBHEDigiCollection::const_iterator data) {
00041   CrateData* cd=m_patternSet->getCrate(HEID.readoutVMECrateId());
00042   HalfHtrData* hd=0;
00043   ChannelPattern* cp=0;
00044 
00045   if (cd) {
00046     hd=cd->getHalfHtrData(HEID.htrSlot(),HEID.htrTopBottom());
00047     if (hd) {
00048       hd->setSpigot(HEID.spigot());
00049       hd->setDCC(HEID.dccid());
00050 
00051       cp=hd->getPattern(HEID.htrChanId());
00052       if (cp) cp->Fill(m_params,data);
00053       else if(m_params->m_show_errors) std::cerr << "Bad (crate,slot,channel): (" 
00054                                                  << HEID.readoutVMECrateId() << "," 
00055                                                  << HEID.htrSlot()           << "," 
00056                                                  << HEID.htrChanId()         << ")" << std::endl;
00057     }
00058     else if(m_params->m_show_errors) std::cerr << "Bad (crate,slot): (" 
00059                                                << HEID.readoutVMECrateId() << "," 
00060                                                << HEID.htrSlot()           << ")" << std::endl;
00061   }
00062   else if(m_params->m_show_errors) std::cerr << "Bad (crate): (" << HEID.readoutVMECrateId() << ")" << std::endl;
00063 }
00064 
00065 void HtrXmlPatternTool::Fill(const HcalElectronicsId HEID,HFDigiCollection::const_iterator data) {
00066   CrateData* cd=m_patternSet->getCrate(HEID.readoutVMECrateId());
00067   HalfHtrData* hd=0;
00068   ChannelPattern* cp=0;
00069 
00070   if (cd) {
00071     hd=cd->getHalfHtrData(HEID.htrSlot(),HEID.htrTopBottom());
00072     if (hd) {
00073       hd->setSpigot(HEID.spigot());
00074       hd->setDCC(HEID.dccid());
00075 
00076       cp=hd->getPattern(HEID.htrChanId());
00077       if (cp) cp->Fill(m_params,data);
00078       else if(m_params->m_show_errors) std::cerr << "Bad (crate,slot,channel): (" 
00079                                                  << HEID.readoutVMECrateId() << "," 
00080                                                  << HEID.htrSlot()           << "," 
00081                                                  << HEID.htrChanId()         << ")" << std::endl;
00082     }
00083     else if(m_params->m_show_errors) std::cerr << "Bad (crate,slot): (" 
00084                                                << HEID.readoutVMECrateId() << "," 
00085                                                << HEID.htrSlot()           << ")" << std::endl;
00086   }
00087   else if(m_params->m_show_errors) std::cerr << "Bad (crate): (" << HEID.readoutVMECrateId() << ")" << std::endl;
00088 }
00089 
00090 void HtrXmlPatternTool::Fill(const HcalElectronicsId HEID,HODigiCollection::const_iterator data) {
00091   CrateData* cd=m_patternSet->getCrate(HEID.readoutVMECrateId());
00092   HalfHtrData* hd=0;
00093   ChannelPattern* cp=0;
00094 
00095   if (cd) {
00096     hd=cd->getHalfHtrData(HEID.htrSlot(),HEID.htrTopBottom());
00097     if (hd) {
00098       hd->setSpigot(HEID.spigot());
00099       hd->setDCC(HEID.dccid());
00100 
00101       cp=hd->getPattern(HEID.htrChanId());
00102       if (cp) cp->Fill(m_params,data);
00103       else if(m_params->m_show_errors) std::cerr << "Bad (crate,slot,channel): (" 
00104                                                  << HEID.readoutVMECrateId() << "," 
00105                                                  << HEID.htrSlot()           << "," 
00106                                                  << HEID.htrChanId()         << ")" << std::endl;
00107     }
00108     else if(m_params->m_show_errors) std::cerr << "Bad (crate,slot): (" 
00109                                                << HEID.readoutVMECrateId() << "," 
00110                                                << HEID.htrSlot()           << ")" << std::endl;
00111   }
00112   else if(m_params->m_show_errors) std::cerr << "Bad (crate): (" << HEID.readoutVMECrateId() << ")" << std::endl;
00113 }
00114 
00115 void HtrXmlPatternTool::prepareDirs() {
00116   boost::filesystem::create_directory(m_params->m_output_directory);
00117 }
00118 
00119 void HtrXmlPatternTool::writeXML() {
00120   std::cout << "Writing XML..." << std::endl;
00121   std::ofstream* of=0;
00122 
00123   if (m_params->m_XML_file_mode==1) {
00124     std::string name=m_params->m_output_directory+(m_params->m_file_tag)+"_all.xml";
00125     of=new std::ofstream(name.c_str(),std::ios_base::out|std::ios_base::trunc);
00126     if (!of->good()) {
00127       std::cerr << "XML output file " << name << " is bad." << std::endl;
00128       return;
00129     }
00130     (*of) << "<?xml version='1.0' encoding='UTF-8'?>" << std::endl;
00131     (*of) << "<CFGBrickSet name='" << m_params->m_file_tag << "'>" << std::endl;
00132   }
00133 
00134   for (int crate=0; crate<ChannelPattern::NUM_CRATES; crate++) {
00135     CrateData* cd=m_patternSet->getCrate(crate);
00136     if (cd==0) continue;
00137 
00138     if (m_params->m_XML_file_mode==2) {
00139       std::string name=m_params->m_output_directory+(m_params->m_file_tag);
00140       char cr_name[256];
00141       snprintf(cr_name,256,"_crate_%d.xml",crate);
00142       name += cr_name;
00143       of=new std::ofstream(name.c_str(),std::ios_base::out|std::ios_base::trunc);
00144       if (!of->good()) {
00145         std::cerr << "XML output file " << name << " is bad." << std::endl;
00146         return;
00147       }
00148       (*of) << "<?xml version='1.0' encoding='UTF-8'?>" << std::endl;
00149       (*of) << "<CFGBrickSet name='" << m_params->m_file_tag << "'>" << std::endl;
00150     }
00151 
00152     for (int slot=0; slot<ChannelPattern::NUM_SLOTS; slot++) {
00153       for (int tb=0; tb<=1; tb++) {
00154         HalfHtrData* hd=cd->getHalfHtrData(slot,tb);
00155         if (hd==0) continue;
00156         for (int fiber=1; fiber<=8; fiber++) {
00157 
00158           if (m_params->m_XML_file_mode==3) {
00159             std::string name=m_params->m_output_directory+(m_params->m_file_tag);
00160             char cr_name[256];
00161             snprintf(cr_name,256,"_crate_%d_slot_%d_tb_%d_fiber_%d.xml",crate,slot,tb,fiber);
00162             name += cr_name;
00163             of=new std::ofstream(name.c_str(),std::ios_base::out|std::ios_base::trunc);
00164             if (!of->good()) {
00165               std::cerr << "XML output file " << name << " is bad." << std::endl;
00166               return;
00167             }
00168             (*of) << "<?xml version='1.0' encoding='UTF-8'?>" << std::endl;
00169           }
00170           m_xmlWriter.writePattern(hd,fiber,*of,1);
00171           if (m_params->m_XML_file_mode==3) {
00172             of->close();
00173             delete of;
00174             of=0;
00175           }
00176 
00177         } //end fiber loop
00178       } // end tb loop
00179     } //end slot loop
00180     
00181     if (m_params->m_XML_file_mode==2) {
00182       (*of) << "</CFGBrickSet>" << std::endl;
00183       of->close();
00184       delete of;
00185       of=0;
00186     }
00187     
00188   } //end crate loop
00189   
00190   if (m_params->m_XML_file_mode==1) {
00191     (*of) << "</CFGBrickSet>" << std::endl;
00192     of->close();
00193     delete of;
00194     of=0;
00195   }
00196 }
00197 
00198 void HtrXmlPatternTool::createHists() {
00199   std::cout << "Writing root file..." << std::endl;
00200   std::string name=m_params->m_output_directory+"/"+(m_params->m_file_tag)+".root";
00201   TFile of(name.c_str(),"RECREATE");
00202   if (of.IsZombie()) {
00203     std::cerr << "root output file " << name << " is bad." << std::endl;
00204     return;
00205   }
00206 
00207   of.mkdir("adc");
00208 
00209   for (int crate=0; crate<ChannelPattern::NUM_CRATES; crate++) {
00210     CrateData* cd=m_patternSet->getCrate(crate);
00211     if (cd==0) continue;
00212     for (int slot=0; slot<ChannelPattern::NUM_SLOTS; slot++) {
00213       for (int tb=0; tb<=1; tb++) {
00214         HalfHtrData* hd=cd->getHalfHtrData(slot,tb);
00215         if (hd==0) continue;
00216         for (int chan=1; chan<=24; chan++) {
00217           ChannelPattern* cp=hd->getPattern(chan);
00218           char hname[128];
00219           sprintf(hname,"Exact fC Cr%d,%d%s-%d",crate,slot,
00220                   ((tb==1)?("t"):("b")),chan);
00221           TH1* hp=new TH1F(hname,hname,ChannelPattern::SAMPLES,-0.5,ChannelPattern::SAMPLES-0.5);
00222           hp->SetDirectory(0);
00223           sprintf(hname,"Quantized fC Cr%d,%d%s-%d",crate,slot,
00224                   ((tb==1)?("t"):("b")),chan);
00225           TH1* hq=new TH1F(hname,hname,ChannelPattern::SAMPLES,-0.5,ChannelPattern::SAMPLES-0.5);
00226           hp->SetDirectory(0);
00227           sprintf(hname,"Encoded fC Cr%d,%d%s-%d",crate,slot,
00228                   ((tb==1)?("t"):("b")),chan);
00229           TH1* ha=new TH1F(hname,hname,ChannelPattern::SAMPLES,-0.5,ChannelPattern::SAMPLES-0.5);
00230           ha->SetDirectory(0);
00231           for (int i=0; i<ChannelPattern::SAMPLES; i++) {
00232             hp->Fill(i*1.0,(*cp)[i]);
00233             hq->Fill(i*1.0,cp->getQuantized(i));
00234             ha->Fill(i*1.0,cp->getCoded(i));
00235           }
00236           //of.cd("perfect");   hp->Write();
00237           //of.cd("quantized"); hq->Write();
00238           of.cd("adc");       ha->Write();
00239           delete hp;
00240           delete hq;
00241           delete ha;
00242         }
00243       }
00244     }
00245   }
00246   of.Close();
00247 }