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