EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Fun4AllSingleDstPileupInputManager.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Fun4AllSingleDstPileupInputManager.cc
1 
8 
10 #include <fun4all/Fun4AllServer.h>
11 
12 #include <ffaobjects/RunHeader.h>
13 
14 #include <frog/FROG.h>
15 
16 #include <phool/PHCompositeNode.h>
17 #include <phool/PHNodeIOManager.h>
18 #include <phool/PHNodeIntegrate.h>
19 #include <phool/PHNodeIterator.h> // for PHNodeIterator
20 #include <phool/PHRandomSeed.h>
21 #include <phool/getClass.h>
22 #include <phool/phool.h> // for PHWHERE, PHReadOnly, PHRunTree
23 
24 #include <TSystem.h>
25 
26 #include <gsl/gsl_randist.h>
27 
28 #include <cassert>
29 #include <iostream> // for operator<<, basic_ostream, endl
30 #include <utility> // for pair
31 
32 //_____________________________________________________________________________
33 Fun4AllSingleDstPileupInputManager::Fun4AllSingleDstPileupInputManager(const std::string &name, const std::string &nodename, const std::string &topnodename)
34  : Fun4AllInputManager(name, nodename, topnodename)
35 {
36  // initialize random generator
37  const uint seed = PHRandomSeed();
38  m_rng.reset( gsl_rng_alloc(gsl_rng_mt19937) );
39  gsl_rng_set( m_rng.get(), seed );
40 }
41 
42 //_____________________________________________________________________________
43 int Fun4AllSingleDstPileupInputManager::fileopen(const std::string &filenam)
44 {
45  /*
46  this is largely copied from fun4all/Fun4AllDstInputManager::fileopen
47  with additional code to handle the background IManager
48  */
49 
50  auto se = Fun4AllServer::instance();
51  if (IsOpen())
52  {
53  std::cout << "Closing currently open file "
54  << FileName()
55  << " and opening " << filenam << std::endl;
56  fileclose();
57  }
58  FileName(filenam);
59  FROG frog;
61  if (Verbosity() > 0)
62  {
63  std::cout << Name() << ": opening file " << m_fullfilename << std::endl;
64  }
65  // sanity check - the IManager must be nullptr when this method is executed
66  // if not something is very very wrong and we must not continue
67  assert( !m_IManager );
68 
69  // first read the runnode if not disabled
70  if (m_ReadRunTTree)
71  {
73  if (m_IManager->isFunctional())
74  {
75  m_runNode = se->getNode(m_RunNode, TopNodeName());
76  m_IManager->read(m_runNode);
77 
78  // get the current run number
79  auto runheader = findNode::getClass<RunHeader>(m_runNode, "RunHeader");
80  if (runheader)
81  {
82  SetRunNumber(runheader->get_RunNumber());
83  }
84  // delete our internal copy of the runnode when opening subsequent files
85  assert( !m_runNodeCopy );
86  m_runNodeCopy.reset(new PHCompositeNode("RUNNODECOPY"));
87  if (!m_runNodeSum)
88  {
89  m_runNodeSum.reset(new PHCompositeNode("RUNNODESUM"));
90  }
91 
92  {
93  // read run node using temporary node iomanager
95  }
96 
97  PHNodeIntegrate integrate;
98  integrate.RunNode(m_runNode);
99  integrate.RunSumNode(m_runNodeSum.get());
100  // run recursively over internal run node copy and integrate objects
101  PHNodeIterator mainIter(m_runNodeCopy.get());
102  mainIter.forEach(integrate);
103  // we do not need to keep the internal copy, keeping it would crate
104  // problems in case a subsequent file does not contain all the
105  // runwise objects from the previous file. Keeping this copy would then
106  // integrate the missing object again with the old copy
107  m_runNodeCopy.reset();
108  }
109  // DLW: move the delete outside the if block to cover the case where isFunctional() fails
110  m_IManager.reset();
111  }
112 
113  // create internal dst node
114  if (!m_dstNodeInternal)
115  {
116  m_dstNodeInternal.reset(new PHCompositeNode("DST_INTERNAL"));
117  }
118 
119  // update dst node from fun4all server
120  m_dstNode = se->getNode(InputNode(), TopNodeName());
121 
122  // open file in both active and background input manager
125  if (m_IManager->isFunctional())
126  {
127  IsOpen(1);
128  m_ievent_thisfile = 0;
129  setBranches(); // set branch selections
130  AddToFileOpened(FileName()); // add file to the list of files which were opened
131  return 0;
132  }
133  else
134  {
135  std::cout << PHWHERE << ": " << Name() << " Could not open file " << FileName() << std::endl;
136  m_IManager.reset();
137  m_IManager_background.reset();
138  return -1;
139  }
140 }
141 
142 //_____________________________________________________________________________
144 {
145 
146  if (!IsOpen())
147  {
148  if (FileListEmpty())
149  {
150  if (Verbosity() > 0) std::cout << Name() << ": No Input file open" << std::endl;
151  return -1;
152  }
153  else
154  {
155  if (OpenNextFile())
156  {
157  std::cout << Name() << ": No Input file from filelist opened" << std::endl;
158  return -1;
159  }
160  }
161  }
162 
163  if (Verbosity() > 3)
164  {
165  std::cout << "Getting Event from " << Name() << std::endl;
166  }
167 
168 readagain:
169 
170  // read main event to dstNode
171  auto dummy = m_IManager->read(m_dstNode);
172  int ncount = 0;
173  while (dummy)
174  {
175  ++ncount;
176  if (nevents > 0 && ncount >= nevents) break;
177  dummy = m_IManager->read(m_dstNode);
178  }
179 
180  if (!dummy)
181  {
182  fileclose();
183  if (!OpenNextFile())
184  goto readagain;
185  else
186  return -1;
187  }
188 
189  m_ievent_total += ncount;
190  m_ievent_thisfile += ncount;
191 
192  // check events consistency
193  if (m_ievent_thisfile != static_cast<int>(m_IManager->getEventNumber()))
194  {
195  std::cout << PHWHERE
196  << " inconsistent event counters between inputmanager and ionode manager: "
197  << " m_ievent_thisfile: " << m_ievent_thisfile
198  << " m_IManager->getEventNumber(): " << m_IManager->getEventNumber()
199  << std::endl;
200  gSystem->Exit(1);
201  }
202 
203  // check if the local SubsysReco discards this event
205  {
206  std::cout << "Fun4AllSingleDstPileupInputManager::run - skipped event " << m_ievent_thisfile - 1 << std::endl;
207  goto readagain;
208  }
209 
210  // load relevant DST nodes to internal pointers
211  if (Verbosity() > 0)
212  {
213  std::cout << "Fun4AllSingleDstPileupInputManager::run - loaded event " << m_ievent_thisfile - 1 << std::endl;
214  }
215 
216  Fun4AllDstPileupMerger merger;
217  merger.load_nodes(m_dstNode);
218 
219  // generate background collisions
220  const double mu = m_collision_rate*m_time_between_crossings*1e-9;
221 
222  const int min_crossing = m_tmin/m_time_between_crossings;
223  const int max_crossing = m_tmax/m_time_between_crossings;
224  int ievent_thisfile = m_ievent_thisfile;
225  int neventsbackground = 0;
226  for( int icrossing = min_crossing; icrossing <= max_crossing; ++icrossing )
227  {
228  const double crossing_time = m_time_between_crossings * icrossing;
229  const int ncollisions = gsl_ran_poisson(m_rng.get(), mu);
230  for (int icollision = 0; icollision < ncollisions; ++icollision)
231  {
232 
233  // try read
234  if(!m_IManager_background->read(m_dstNodeInternal.get(), ievent_thisfile) ) break;
235 
236  // merge
237 
238  if (Verbosity() > 0)
239  {
240  std::cout << "Fun4AllSingleDstPileupInputManager::run - merged background event " << ievent_thisfile << " time: " << crossing_time << std::endl;
241  }
242  merger.copy_background_event(m_dstNodeInternal.get(), crossing_time);
243 
244  ++neventsbackground;
245  ++ievent_thisfile;
246  }
247  }
248 
249  // jump event counter to the last background accepted event
250  if( neventsbackground > 0 ) PushBackEvents( -neventsbackground );
251  return 0;
252 }
253 
254 //_____________________________________________________________________________
256 {
257  if (!IsOpen())
258  {
259  std::cout << Name() << ": fileclose: No Input file open" << std::endl;
260  return -1;
261  }
262  m_IManager.reset();
263  m_IManager_background.reset();
264  IsOpen(0);
265  UpdateFileList();
266  return 0;
267 }
268 
269 //_____________________________________________________________________________
270 int Fun4AllSingleDstPileupInputManager::BranchSelect(const std::string &branch, const int iflag)
271 {
272  int myflag = iflag;
273  // if iflag > 0 the branch is set to read
274  // if iflag = 0, the branch is set to NOT read
275  // if iflag < 0 the branchname is erased from our internal branch read map
276  // this does not have any effect on phool yet
277  if (myflag < 0)
278  {
279  std::map<const std::string, int>::iterator branchiter;
280  branchiter = m_branchread.find(branch);
281  if (branchiter != m_branchread.end())
282  {
283  m_branchread.erase(branchiter);
284  }
285  return 0;
286  }
287 
288  if (myflag > 0)
289  {
290  if (Verbosity() > 1)
291  {
292  std::cout << "Setting Root Tree Branch: " << branch << " to read" << std::endl;
293  }
294  myflag = 1;
295  }
296  else
297  {
298  if (Verbosity() > 1)
299  {
300  std::cout << "Setting Root Tree Branch: " << branch << " to NOT read" << std::endl;
301  }
302  }
303  m_branchread[branch] = myflag;
304  return 0;
305 }
306 
307 //_____________________________________________________________________________
309 {
310  if (m_IManager)
311  {
312  if (!m_branchread.empty())
313  {
314  std::map<const std::string, int>::const_iterator branchiter;
315  for (branchiter = m_branchread.begin(); branchiter != m_branchread.end(); ++branchiter)
316  {
317  m_IManager->selectObjectToRead(branchiter->first.c_str(), branchiter->second);
319  {
320  m_IManager_background->selectObjectToRead(branchiter->first.c_str(), branchiter->second);
321  }
322  if (Verbosity() > 0)
323  {
324  std::cout << branchiter->first << " set to " << branchiter->second << std::endl;
325  }
326  }
327  }
328  }
329  else
330  {
331  std::cout << PHWHERE << " " << Name() << ": You can only call this function after a file has been opened" << std::endl;
332  std::cout << "Do not worry, the branches will be set as soon as you open a file" << std::endl;
333  return -1;
334  }
335  return 0;
336 }
337 
338 //_____________________________________________________________________________
339 void Fun4AllSingleDstPileupInputManager::Print(const std::string &what) const
340 {
341  if (what == "ALL" || what == "BRANCH")
342  {
343  // loop over the map and print out the content (name and location in memory)
344  std::cout << "--------------------------------------" << std::endl
345  << std::endl;
346  std::cout << "List of selected branches in Fun4AllSingleDstPileupInputManager " << Name() << ":" << std::endl;
347 
348  std::map<const std::string, int>::const_iterator iter;
349  for (iter = m_branchread.begin(); iter != m_branchread.end(); ++iter)
350  {
351  std::cout << iter->first << " is switched ";
352  if (iter->second)
353  {
354  std::cout << "ON";
355  }
356  else
357  {
358  std::cout << "OFF";
359  }
360  std::cout << std::endl;
361  }
362  }
363  if ((what == "ALL" || what == "PHOOL") && m_IManager)
364  {
365  // loop over the map and print out the content (name and location in memory)
366  std::cout << "--------------------------------------" << std::endl
367  << std::endl;
368  std::cout << "PHNodeIOManager print in Fun4AllSingleDstPileupInputManager " << Name() << ":" << std::endl;
369  m_IManager->print();
370  }
372  return;
373 }
374 
375 //_____________________________________________________________________________
377 {
378  if (m_IManager)
379  {
380  unsigned EventOnDst = m_IManager->getEventNumber();
381  EventOnDst -= static_cast<unsigned>(i);
382  m_ievent_thisfile -= i;
383  m_ievent_total -= i;
384  m_IManager->setEventNumber(EventOnDst);
385  return 0;
386  }
387  std::cout << PHWHERE << Name() << ": could not push back events, Imanager is NULL"
388  << " probably the dst is not open yet (you need to call fileopen or run 1 event for lists)" << std::endl;
389  return -1;
390 }
391