ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Fun4AllEventOutStream.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Fun4AllEventOutStream.cc
2 
3 #include <fun4all/Fun4AllBase.h> // for Fun4AllBase
4 
5 #include <phool/phool.h>
6 
7 #include <Event/A_Event.h>
8 #include <Event/Event.h>
9 #include <Event/oEvent.h>
10 #include <Event/packet.h>
11 #include <Event/phenixTypes.h> // for PHDWORD
12 
13 #include <algorithm> // for copy, copy_backward, max
14 #include <cstdlib> // for exit
15 #include <exception> // for exception
16 #include <iostream> // for operator<<, basic_ost...
17 #include <queue>
18 #include <utility> // for swap, pair
19 
20 using namespace std;
21 
23  : Fun4AllBase(name)
24  , evtbuf(nullptr)
25  , m_MyManager(nullptr)
26  , evtbuf_size(0)
27  , add_or_remove(0)
28  , plist(nullptr)
29  , max_npackets(1000)
30  , // there shouldn't be more than this number of packets in a single event
31  npackets(0)
32  , default_addall(0)
33 {
34 }
35 
37 {
38  delete[] evtbuf;
39  delete[] plist;
40  return;
41 }
42 
43 int Fun4AllEventOutStream::resize_evtbuf(const unsigned int newsize)
44 {
45  delete[] evtbuf;
46  evtbuf_size = newsize;
47  evtbuf = new PHDWORD[evtbuf_size];
48  for (unsigned int i = 0; i < evtbuf_size; i++)
49  {
50  evtbuf[i] = 0;
51  }
52  return 0;
53 }
54 
56 {
57  int iret;
58  if (!add_or_remove)
59  {
60  iret = WriteEventOut(evt);
61  return iret;
62  }
63  std::queue<int> savepacket;
64  unsigned int size = 0;
65  if (!plist)
66  {
67  plist = new Packet *[max_npackets];
68  }
70  {
71  for (int i = 0; i < npackets; i++)
72  {
73  delete plist[i];
74  }
75  delete[] plist;
76  // cout << "max_npackets " << max_npackets << " too small, take times 2" << endl;
77  max_npackets *= 2;
78  plist = new Packet *[max_npackets];
79  }
80  std::map<int, boost::numeric::interval<int> >::const_iterator dropiter;
81  int dropIt;
82  for (int i = 0; i < npackets; i++)
83  {
84  int packetid = plist[i]->getIdentifier();
85  if (default_addall)
86  {
87  dropIt = 0;
88  for (dropiter = droppktrange.begin(); dropiter != droppktrange.end(); ++dropiter)
89  {
90  if (packetid < dropiter->first)
91  {
92  // abort loop if packetid is smaller than first packet in range
93  break;
94  }
95  if (boost::numeric::in(packetid, dropiter->second))
96  {
97  dropIt = 1;
98  break;
99  }
100  }
101  }
102  else
103  {
104  dropIt = 1;
105  }
106  for (dropiter = addpktrange.begin(); dropiter != addpktrange.end(); ++dropiter)
107  {
108  if (packetid < dropiter->first)
109  {
110  // abort loop if packetid is smaller than first packet in range
111  break;
112  }
113  if (boost::numeric::in(packetid, dropiter->second))
114  {
115  dropIt = 0;
116  break;
117  }
118  }
119  if (!dropIt)
120  {
121  savepacket.push(i);
122  size += plist[i]->getLength() + 4;
123  }
124  else
125  {
126  if (Verbosity() > 0)
127  {
128  cout << "Fun4AllEventOutStream: dropping packet " << i
129  << " in list with id " << plist[i]->getIdentifier() << endl;
130  }
131  }
132  }
133  size += 100; // add some size for the event header
134  if (size > evtbuf_size)
135  {
136  // Add 10000 so we do this resize only a few times
137  resize_evtbuf(size + 10000);
138  }
139 
140  oEvent new_event(evtbuf, size, evt->getRunNumber(), evt->getEvtType(), evt->getEvtSequence());
141  while (!savepacket.empty())
142  {
143  int index = savepacket.front();
144  new_event.addPacket(plist[index]);
145  savepacket.pop();
146  }
147 
148  Event *newE = new A_Event(evtbuf);
149  iret = WriteEventOut(newE);
150  delete newE;
151  for (int i = 0; i < npackets; i++)
152  {
153  delete plist[i];
154  }
155  return iret;
156 }
157 
159 {
160  AddPacketRange(ipkt, ipkt);
161  return 0;
162 }
163 
165 {
166  DropPacketRange(ipkt, ipkt);
167  return 0;
168 }
169 
170 int Fun4AllEventOutStream::AddPacketRange(const int minpacket, const int maxpacket)
171 {
172  add_or_remove = 1;
173  boost::numeric::interval<int> newinterval;
174  try
175  {
176  newinterval.assign(minpacket, maxpacket);
177  }
178  catch (exception &e)
179  {
180  cout << "Exception thrown: " << e.what() << endl;
181  cout << "for interval[" << minpacket << "," << maxpacket << "]" << endl;
182  cout << "exiting" << endl;
183  exit(1);
184  }
185  addpktrange[minpacket] = newinterval;
186  if (!boost::numeric::in(minpacket, newinterval))
187  {
188  cout << PHWHERE << " boost interval does not cover minpacket " << minpacket << endl;
189  cout << "that is seriously wrong, exiting" << endl;
190  exit(1);
191  }
192  if (!boost::numeric::in(maxpacket, newinterval))
193  {
194  cout << PHWHERE << " boost interval does not cover maxpacket " << maxpacket << endl;
195  cout << "that is seriously wrong, exiting" << endl;
196  exit(1);
197  }
198  return 0;
199 }
200 
201 int Fun4AllEventOutStream::DropPacketRange(const int minpacket, const int maxpacket)
202 {
203  add_or_remove = 1;
204  default_addall = 1;
205  boost::numeric::interval<int> newinterval;
206  try
207  {
208  newinterval.assign(minpacket, maxpacket);
209  }
210  catch (exception &e)
211  {
212  cout << "Exception thrown: " << e.what() << endl;
213  cout << "for interval[" << minpacket << "," << maxpacket << "]" << endl;
214  cout << "exiting" << endl;
215  exit(1);
216  }
217  droppktrange[minpacket] = newinterval;
218  if (!boost::numeric::in(minpacket, newinterval))
219  {
220  cout << PHWHERE << " boost interval does not cover minpacket " << minpacket << endl;
221  cout << "that is seriously wrong, exiting" << endl;
222  exit(1);
223  }
224  if (!boost::numeric::in(maxpacket, newinterval))
225  {
226  cout << PHWHERE << " boost interval does not cover maxpacket " << maxpacket << endl;
227  cout << "that is seriously wrong, exiting" << endl;
228  exit(1);
229  }
230  return 0;
231 }