CMS 3D CMS Logo

Demonstrator.cc
Go to the documentation of this file.
2 
3 #include <vector>
4 #include <string>
5 #include <sstream>
6 #include <fstream>
7 
8 using namespace std;
9 using namespace edm;
10 using namespace tt;
11 
12 namespace trackerTFP {
13 
14  Demonstrator::Demonstrator(const ParameterSet& iConfig, const Setup* setup)
15  : dirIPBB_(iConfig.getParameter<string>("DirIPBB")),
16  runTime_(iConfig.getParameter<double>("RunTime")),
17  dirIn_(dirIPBB_ + "in.txt"),
18  dirOut_(dirIPBB_ + "out.txt"),
19  dirPre_(dirIPBB_ + "pre.txt"),
20  dirDiff_(dirIPBB_ + "diff.txt"),
21  numFrames_(setup->numFramesIO()),
22  numFramesInfra_(setup->numFramesInfra()),
23  numRegions_(setup->numRegions()) {}
24 
25  // plays input through modelsim and compares result with output
26  bool Demonstrator::analyze(const vector<vector<Frame>>& input, const vector<vector<Frame>>& output) const {
27  stringstream ss;
28  // converts input into stringstream
29  convert(input, ss);
30  // play input through modelsim
31  sim(ss);
32  // converts output into stringstream
33  convert(output, ss);
34  // compares output with modelsim output
35  return compare(ss);
36  }
37 
38  // converts streams of bv into stringstream
39  void Demonstrator::convert(const vector<vector<Frame>>& bits, stringstream& ss) const {
40  // reset ss
41  ss.str("");
42  ss.clear();
43  // number of tranceiver in a quad
44  static constexpr int quad = 4;
45  const int numChannel = bits.size() / numRegions_;
46  const int voidChannel = numChannel % quad == 0 ? 0 : quad - numChannel % quad;
47  // start with header
48  ss << header(numChannel + voidChannel);
49  int nFrame(0);
50  // create one packet per region
51  for (int region = 0; region < numRegions_; region++) {
52  const int offset = region * numChannel;
53  // start with emp 6 frame gap
54  ss << infraGap(nFrame, numChannel + voidChannel);
55  for (int frame = 0; frame < numFrames_; frame++) {
56  // write one frame for all channel
57  ss << this->frame(nFrame);
58  for (int channel = 0; channel < numChannel; channel++) {
59  const vector<Frame>& bvs = bits[offset + channel];
60  ss << (frame < (int)bvs.size() ? hex(bvs[frame]) : hex(Frame()));
61  }
62  for (int channel = 0; channel < voidChannel; channel++)
63  ss << " 0v" << string(TTBV::S_ / 4, '0');
64  ss << endl;
65  }
66  }
67  }
68 
69  // plays stringstream through modelsim
70  void Demonstrator::sim(const stringstream& ss) const {
71  // write ss to disk
72  fstream fs;
73  fs.open(dirIn_.c_str(), fstream::out);
74  fs << ss.rdbuf();
75  fs.close();
76  // run modelsim
77  stringstream cmd;
78  cmd << "cd " << dirIPBB_ << " && ./run_sim -quiet -c work.top -do 'run " << runTime_
79  << "us' -do 'quit' &> /dev/null";
80  system(cmd.str().c_str());
81  }
82 
83  // compares stringstream with modelsim output
84  bool Demonstrator::compare(stringstream& ss) const {
85  // write ss to disk
86  fstream fs;
87  fs.open(dirPre_.c_str(), fstream::out);
88  fs << ss.rdbuf();
89  fs.close();
90  // use linux diff on disk
91  const string c = "diff " + dirPre_ + " " + dirOut_ + " &> " + dirDiff_;
92  system(c.c_str());
93  ss.str("");
94  ss.clear();
95  // read diff output
96  fs.open(dirDiff_.c_str(), fstream::in);
97  ss << fs.rdbuf();
98  fs.close();
99  // count lines, 4 are expected
100  int n(0);
101  string token;
102  while (getline(ss, token))
103  n++;
104  return n == 4;
105  }
106 
107  // creates emp file header
108  string Demonstrator::header(int numLinks) const {
109  stringstream ss;
110  // file header
111  ss << "Board CMSSW" << endl << " Quad/Chan :";
112  // quad header
113  for (int link = 0; link < numLinks; link++)
114  ss << " q" << setfill('0') << setw(2) << link / 4 << "c" << link % 4 << " ";
115  ss << endl;
116  // link header
117  ss << " Link :";
118  for (int link = 0; link < numLinks; link++)
119  ss << " " << setfill('0') << setw(3) << link << " ";
120  ss << endl;
121  return ss.str();
122  }
123 
124  // creates 6 frame gap between packets
125  string Demonstrator::infraGap(int& nFrame, int numLinks) const {
126  stringstream ss;
127  for (int gap = 0; gap < numFramesInfra_; gap++) {
128  ss << frame(nFrame);
129  for (int link = 0; link < numLinks; link++)
130  ss << " 0v" << string(TTBV::S_ / 4, '0');
131  ss << endl;
132  }
133  return ss.str();
134  }
135 
136  // creates frame number
137  string Demonstrator::frame(int& nFrame) const {
138  stringstream ss;
139  ss << "Frame " << setfill('0') << setw(4) << nFrame++ << " :";
140  return ss.str();
141  }
142 
143  // converts bv into hex
144  string Demonstrator::hex(const Frame& bv) const {
145  stringstream ss;
146  ss << " 1v" << setfill('0') << setw(TTBV::S_ / 4) << std::hex << bv.to_ullong();
147  return ss.str();
148  }
149 
150 } // namespace trackerTFP
std::bitset< TTBV::S_ > Frame
Definition: TTTypes.h:58
void convert(const std::vector< std::vector< tt::Frame >> &bits, std::stringstream &ss) const
Definition: Demonstrator.cc:39
Class to process and provide run-time constants used by Track Trigger emulators.
Definition: Setup.h:44
constexpr uint32_t bits
Definition: gpuClustering.h:23
bool analyze(const std::vector< std::vector< tt::Frame >> &input, const std::vector< std::vector< tt::Frame >> &output) const
Definition: Demonstrator.cc:26
static std::string const input
Definition: EdmProvDump.cc:50
void sim(const std::stringstream &ss) const
Definition: Demonstrator.cc:70
Definition: TTTypes.h:54
std::string header(int numChannel) const
std::string infraGap(int &nFrame, int numChannel) const
bool compare(std::stringstream &ss) const
Definition: Demonstrator.cc:84
std::string hex(const tt::Frame &bv) const
HLT enums.
list cmd
Definition: mps_setup.py:244
static constexpr int S_
Definition: TTBV.h:22
std::string frame(int &nFrame) const