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  bool first = true;
52  for (int region = 0; region < numRegions_; region++) {
53  const int offset = region * numChannel;
54  // start with emp 6 frame gap
55  ss << infraGap(nFrame, numChannel + voidChannel);
56  for (int frame = 0; frame < numFrames_; frame++) {
57  // write one frame for all channel
58  ss << this->frame(nFrame);
59  for (int channel = 0; channel < numChannel; channel++) {
60  const vector<Frame>& bvs = bits[offset + channel];
61  ss << (frame < (int)bvs.size() ? hex(bvs[frame], first) : hex(Frame(), first));
62  }
63  for (int channel = 0; channel < voidChannel; channel++)
64  ss << " 0000 " << string(TTBV::S_ / 4, '0');
65  ss << endl;
66  first = false;
67  }
68  }
69  }
70 
71  // plays stringstream through modelsim
72  void Demonstrator::sim(const stringstream& ss) const {
73  // write ss to disk
74  fstream fs;
75  fs.open(dirIn_.c_str(), fstream::out);
76  fs << ss.rdbuf();
77  fs.close();
78  // run modelsim
79  stringstream cmd;
80  cmd << "cd " << dirIPBB_ << " && ./run_sim -quiet -c work.top -do 'run " << runTime_
81  << "us' -do 'quit' &> /dev/null";
82  system(cmd.str().c_str());
83  }
84 
85  // compares stringstream with modelsim output
86  bool Demonstrator::compare(stringstream& ss) const {
87  // write ss to disk
88  fstream fs;
89  fs.open(dirPre_.c_str(), fstream::out);
90  fs << ss.rdbuf();
91  fs.close();
92  // use linux diff on disk
93  const string c = "diff " + dirPre_ + " " + dirOut_ + " &> " + dirDiff_;
94  system(c.c_str());
95  ss.str("");
96  ss.clear();
97  // read diff output
98  fs.open(dirDiff_.c_str(), fstream::in);
99  ss << fs.rdbuf();
100  fs.close();
101  // count lines, 4 are expected
102  int n(0);
103  string token;
104  while (getline(ss, token))
105  n++;
106  return n == 4;
107  }
108 
109  // creates emp file header
110  string Demonstrator::header(int numLinks) const {
111  stringstream ss;
112  // file header
113  ss << "Board CMSSW" << endl
114  << "Metadata: (strobe,) start of orbit, start of packet, end of packet, valid" << endl
115  << 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 << " 0000 " << 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, bool first) const {
145  stringstream ss;
146  ss << (first ? " 1001 " : " 0001 ") << 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
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:72
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:86
HLT enums.
list cmd
Definition: mps_setup.py:244
Definition: output.py:1
static constexpr int S_
Definition: TTBV.h:22
std::string frame(int &nFrame) const
std::string hex(const tt::Frame &bv, bool first=false) const