Analysis Software
Documentation for sPHENIX simulation 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 
21  : Fun4AllBase(name)
22 {
23 }
24 
26 {
27  delete[] evtbuf;
28  delete[] plist;
29  return;
30 }
31 
32 int Fun4AllEventOutStream::resize_evtbuf(const unsigned int newsize)
33 {
34  delete[] evtbuf;
35  evtbuf_size = newsize;
36  evtbuf = new PHDWORD[evtbuf_size];
37  for (unsigned int i = 0; i < evtbuf_size; i++)
38  {
39  evtbuf[i] = 0;
40  }
41  return 0;
42 }
43 
45 {
46  int iret;
47  if (!add_or_remove)
48  {
49  iret = WriteEventOut(evt);
50  return iret;
51  }
52  std::queue<int> savepacket;
53  unsigned int size = 0;
54  if (!plist)
55  {
56  plist = new Packet *[max_npackets];
57  }
59  {
60  for (int i = 0; i < npackets; i++)
61  {
62  delete plist[i];
63  }
64  delete[] plist;
65  // std::cout << "max_npackets " << max_npackets << " too small, take times 2" << std::endl;
66  max_npackets *= 2;
67  plist = new Packet *[max_npackets];
68  }
69  std::map<int, boost::numeric::interval<int> >::const_iterator dropiter;
70  int dropIt;
71  for (int i = 0; i < npackets; i++)
72  {
73  int packetid = plist[i]->getIdentifier();
74  if (default_addall)
75  {
76  dropIt = 0;
77  for (dropiter = droppktrange.begin(); dropiter != droppktrange.end(); ++dropiter)
78  {
79  if (packetid < dropiter->first)
80  {
81  // abort loop if packetid is smaller than first packet in range
82  break;
83  }
84  if (boost::numeric::in(packetid, dropiter->second))
85  {
86  dropIt = 1;
87  break;
88  }
89  }
90  }
91  else
92  {
93  dropIt = 1;
94  }
95  for (dropiter = addpktrange.begin(); dropiter != addpktrange.end(); ++dropiter)
96  {
97  if (packetid < dropiter->first)
98  {
99  // abort loop if packetid is smaller than first packet in range
100  break;
101  }
102  if (boost::numeric::in(packetid, dropiter->second))
103  {
104  dropIt = 0;
105  break;
106  }
107  }
108  if (!dropIt)
109  {
110  savepacket.push(i);
111  size += plist[i]->getLength() + 4;
112  }
113  else
114  {
115  if (Verbosity() > 0)
116  {
117  std::cout << "Fun4AllEventOutStream: dropping packet " << i
118  << " in list with id " << plist[i]->getIdentifier() << std::endl;
119  }
120  }
121  }
122  size += 100; // add some size for the event header
123  if (size > evtbuf_size)
124  {
125  // Add 10000 so we do this resize only a few times
126  resize_evtbuf(size + 10000);
127  }
128 
129  oEvent new_event(evtbuf, size, evt->getRunNumber(), evt->getEvtType(), evt->getEvtSequence());
130  while (!savepacket.empty())
131  {
132  int index = savepacket.front();
133  new_event.addPacket(plist[index]);
134  savepacket.pop();
135  }
136 
137  Event *newE = new A_Event(evtbuf);
138  iret = WriteEventOut(newE);
139  delete newE;
140  for (int i = 0; i < npackets; i++)
141  {
142  delete plist[i];
143  }
144  return iret;
145 }
146 
148 {
149  AddPacketRange(ipkt, ipkt);
150  return 0;
151 }
152 
154 {
155  DropPacketRange(ipkt, ipkt);
156  return 0;
157 }
158 
159 int Fun4AllEventOutStream::AddPacketRange(const int minpacket, const int maxpacket)
160 {
161  add_or_remove = 1;
162  boost::numeric::interval<int> newinterval;
163  try
164  {
165  newinterval.assign(minpacket, maxpacket);
166  }
167  catch (std::exception &e)
168  {
169  std::cout << "Exception thrown: " << e.what() << std::endl;
170  std::cout << "for interval[" << minpacket << "," << maxpacket << "]" << std::endl;
171  std::cout << "exiting" << std::endl;
172  exit(1);
173  }
174  addpktrange[minpacket] = newinterval;
175  if (!boost::numeric::in(minpacket, newinterval))
176  {
177  std::cout << PHWHERE << " boost interval does not cover minpacket " << minpacket << std::endl;
178  std::cout << "that is seriously wrong, exiting" << std::endl;
179  exit(1);
180  }
181  if (!boost::numeric::in(maxpacket, newinterval))
182  {
183  std::cout << PHWHERE << " boost interval does not cover maxpacket " << maxpacket << std::endl;
184  std::cout << "that is seriously wrong, exiting" << std::endl;
185  exit(1);
186  }
187  return 0;
188 }
189 
190 int Fun4AllEventOutStream::DropPacketRange(const int minpacket, const int maxpacket)
191 {
192  add_or_remove = 1;
193  default_addall = 1;
194  boost::numeric::interval<int> newinterval;
195  try
196  {
197  newinterval.assign(minpacket, maxpacket);
198  }
199  catch (std::exception &e)
200  {
201  std::cout << "Exception thrown: " << e.what() << std::endl;
202  std::cout << "for interval[" << minpacket << "," << maxpacket << "]" << std::endl;
203  std::cout << "exiting" << std::endl;
204  exit(1);
205  }
206  droppktrange[minpacket] = newinterval;
207  if (!boost::numeric::in(minpacket, newinterval))
208  {
209  std::cout << PHWHERE << " boost interval does not cover minpacket " << minpacket << std::endl;
210  std::cout << "that is seriously wrong, exiting" << std::endl;
211  exit(1);
212  }
213  if (!boost::numeric::in(maxpacket, newinterval))
214  {
215  std::cout << PHWHERE << " boost interval does not cover maxpacket " << maxpacket << std::endl;
216  std::cout << "that is seriously wrong, exiting" << std::endl;
217  exit(1);
218  }
219  return 0;
220 }