mana-core-athenarootcomps / src / RootNtupleOutputStream.cxx

The default branch has multiple heads

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include <cassert>
#include <string>
#include <vector>
#include <fnmatch.h>

// Framework include files
#include "GaudiKernel/GaudiException.h"
#include "GaudiKernel/Tokenizer.h"
#include "GaudiKernel/AlgFactory.h"
#include "GaudiKernel/IAlgManager.h"
#include "GaudiKernel/ISvcLocator.h"
#include "GaudiKernel/IOpaqueAddress.h"
#include "GaudiKernel/IProperty.h"
#include "GaudiKernel/ClassID.h"

#include "AthenaKernel/IClassIDSvc.h"
#include "AthenaKernel/IAthenaOutputTool.h"
#include "AthenaKernel/IAthenaOutputStreamTool.h"

#include "StoreGate/StoreGateSvc.h"
#include "SGTools/DataProxy.h"
#include "SGTools/ProxyMap.h"
#include "SGTools/SGIFolder.h"

#include "RootNtupleOutputStream.h"

namespace Athena {

// Standard Constructor
RootNtupleOutputStream::RootNtupleOutputStream(const std::string& name, ISvcLocator* pSvcLocator)
  : FilteredAlgorithm(name, pSvcLocator),
    m_dataStore("StoreGateSvc", name),
    m_pCLIDSvc("ClassIDSvc", name),
    m_streamer(std::string("AthenaOutputStreamTool/") + 
	       name + std::string("Tool"), this),
   m_helperTools(this) 
{
  assert(pSvcLocator);
  declareProperty("ItemList",               m_itemList);
  declareProperty("OutputFile",             m_outputName="DidNotNameOutput.root");
  declareProperty("EvtConversionSvc",       m_persName="EventPersistencySvc");
  declareProperty("WritingTool",            m_streamer);
  declareProperty("Store",                  m_dataStore);
  declareProperty("ProcessingTag",          m_processTag=name);
  declareProperty("ForceRead",              m_forceRead=false);
  declareProperty("PersToPers",             m_persToPers=false);
  declareProperty("ExemptPersToPers",       m_exemptPersToPers);
  declareProperty("ProvideDef",             m_provideDef=false);
  declareProperty("WriteOnExecute",         m_writeOnExecute=true);
  declareProperty("WriteOnFinalize",        m_writeOnFinalize=false);
  declareProperty("TakeItemsFromInput",     m_itemListFromTool=false);
  declareProperty("HelperTools",            m_helperTools);

  declareProperty("DynamicItemList",
		  m_dynamicItemList = false,
		  "dynamic output itemlist:\n" \
		  "  if enabled rediscover object list to be written out at each event\n" \
		  "  otherwise: reuse the one from the first event.");
}

// Standard Destructor
RootNtupleOutputStream::~RootNtupleOutputStream() 
{}

// initialize data writer
StatusCode 
RootNtupleOutputStream::initialize() 
{
  ATH_MSG_DEBUG("In initialize");
  if (!this->FilteredAlgorithm::initialize().isSuccess()) {
    ATH_MSG_ERROR("could not initialize base class");
    return StatusCode::FAILURE;
  }

  // Reset the number of events written
  m_events = 0;

  // set up the SG service:
  if (!m_dataStore.retrieve().isSuccess()) {
    ATH_MSG_FATAL("Could not locate default store");
    return StatusCode::FAILURE;
  } else {
    ATH_MSG_DEBUG("Found " << m_dataStore.type() << " store.");
  }
  assert(static_cast<bool>(m_dataStore));

  // set up the CLID service:
  if (!m_pCLIDSvc.retrieve().isSuccess()) {
    ATH_MSG_FATAL("Could not locate default ClassIDSvc");
    return StatusCode::FAILURE;
  }
  assert(static_cast<bool>(m_pCLIDSvc));

  // Get Output Stream tool for writing
  if (!m_streamer.retrieve().isSuccess()) {
    ATH_MSG_FATAL("Can not find " << m_streamer);
    return StatusCode::FAILURE;
  }
   
  const bool extendProvenanceRecord = true;
  if (!m_streamer->connectServices(m_dataStore.type(), 
				   m_persName, 
				   extendProvenanceRecord).isSuccess()) {
    ATH_MSG_FATAL("Unable to connect services");
    return StatusCode::FAILURE;
  }

  if (!m_helperTools.retrieve().isSuccess()) {
    ATH_MSG_FATAL("Can not find " << m_helperTools);
    return StatusCode::FAILURE;
  }
  ATH_MSG_INFO("Found " << m_helperTools << endreq << "Data output: " << m_outputName);

  bool allgood = true;
  for (std::vector<ToolHandle<IAthenaOutputTool> >::const_iterator 
	 iter = m_helperTools.begin(),
	 iend = m_helperTools.end();
       iter != iend; 
       ++iter) {
    if (!(*iter)->postInitialize().isSuccess()) {
      allgood = false;
    }
  }
  if (!allgood) {
    ATH_MSG_ERROR("problem in postInitialize of a helper tool");
    return StatusCode::FAILURE;
  }

  // For 'write on finalize', we set up listener for 'LastInputFile'
  // and perform write at this point. This happens at 'stop' of the
  // event selector. RDS 04/2010
  if (m_writeOnFinalize) {
    // Set to be listener for end of event
    ServiceHandle<IIncidentSvc> incSvc("IncidentSvc", this->name());
    if (!incSvc.retrieve().isSuccess()) {
      ATH_MSG_ERROR("Cannot get the IncidentSvc");
      return StatusCode::FAILURE;
    } else {
      ATH_MSG_DEBUG("Retrieved IncidentSvc");
    }
    incSvc->addListener(this, "MetaDataStop", 50);
    ATH_MSG_DEBUG("Added MetaDataStop listener");
  }
  ATH_MSG_DEBUG("End initialize");
  return StatusCode::SUCCESS;
}

void 
RootNtupleOutputStream::handle(const Incident& inc) 
{
   ATH_MSG_DEBUG("handle() incident type: " << inc.type());
   static const std::string s_METADATASTOP = "MetaDataStop";
   if (inc.type() == s_METADATASTOP) {
     // Moved preFinalize of helper tools to stop - want to optimize the
     // output file in finalize RDS 12/2009
     for (std::vector<ToolHandle<IAthenaOutputTool> >::const_iterator 
	    iter = m_helperTools.begin(),
	    iend = m_helperTools.end();
	  iter != iend; 
	  ++iter) {
       if (!(*iter)->preFinalize().isSuccess()) {
	 ATH_MSG_ERROR("Cannot finalize helper tool");
       }
     }
     // Always force a final commit in stop - mainly applies to AthenaPool 
     if (m_writeOnFinalize) {
       if (!write().isSuccess()) {  // true mean write AND commit
	 ATH_MSG_ERROR("Cannot write on finalize");
       }
     }
   }
   ATH_MSG_INFO("Records written: " << m_events);
   ATH_MSG_DEBUG("Leaving handle");
}

// terminate data writer
StatusCode 
RootNtupleOutputStream::finalize() 
{
  bool failed = false;
  ATH_MSG_DEBUG("finalize: Optimize output");
  // Connect the output file to the service
  if (!m_streamer->finalizeOutput().isSuccess()) {
    failed = true;
  }
  ATH_MSG_DEBUG("finalize: end optimize output");
  if (!m_helperTools.release().isSuccess()) {
    failed = true;
  }
  if (!m_streamer.release().isSuccess()) {
    failed = true;
  }
  return failed 
    ? StatusCode::FAILURE 
    : StatusCode::SUCCESS;
}

StatusCode
RootNtupleOutputStream::execute() 
{
  bool failed = false;
  for (std::vector<ToolHandle<IAthenaOutputTool> >::const_iterator 
	 iter = m_helperTools.begin(),
	 iend = m_helperTools.end();
       iter != iend; 
       ++iter) {
    if (!(*iter)->preExecute().isSuccess()) {
      failed = true;
    }
  }
  if (m_writeOnExecute) {
    if (!write().isSuccess()) {
      failed = true;
    }
  }
  for (std::vector<ToolHandle<IAthenaOutputTool> >::const_iterator 
	 iter = m_helperTools.begin(),
	 iend = m_helperTools.end();
       iter != iend; 
       ++iter) {
    if(!(*iter)->postExecute().isSuccess()) {
      failed = true;
    }
  }

  return failed 
    ? StatusCode::FAILURE 
    : StatusCode::SUCCESS;
}

// Work entry point
StatusCode
RootNtupleOutputStream::write() 
{
  bool failed = false;
  // Clear any previously existing item list
  clearSelection();
  // Test whether this event should be output
  if (isEventAccepted()) {
    // Connect the output file to the service
    if (m_streamer->connectOutput(m_outputName).isSuccess()) {
      // First check if there are any new items in the list
      collectAllObjects();
      StatusCode sc = m_streamer->streamObjects(m_objects);
      // Do final check of streaming
      if (!sc.isSuccess()) {
	if (!sc.isRecoverable()) {
	  ATH_MSG_FATAL("streamObjects failed.");
	  failed = true;
	} else {
	  ATH_MSG_DEBUG("streamObjects failed.");
	}
      }
      sc = m_streamer->commitOutput();
      if (!sc.isSuccess()) {
	ATH_MSG_FATAL("commitOutput failed.");
	failed = true;
      }
      clearSelection();
      if (!failed) {
	m_events++;
      }
    }
  }

  return failed
    ? StatusCode::FAILURE
    : StatusCode::SUCCESS;
}

// Clear collected object list
inline
void 
RootNtupleOutputStream::clearSelection()
{
  m_objects.resize(0);
  if (m_dynamicItemList) {
    m_selection.resize(0);
  }
}

void
RootNtupleOutputStream::collectAllObjects() 
{
  typedef std::vector<SG::FolderItem> Items_t;

  if (!m_dynamicItemList && !m_selection.empty()) {
    // reuse object list from previous event.
  } else {

    // if (m_itemListFromTool) {
    //   //FIXME:
    //   //   if (!m_streamer->getInputItemList(&*m_p2BWritten).isSuccess()) {
    //   //     ATH_MSG_WARNING("collectAllObjects() could not get ItemList from Tool.");
    //   //   }
    // }

    static const std::string s_plus = "+";
    static const std::string s_dash = "-";
    typedef std::vector<const SG::DataProxy*> Proxies_t;
    Proxies_t proxies = m_dataStore->proxies();

    const std::vector<std::string>& items = m_itemList.value();
    std::vector<std::string> toremove; 
    toremove.reserve(items.size());

    Items_t selection; 
    selection.reserve(items.size());

    for (Proxies_t::const_iterator
	   iproxy = proxies.begin(),
	   iend = proxies.end();
	 iproxy != iend;
	 ++iproxy) {
      const SG::DataProxy *proxy = *iproxy;
      if (!proxy) {
	continue;
      }
      for (std::vector<std::string>::const_iterator
	     jkey = items.begin(),
	     jend = items.end();
	   jkey != jend;
	   ++jkey) {
	if (!jkey->empty()) {
	  if ((*jkey)[0] == s_dash[0]) {
	    toremove.push_back(jkey->substr(1, std::string::npos));
	    continue;
	  }
	  std::string key = *jkey;
	  if ((*jkey)[0] == s_plus[0]) {
	    key = jkey->substr(1, std::string::npos);
	  }
	  int o = fnmatch(key.c_str(), 
			  proxy->name().c_str(),
			  FNM_PATHNAME);
	  if (o == 0) {
	    // accept
	    selection.push_back(SG::FolderItem(proxy->clID(), proxy->name()));
	    break;
	  }
	}
      }
    }

    m_selection.reserve(selection.size());
    if (toremove.empty()) {
      m_selection = selection;
    } else {
      for(Items_t::const_iterator 
	    isel=selection.begin(),
	    iend=selection.end();
	  isel != iend;
	  ++isel) {
	const std::string &name = isel->key();
	bool keep = true;
	for (std::vector<std::string>::const_iterator
	       jkey = toremove.begin(),
	       jend = toremove.end();
	     jkey != jend;
	     ++jkey) {
	  const std::string& key = *jkey;
	  int o = fnmatch(key.c_str(), 
			  name.c_str(),
			  FNM_PATHNAME);
	  if (o == 0) {
	    // reject
	    keep = false;
	    break;
	  }
	}
	if (keep) {
	  m_selection.push_back(*isel);
	}
      }
    }
  }

  for (Items_t::const_iterator
	 itr = m_selection.begin(),
	 iend = m_selection.end();
       itr != iend;
       ++itr) {
    const std::string &name = itr->key();
    SG::DataProxy *proxy = m_dataStore->proxy(itr->id(), name);
    if (NULL == proxy) {
      continue;
    }
    if (m_forceRead && proxy->isValid()) {
      if (!m_persToPers) {
        if (NULL == proxy->accessData()) {
          ATH_MSG_ERROR(" Could not get data object for id "
                        << proxy->clID() << ",\"" << proxy->name());
        }
      } else if (true /*m_exemptPersToPers.find(item.id()) != m_exemptPersToPers.end()*/) {
        if (NULL == proxy->accessData()) {
          ATH_MSG_ERROR(" Could not get data object for id "
                        << proxy->clID() << ",\"" << proxy->name());
        }
      }
    }
    DataObject *obj = proxy->object();
    if (NULL != obj) {
      m_objects.push_back(obj);
      ATH_MSG_DEBUG(" Added object " << proxy->clID() << ",\"" << proxy->name() << "\"");
	  // if (m_checkNumberOfWrites && !m_provideDef) {
	  //   std::string tn;
	  //   if (!m_pCLIDSvc->getTypeNameOfID(item.id(), tn).isSuccess()) {
	  //     ATH_MSG_ERROR(" Could not get type name for id "
	  // 		    << item.id() << ",\"" << itemProxy->name());
	  //   } else {
	  //     tn += '_' + itemProxy->name();
	  //     CounterMapType::iterator cit = m_objectWriteCounter.find(tn);
	  //     if (cit == m_objectWriteCounter.end()) {
	  // 	// First time through
	  // 	//std::pair<CounterMapType::iterator, bool> result =
	  // 	m_objectWriteCounter.insert(CounterMapType::value_type(tn, 1));
	  //     } else {
	  // 	// set to next iteration (to avoid double counting)
	  // 	// StreamTools will eliminate duplicates.
	  // 	(*cit).second = m_events + 1;
	  //     }
	  //   }
	  // }
	// } else if (!m_forceRead && m_persToPers && proxy->isValid()) {
	//   tAddr = itemProxy->transientAddress();
    } //if data object there
  }

  return;
}

} //> ns Athena
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.