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
00015 int slotsOn [ChannelPattern::NUM_SLOTS];
00016 int cratesOn[ChannelPattern::NUM_CRATES];
00017
00018
00019 for (int i=0; i<ChannelPattern::NUM_SLOTS; i++) slotsOn[i]=0;
00020
00021 for (int i=2; i<8; i++) slotsOn[i]=1;
00022
00023 for (int i=13; i<19; i++) slotsOn[i]=1;
00024
00025
00026 for (int i=0; i<ChannelPattern::NUM_CRATES; i++) cratesOn[i]=1;
00027
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 bool singleMode=false;
00122 std::ofstream* of=0;
00123
00124 if (m_params->m_single_XML_file) {
00125 singleMode=true;
00126 std::string name=m_params->m_output_directory+(m_params->m_file_tag)+"-all.xml";
00127 of=new std::ofstream(name.c_str(),std::ios_base::out|std::ios_base::trunc);
00128 if (!of->good()) {
00129 std::cerr << "XML output file " << name << " is bad." << std::endl;
00130 return;
00131 }
00132 (*of) << "<?xml version='1.0' encoding='UTF-8'?>" << std::endl;
00133 (*of) << "<CFGBrickSet name='" << m_params->m_file_tag << "'>" << std::endl;
00134 }
00135 for (int crate=0; crate<ChannelPattern::NUM_CRATES; crate++) {
00136 CrateData* cd=m_patternSet->getCrate(crate);
00137 if (cd==0) continue;
00138 for (int slot=0; slot<ChannelPattern::NUM_SLOTS; slot++) {
00139 for (int tb=0; tb<=1; tb++) {
00140 HalfHtrData* hd=cd->getHalfHtrData(slot,tb);
00141 if (hd==0) continue;
00142
00143 for (int fiber=1; fiber<=8; fiber++) {
00144
00145 if (!singleMode) {
00146 char fname[1024];
00147 snprintf(fname,1024,"%s/%s_%d_%d_%d_%d.xml",m_params->m_output_directory.c_str(),
00148 m_params->m_file_tag.c_str(),
00149 hd->getCrate(),hd->getSlot(),hd->getTB(),fiber);
00150 of=new std::ofstream(fname,std::ios_base::out|std::ios_base::trunc);
00151 if (!of->good()) {
00152 std::cerr << "XML output file " << fname << " is bad." << std::endl;
00153 return;
00154 }
00155 (*of) << "<?xml version='1.0' encoding='UTF-8'?>" << std::endl;
00156 }
00157
00158 m_xmlWriter.writePattern(hd,fiber,*of,(singleMode)?(1):(0));
00159
00160 if (!singleMode) {
00161 of->close();
00162 delete of; of=0;
00163 }
00164 }
00165 }
00166 }
00167 }
00168 if (singleMode) {
00169 (*of) << "</CFGBrickSet>" << std::endl;
00170 of->close();
00171 delete of;
00172 of=0;
00173 }
00174 }
00175
00176 void HtrXmlPatternTool::createHists() {
00177 std::cout << "Writing root file..." << std::endl;
00178 std::string name=m_params->m_output_directory+"/"+(m_params->m_file_tag)+".root";
00179 TFile of(name.c_str(),"RECREATE");
00180 if (of.IsZombie()) {
00181 std::cerr << "root output file " << name << " is bad." << std::endl;
00182 return;
00183 }
00184
00185
00186
00187 TDirectory* da=new TDirectory("adc","adc");
00188
00189
00190
00191 da->Write(); delete da;
00192
00193 for (int crate=0; crate<ChannelPattern::NUM_CRATES; crate++) {
00194 CrateData* cd=m_patternSet->getCrate(crate);
00195 if (cd==0) continue;
00196 for (int slot=0; slot<ChannelPattern::NUM_SLOTS; slot++) {
00197 for (int tb=0; tb<=1; tb++) {
00198 HalfHtrData* hd=cd->getHalfHtrData(slot,tb);
00199 if (hd==0) continue;
00200 for (int chan=1; chan<=24; chan++) {
00201 ChannelPattern* cp=hd->getPattern(chan);
00202 char hname[128];
00203 sprintf(hname,"Exact fC Cr%d,%d%s-%d",crate,slot,
00204 ((tb==1)?("t"):("b")),chan);
00205 TH1* hp=new TH1F(hname,hname,ChannelPattern::SAMPLES,-0.5,ChannelPattern::SAMPLES-0.5);
00206 hp->SetDirectory(0);
00207 sprintf(hname,"Quantized fC Cr%d,%d%s-%d",crate,slot,
00208 ((tb==1)?("t"):("b")),chan);
00209 TH1* hq=new TH1F(hname,hname,ChannelPattern::SAMPLES,-0.5,ChannelPattern::SAMPLES-0.5);
00210 hp->SetDirectory(0);
00211 sprintf(hname,"Encoded fC Cr%d,%d%s-%d",crate,slot,
00212 ((tb==1)?("t"):("b")),chan);
00213 TH1* ha=new TH1F(hname,hname,ChannelPattern::SAMPLES,-0.5,ChannelPattern::SAMPLES-0.5);
00214 ha->SetDirectory(0);
00215 for (int i=0; i<ChannelPattern::SAMPLES; i++) {
00216 hp->Fill(i*1.0,(*cp)[i]);
00217 hq->Fill(i*1.0,cp->getQuantized(i));
00218 ha->Fill(i*1.0,cp->getCoded(i));
00219 }
00220
00221
00222 of.cd("adc"); ha->Write();
00223 delete hp;
00224 delete hq;
00225 delete ha;
00226 }
00227 }
00228 }
00229 }
00230 of.Close();
00231 }