Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PktSizeMon.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PktSizeMon.cc
1 #include "PktSizeMon.h"
2 #include "PktSizeCommon.h"
3 #include "PktSizeDBodbc.h"
4 
5 #include <onlmon/OnlMonServer.h>
6 
7 #include <Event/Event.h>
8 #include <Event/EventTypes.h>
9 #include <Event/msg_profile.h>
10 
11 #include <phool/phool.h>
12 
13 #include <TH1.h>
14 
15 #include <iostream>
16 #include <sstream>
17 
18 const char *histoname = "pktsize_hist";
19 const int NUPDATE = 1000;
20 
22  : OnlMon(name)
23  , nevnts(0)
24  , db(new PktSizeDBodbc(name))
25 {
26  return;
27 }
28 
30 {
31  Reset();
32  delete db;
33  return;
34 }
35 
36 // void
37 // PktSizeMon::Verbosity(const int i)
38 // {
39 // if (db)
40 // {
41 // db->Verbosity(i);
42 // }
43 // OnlMon::Verbosity(i);
44 // return ;
45 // }
46 
48 {
50  sizehist = new TH1F(histoname, "dummy", 1, 0, 1);
51  se->registerHisto(this, sizehist);
55  return 0;
56 }
57 
59 {
60  packetsize.clear();
61 
63  TH1 *newhist = new TH1F("pktsize_tmp", "packet size storage facility", 1, 0, 1);
64  se->registerHisto(this->Name(), histoname, newhist, 1);
65  sizehist = newhist;
66  nevnts = 0;
67  return 0;
68 }
69 
71 {
72  // only data events
73  if (e->getEvtType() != DATAEVENT)
74  {
75  return 0;
76  }
77  int nw = e->getPacketList(plist, 10000);
78  if (nw >= 10000)
79  {
80  std::ostringstream errmsg;
81  errmsg << "Packet array too small, need at least " << nw << " entries";
83  se->send_message(this, MSG_SOURCE_DAQMON, MSG_SEV_ERROR, errmsg.str(), 1);
84  nw = 10000;
85  }
86  int packetid;
87  unsigned int size;
88  std::map<unsigned int, unsigned int>::iterator mapiter;
89  for (int i = 0; i < nw; i++)
90  {
91  packetid = plist[i]->getIdentifier();
92  size = (plist[i]->getLength());
94  // std::string dcmgrp = dcmgroups[packetid];
95  // dcmgroupsize[dcmgrp] += size;
96  // mapiter = packetsize.find(packetid);
97  // if (mapiter != packetsize.end())
98  // {
99  // mapiter->second += size;
100  // }
101  // else
102  // {
103  // packetsize[packetid] = size;
104  // }
105  std::cout << "Packet " << packetid << " size: " << size << " sum size: "
106  << packetsize[packetid] << std::endl;
107  delete plist[i];
108  }
109  nevnts++;
110  if (nevnts % NUPDATE == 0)
111  {
112  if (verbosity > 0)
113  {
114  std::cout << "putting map into histos" << std::endl;
115  }
116  putmapinhisto();
117  }
118  return 0;
119 }
120 
121 int PktSizeMon::EndRun(const int runno)
122 {
123  putmapinhisto();
124  UpdateDB(runno);
125  return 0;
126 }
127 
129 {
131  if (packetsize.size() != (unsigned int) sizehist->GetNbinsX())
132  {
133  TH1 *newhist = new TH1F("pktsize_tmp", "packet size storage facility", packetsize.size(), 0, packetsize.size());
134  se->registerHisto(this->Name(), histoname, newhist, 1);
135  newhist->SetName(histoname);
136  sizehist = newhist;
137  }
138  std::map<unsigned int, unsigned int>::const_iterator mapiter;
139  int nbin = 1;
140  double aversize;
141  sizehist->SetBinContent(0, nevnts); // fill number of evts into 0th bin
142  for (mapiter = packetsize.begin(); mapiter != packetsize.end(); ++mapiter)
143  {
144  aversize = (double) (mapiter->second) / (double) (nevnts);
145  sizehist->SetBinContent(nbin, aversize);
146  sizehist->SetBinError(nbin, mapiter->first);
147  nbin++;
148  }
149  return 0;
150 }
151 
153 {
154  if (what == "ALL")
155  {
156  std::map<unsigned int, unsigned int>::const_iterator mapiter;
157  for (mapiter = packetsize.begin(); mapiter != packetsize.end(); ++mapiter)
158  {
159  std::cout << "Packet " << mapiter->first
160  << ", SumBytes " << mapiter->second
161  << ", Average " << (float) (mapiter->second) / (float) (nevnts)
162  << std::endl;
163  if (dcmgroups.find(mapiter->first) == dcmgroups.end())
164  {
165  std::cout << "could not find dcm group for packet " << mapiter->first << std::endl;
166  }
167  else
168  {
169  std::string dcmgrp = dcmgroups[mapiter->first];
170  dcmgroupsize[dcmgrp] += mapiter->second;
171  std::string fibergrp = fibergroups[mapiter->first];
172  fibergroupsize[fibergrp] += mapiter->second;
173  }
174  }
175 
176  std::map<std::string, unsigned int>::const_iterator iter;
177  for (iter = dcmgroupsize.begin(); iter != dcmgroupsize.end(); ++iter)
178  {
179  std::cout << "DCM group " << iter->first
180  << ", SumBytes " << iter->second
181  << ", Average " << (float) (iter->second) / (float) (nevnts)
182  << std::endl;
183  }
184 
185  for (iter = fibergroupsize.begin(); iter != fibergroupsize.end(); ++iter)
186  {
187  std::cout << "Fiber group " << iter->first
188  << ", SumBytes " << iter->second
189  << ", Average " << (float) (iter->second) / (float) (nevnts)
190  << std::endl;
191  }
192  }
193  if (what == "GRANULES")
194  {
195  std::map<std::string, std::pair<unsigned int, unsigned int> >::const_iterator mapiter;
196  for (mapiter = granulepacketlimits.begin(); mapiter != granulepacketlimits.end(); ++mapiter)
197  {
198  std::cout << "Granule " << mapiter->first
199  << ", Min Packet " << mapiter->second.first
200  << ", Max Packet " << mapiter->second.second
201  << std::endl;
202  }
203  }
204  if (what == "SORT")
205  {
206  std::multimap<double, int> sortlist;
207  std::map<unsigned int, unsigned int>::const_iterator mapiter;
208  for (mapiter = packetsize.begin(); mapiter != packetsize.end(); ++mapiter)
209  {
210  double aversize = (double) (mapiter->second) / (double) (nevnts);
211  sortlist.insert(std::pair<double, int>(aversize, mapiter->first));
212  }
213  std::multimap<double, int>::const_iterator mmapiter;
214  for (mmapiter = sortlist.begin(); mmapiter != sortlist.end(); ++mmapiter)
215  {
216  std::cout << "Packet " << mmapiter->second
217  << " Size: " << mmapiter->first
218  << " Bytes "
219  << std::endl;
220  }
221  }
222  if (what == "DCMGROUP")
223  {
224  std::map<std::string, std::set<unsigned int> > dcms;
225  std::map<unsigned int, std::string>::const_iterator iter;
226  for (iter = dcmgroups.begin(); iter != dcmgroups.end(); ++iter)
227  {
228  std::map<std::string, std::set<unsigned int> >::iterator iter2 = dcms.find(iter->second);
229  if (iter2 == dcms.end())
230  {
231  std::set<unsigned int> newset;
232  newset.insert(iter->first);
233  dcms[iter->second] = newset;
234  }
235  else
236  {
237  (iter2->second).insert(iter->first);
238  }
239  }
240  std::map<std::string, std::set<unsigned int> >::const_iterator iter3;
241  std::set<unsigned int>::const_iterator iter4;
242  for (iter3 = dcms.begin(); iter3 != dcms.end(); ++iter3)
243  {
244  std::cout << "dcm group " << iter3->first << " packets: " << std::endl;
245  for (iter4 = (iter3->second).begin(); iter4 != (iter3->second).end(); ++iter4)
246  {
247  std::cout << *iter4 << " ";
248  }
249  std::cout << std::endl;
250  }
251  }
252  return;
253 }
254 
255 int PktSizeMon::UpdateDB(const int runno)
256 {
257  std::map<std::string, std::pair<unsigned int, unsigned int> >::const_iterator graniter;
259  unsigned int lolim, hilim;
260  std::map<unsigned int, unsigned int> granpackets;
261  std::map<unsigned int, unsigned int>::iterator piter0, piter1, piter2;
262  for (graniter = granulepacketlimits.begin(); graniter != granulepacketlimits.end(); ++graniter)
263  {
264  name = graniter->first;
265  lolim = graniter->second.first;
266  hilim = graniter->second.second;
267  piter1 = packetsize.lower_bound(lolim);
268  if (piter1->first < hilim)
269  {
270  if (verbosity > 0)
271  {
272  piter2 = packetsize.upper_bound(hilim);
273  --piter2;
274  std::cout << "Name: " << name << " first packet: " << piter1->first
275  << " last packet: " << piter2->first << std::endl;
276  }
277  }
278  else
279  {
280  if (verbosity > 0)
281  {
282  std::cout << "Name: " << name << " No packet found" << std::endl;
283  }
284  continue;
285  }
286  for (piter0 = piter1; piter0 != packetsize.upper_bound(hilim); ++piter0)
287  {
288  granpackets[piter0->first] = piter0->second;
289  }
290  db->AddRow(name, runno, nevnts, granpackets);
291  packetsize.erase(packetsize.lower_bound(lolim), packetsize.upper_bound(hilim));
292  granpackets.clear();
293  }
294  if (packetsize.size() > 0)
295  {
296  std::cout << "Non assigned packets found" << std::endl;
297  for (piter0 = packetsize.begin(); piter0 != packetsize.end(); ++piter0)
298  {
299  std::cout << "Packet ID: " << piter0->first << std::endl;
300  }
301  std::ostringstream errmsg;
302  errmsg << "PktSizeMon::UpdateDB() unassigned packets found:";
303  for (piter0 = packetsize.begin(); piter0 != packetsize.end(); ++piter0)
304  {
305  errmsg << " " << piter0->first;
306  }
308  se->send_message(this, MSG_SOURCE_DAQMON, MSG_SEV_ERROR, errmsg.str(), 2);
309  db->AddRow("nogran", runno, nevnts, packetsize);
310  }
311  packetsize.clear();
312  return 0;
313 }