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