CMS 3D CMS Logo

HtrXmlPatternTool.cc
Go to the documentation of this file.
1 #include "HtrXmlPatternTool.h"
3 #include "HtrXmlPatternSet.h"
4 #include "HtrXmlPatternWriter.h"
5 #include "TFile.h"
6 #include "TH1.h"
7 #include <filesystem>
8 #include <fstream>
9 #include <iostream>
10 
12  m_params = params;
13 
14  //slotsOn array index corresponds to physical number (e.g., slotsOn[1] for slot 1)
15  int slotsOn[ChannelPattern::NUM_SLOTS];
16  int cratesOn[ChannelPattern::NUM_CRATES];
17 
18  //turn off all slots
19  for (int i = 0; i < ChannelPattern::NUM_SLOTS; i++)
20  slotsOn[i] = 0;
21  //turn on slots 2,3,4,5,6,7,8
22  for (int i = 2; i < 9; i++)
23  slotsOn[i] = 1;
24  //turn on slots 13,14,15,16,17,18
25  for (int i = 13; i < 19; i++)
26  slotsOn[i] = 1;
27 
28  //turn on all crates
29  for (int i = 0; i < ChannelPattern::NUM_CRATES; i++)
30  cratesOn[i] = 1;
31  //turn off two unused crates
32  cratesOn[8] = 0;
33  cratesOn[16] = 0;
34 
35  m_patternSet = new HtrXmlPatternSet(cratesOn, slotsOn);
37 }
38 
40 
43  HalfHtrData* hd = nullptr;
44  ChannelPattern* cp = nullptr;
45 
46  if (cd) {
47  hd = cd->getHalfHtrData(HEID.htrSlot(), HEID.htrTopBottom());
48  if (hd) {
49  hd->setSpigot(HEID.spigot());
50  hd->setDCC(HEID.dccid());
51 
52  cp = hd->getPattern(HEID.htrChanId());
53  if (cp)
54  cp->Fill(m_params, data);
55  else if (m_params->m_show_errors)
56  std::cerr << "Bad (crate,slot,channel): (" << HEID.readoutVMECrateId() << "," << HEID.htrSlot() << ","
57  << HEID.htrChanId() << ")" << std::endl;
58  } else if (m_params->m_show_errors)
59  std::cerr << "Bad (crate,slot): (" << HEID.readoutVMECrateId() << "," << HEID.htrSlot() << ")" << std::endl;
60  } else if (m_params->m_show_errors)
61  std::cerr << "Bad (crate): (" << HEID.readoutVMECrateId() << ")" << std::endl;
62 }
63 
66  HalfHtrData* hd = nullptr;
67  ChannelPattern* cp = nullptr;
68 
69  if (cd) {
70  hd = cd->getHalfHtrData(HEID.htrSlot(), HEID.htrTopBottom());
71  if (hd) {
72  hd->setSpigot(HEID.spigot());
73  hd->setDCC(HEID.dccid());
74 
75  cp = hd->getPattern(HEID.htrChanId());
76  if (cp)
77  cp->Fill(m_params, data);
78  else if (m_params->m_show_errors)
79  std::cerr << "Bad (crate,slot,channel): (" << HEID.readoutVMECrateId() << "," << HEID.htrSlot() << ","
80  << HEID.htrChanId() << ")" << std::endl;
81  } else if (m_params->m_show_errors)
82  std::cerr << "Bad (crate,slot): (" << HEID.readoutVMECrateId() << "," << HEID.htrSlot() << ")" << std::endl;
83  } else if (m_params->m_show_errors)
84  std::cerr << "Bad (crate): (" << HEID.readoutVMECrateId() << ")" << std::endl;
85 }
86 
89  HalfHtrData* hd = nullptr;
90  ChannelPattern* cp = nullptr;
91 
92  if (cd) {
93  hd = cd->getHalfHtrData(HEID.htrSlot(), HEID.htrTopBottom());
94  if (hd) {
95  hd->setSpigot(HEID.spigot());
96  hd->setDCC(HEID.dccid());
97 
98  cp = hd->getPattern(HEID.htrChanId());
99  if (cp)
100  cp->Fill(m_params, data);
101  else if (m_params->m_show_errors)
102  std::cerr << "Bad (crate,slot,channel): (" << HEID.readoutVMECrateId() << "," << HEID.htrSlot() << ","
103  << HEID.htrChanId() << ")" << std::endl;
104  } else if (m_params->m_show_errors)
105  std::cerr << "Bad (crate,slot): (" << HEID.readoutVMECrateId() << "," << HEID.htrSlot() << ")" << std::endl;
106  } else if (m_params->m_show_errors)
107  std::cerr << "Bad (crate): (" << HEID.readoutVMECrateId() << ")" << std::endl;
108 }
109 
110 void HtrXmlPatternTool::prepareDirs() { std::filesystem::create_directory(m_params->m_output_directory); }
111 
113  std::cout << "Writing XML..." << std::endl;
114  std::ofstream* of = nullptr;
115 
116  if (m_params->m_XML_file_mode == 1) {
118  of = new std::ofstream(name.c_str(), std::ios_base::out | std::ios_base::trunc);
119  if (!of->good()) {
120  std::cerr << "XML output file " << name << " is bad." << std::endl;
121  return;
122  }
123  (*of) << "<?xml version='1.0' encoding='UTF-8'?>" << std::endl;
124  (*of) << "<CFGBrickSet name='" << m_params->m_file_tag << "'>" << std::endl;
125  }
126 
127  for (int crate = 0; crate < ChannelPattern::NUM_CRATES; crate++) {
128  CrateData* cd = m_patternSet->getCrate(crate);
129  if (cd == nullptr)
130  continue;
131 
132  if (m_params->m_XML_file_mode == 2) {
134  char cr_name[256];
135  snprintf(cr_name, 256, "_crate_%d.xml", crate);
136  name += cr_name;
137  of = new std::ofstream(name.c_str(), std::ios_base::out | std::ios_base::trunc);
138  if (!of->good()) {
139  std::cerr << "XML output file " << name << " is bad." << std::endl;
140  delete of;
141  return;
142  }
143  (*of) << "<?xml version='1.0' encoding='UTF-8'?>" << std::endl;
144  (*of) << "<CFGBrickSet name='" << m_params->m_file_tag << "'>" << std::endl;
145  }
146 
147  for (int slot = 0; slot < ChannelPattern::NUM_SLOTS; slot++) {
148  for (int tb = 0; tb <= 1; tb++) {
149  HalfHtrData* hd = cd->getHalfHtrData(slot, tb);
150  if (hd == nullptr)
151  continue;
152  for (int fiber = 1; fiber <= 8; fiber++) {
153  if (m_params->m_XML_file_mode == 3) {
155  char cr_name[256];
156  snprintf(cr_name, 256, "_crate_%d_slot_%d_tb_%d_fiber_%d.xml", crate, slot, tb, fiber);
157  name += cr_name;
158  of = new std::ofstream(name.c_str(), std::ios_base::out | std::ios_base::trunc);
159  if (!of->good()) {
160  std::cerr << "XML output file " << name << " is bad." << std::endl;
161  delete of;
162  return;
163  }
164  (*of) << "<?xml version='1.0' encoding='UTF-8'?>" << std::endl;
165  }
166  m_xmlWriter.writePattern(hd, fiber, *of, 1);
167  if (m_params->m_XML_file_mode == 3) {
168  of->close();
169  delete of;
170  of = nullptr;
171  }
172 
173  } //end fiber loop
174  } // end tb loop
175  } //end slot loop
176 
177  if (m_params->m_XML_file_mode == 2) {
178  (*of) << "</CFGBrickSet>" << std::endl;
179  of->close();
180  delete of;
181  of = nullptr;
182  }
183 
184  } //end crate loop
185 
186  if (m_params->m_XML_file_mode == 1) {
187  (*of) << "</CFGBrickSet>" << std::endl;
188  of->close();
189  delete of;
190  of = nullptr;
191  }
192 }
193 
195  std::cout << "Writing root file..." << std::endl;
197  TFile of(name.c_str(), "RECREATE");
198  if (of.IsZombie()) {
199  std::cerr << "root output file " << name << " is bad." << std::endl;
200  return;
201  }
202 
203  of.mkdir("adc");
204 
205  for (int crate = 0; crate < ChannelPattern::NUM_CRATES; crate++) {
206  CrateData* cd = m_patternSet->getCrate(crate);
207  if (cd == nullptr)
208  continue;
209  for (int slot = 0; slot < ChannelPattern::NUM_SLOTS; slot++) {
210  for (int tb = 0; tb <= 1; tb++) {
211  HalfHtrData* hd = cd->getHalfHtrData(slot, tb);
212  if (hd == nullptr)
213  continue;
214  for (int chan = 1; chan <= 24; chan++) {
216  char hname[128];
217  sprintf(hname, "Exact fC Cr%d,%d%s-%d", crate, slot, ((tb == 1) ? ("t") : ("b")), chan);
218  TH1* hp = new TH1F(hname, hname, ChannelPattern::SAMPLES, -0.5, ChannelPattern::SAMPLES - 0.5);
219  hp->SetDirectory(nullptr);
220  sprintf(hname, "Quantized fC Cr%d,%d%s-%d", crate, slot, ((tb == 1) ? ("t") : ("b")), chan);
221  TH1* hq = new TH1F(hname, hname, ChannelPattern::SAMPLES, -0.5, ChannelPattern::SAMPLES - 0.5);
222  hp->SetDirectory(nullptr);
223  sprintf(hname, "Encoded fC Cr%d,%d%s-%d", crate, slot, ((tb == 1) ? ("t") : ("b")), chan);
224  TH1* ha = new TH1F(hname, hname, ChannelPattern::SAMPLES, -0.5, ChannelPattern::SAMPLES - 0.5);
225  ha->SetDirectory(nullptr);
226  for (int i = 0; i < ChannelPattern::SAMPLES; i++) {
227  hp->Fill(i * 1.0, (*cp)[i]);
228  hq->Fill(i * 1.0, cp->getQuantized(i));
229  ha->Fill(i * 1.0, cp->getCoded(i));
230  }
231  //of.cd("perfect"); hp->Write();
232  //of.cd("quantized"); hq->Write();
233  of.cd("adc");
234  ha->Write();
235  delete hp;
236  delete hq;
237  delete ha;
238  }
239  }
240  }
241  }
242  of.Close();
243 }
constexpr int readoutVMECrateId() const
get the readout VME crate number
std::vector< T >::const_iterator const_iterator
constexpr int htrSlot() const
get the htr slot
void setTagName(std::string tn)
HtrXmlPatternToolParameters * m_params
static const int NUM_CRATES
HtrXmlPatternWriter m_xmlWriter
static const int SAMPLES
HtrXmlPatternTool(HtrXmlPatternToolParameters *m_params)
ChannelPattern * getPattern(int chan)
static const int NUM_SLOTS
HtrXmlPatternSet * m_patternSet
void Fill(const HcalElectronicsId HEID, HBHEDigiCollection::const_iterator data)
constexpr int dccid() const
get the (Hcal local) DCC id for VME, crate number for uTCA
CrateData * getCrate(int crate)
chan
lumi = TPaveText(lowX+0.38, lowY+0.061, lowX+0.45, lowY+0.161, "NDC") lumi.SetBorderSize( 0 ) lumi...
constexpr int htrChanId() const
get the HTR channel id (1-24)
void setSpigot(int spigot)
constexpr int htrTopBottom() const
get the htr top/bottom (1=top/0=bottom), valid for VME
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:80
constexpr int spigot() const
get the spigot (input number on DCC, AMC card number for uTCA)
void writePattern(HalfHtrData *spigotData, int fiber, std::ostream &os, int level=0)
void setDCC(int dcc)
Readout chain identification for Hcal.