35 NTupleSvc *
getNTupleSvc (IWorker *worker,
const std::string& outputStream,
36 const std::string& treeName)
40 NTupleSvc *result =
dynamic_cast<NTupleSvc*
>
41 (worker->getAlg (
"NTupleSvc_" + outputStream +
"_" + treeName));
44 result =
dynamic_cast<NTupleSvc*
>
45 (worker->getAlg (
"NTupleSvc_" + outputStream));
54 testInvariant ()
const
62 NTupleSvc (
const std::string& val_outputName)
63 : m_file (0), m_tree (0), m_initialized (
false),
68 m_outputName = val_outputName;
84 copyBranch (
const std::string& name)
87 m_copyBranches.insert (name);
93 copyBranchList (
const std::string& fileName)
97 std::ifstream
file (fileName.c_str());
99 while (getline (
file, line))
101 while (!line.empty() && isspace (line[0]))
102 line = line.substr (1);
103 while (!line.empty() && isspace (line[line.size()-1]))
105 if (!line.empty() && line[0] !=
'#')
113 addWhiteFloat (
const std::string& varName)
116 m_whiteFloat.insert (varName);
122 addWhiteArray (
const std::string& varName)
125 m_whiteArray.insert (varName);
141 getFilterPassed ()
const
150 setFilterPassed (
bool passed)
159 const std::string& NTupleSvc ::
169 treeName (
const std::string& val_treeName)
172 m_treeName = val_treeName;
177 const char *NTupleSvc ::
181 return m_outputName.c_str();
190 RCU_REQUIRE2_SOFT (job.outputHas (m_outputName), (
"output stream " + m_outputName +
" not configured. try:\n EL::OutputStream output (\"" + m_outputName +
"\");\n job.addOutput (output);").c_str());
191 return EL::StatusCode::SUCCESS;
201 return EL::StatusCode::SUCCESS;
210 m_file = wk()->getOutputFile (m_outputName);
211 std::string name = m_treeName;
212 if (name.empty()) name = wk()->tree()->GetName();
213 TDirectory *curDir = gDirectory;
217 m_tree =
new TTree (name.c_str(), m_tree_title.c_str());
225 m_tree->SetAutoSave(10000);
227 m_tree->SetAutoFlush( -30000000 );
228 TTree::SetBranchStyle(1);
230 return EL::StatusCode::SUCCESS;
240 return EL::StatusCode::SUCCESS;
252 return EL::StatusCode::SUCCESS;
256 for (whiteInfoMIter iter = m_whiteInfo.begin(),
257 end = m_whiteInfo.end(); iter != end; ++ iter)
263 m_whiteboard->getArray (iter->name,
size, values);
264 std::vector<float>(values,values+
size).swap (iter->buffer);
266 iter->buffer[0] = m_whiteboard->getFloat (iter->name);
270 return EL::StatusCode::SUCCESS;
276 hasName (
const std::string& name)
const
280 name ==
"NTupleSvc_" + m_outputName ||
281 name ==
"NTupleSvc_" + m_outputName +
"_" + m_treeName;
289 if (m_initialized ==
false)
291 m_initialized =
true;
293 std::set<std::string> branchList;
295 findBranches (branchList);
296 initOutput (branchList);
298 for (m_whiteFloatIter iter = m_whiteFloat.begin(),
299 end = m_whiteFloat.end(); iter != end; ++ iter)
304 m_whiteInfo.push_back (info);
306 for (m_whiteArrayIter iter = m_whiteArray.begin(),
307 end = m_whiteArray.end(); iter != end; ++ iter)
312 m_whiteInfo.push_back (info);
314 for (whiteInfoMIter iter = m_whiteInfo.begin(),
315 end = m_whiteInfo.end(); iter != end; ++ iter)
319 iter->pointer = &iter->buffer;
320 m_tree->Branch (iter->name.c_str(), &iter->pointer);
323 iter->buffer.resize (1);
324 m_tree->Branch (iter->name.c_str(), &iter->buffer[0], (iter->name +
"/F").c_str());
327 if (!m_whiteInfo.empty())
334 findBranches (std::set<std::string>& branchList)
336 for (m_copyBranchesIter iter = m_copyBranches.begin(),
337 end = m_copyBranches.end(); iter != end; ++ iter)
339 std::size_t
count = 0;
340 TRegexp pattern (*iter);
343 for (TIter branchIter (wk()->
tree()->GetListOfBranches());
344 (
object = branchIter());)
346 TString
str (object->GetName());
349 if (pattern.Index (
str, &len) == 0 && len ==
str.Length())
351 branchList.insert (
str.Data());
356 RCU_THROW_MSG (
"could not find any branch that matches pattern \"" + *iter +
"\"");
363 initOutput (
const std::string& branchName)
366 for (copyInfoMIter branch = m_copyInfo.begin(),
367 end = m_copyInfo.end(); branch != end; ++ branch)
369 if (branch->name == branchName)
374 info.name = branchName;
375 info.source = wk()->tree()->FindBranch (branchName.c_str());
376 if (info.source == 0)
377 RCU_THROW_MSG (
"could not find input branch: " + branchName);
379 const char *className = info.source->GetClassName();
380 if (strlen (className) > 0)
382 info.target = m_tree->Branch (branchName.c_str(), className,
383 static_cast<void*
>(0));
386 static std::map<std::string,std::string>
types;
389 types[
"Char_t"] =
"B";
390 types[
"UChar_t"] =
"b";
391 types[
"Short_t"] =
"S";
392 types[
"UShort_t"] =
"s";
393 types[
"Int_t"] =
"I";
394 types[
"UInt_t"] =
"i";
395 types[
"Float_t"] =
"F";
396 types[
"Double_t"] =
"D";
397 types[
"Long64_t"] =
"L";
398 types[
"ULong64_t"] =
"l";
399 types[
"Bool_t"] =
"O";
404 for (TIter iter = info.source->GetListOfLeaves(); (
object = iter()); )
406 TLeaf *myleaf =
dynamic_cast<TLeaf*
>(object);
410 std::string typeName = myleaf->GetTypeName();
411 std::map<std::string,std::string>::const_iterator
type
412 =
types.find (typeName);
418 leaves = leaves + myleaf->GetTitle() +
"/" +
type->second;
421 RCU_THROW_MSG (
"failed to scan leaves of branch " + branchName);
423 for (std::string::size_type pos = 0;
424 (pos = leaves.find (
"[", pos)) != std::string::npos; )
427 std::string::size_type pos2 = leaves.find (
"]", pos);
428 if (pos2 == std::string::npos)
429 RCU_THROW_MSG (
"failed to scan leaf dimensions for " + leaves);
430 std::string dim = leaves.substr (pos, pos2 - pos);
431 if (!(dim[0] >=
'0' && dim[0] <=
'9'))
434 info.target = m_tree->Branch (branchName.c_str(),
static_cast<void*
>(0),
437 m_copyInfo.push_back (info);
443 initOutput (
const std::set<std::string>& branchList)
445 for (std::set<std::string>::const_iterator branch = branchList.begin(),
446 end = branchList.end(); branch != end; ++ branch)
447 initOutput (*branch);
457 for (copyInfoMIter info = m_copyInfo.begin(),
458 end = m_copyInfo.end(); info != end; ++ info)
460 info->source =
dynamic_cast<TBranch*
>
461 (wk()->tree()->GetBranch (info->name.c_str()));
462 RCU_ASSERT2_SOFT (info->source != 0, (
"source branch " + info->name +
" not found for copying").c_str());
467 for (copyInfoMIter info = m_copyInfo.begin(),
468 end = m_copyInfo.end(); info != end; ++ info)
470 void *address = info->source->GetAddress();
473 std::size_t
size =
sizeof (
void*);
475 for (TIter iter = info->source->GetListOfLeaves();
478 TLeaf *myleaf =
dynamic_cast<TLeaf*
>(object);
481 TLeaf *countleaf = myleaf->GetLeafCounter (countval);
483 countval = countleaf->GetMaximum();
485 RCU_THROW_MSG (std::string (
"could not determine size of leaf ") + myleaf->GetName() +
" in branch " + info->name);
488 const std::size_t mysize
489 = myleaf->GetOffset() + myleaf->GetLenType() * countval;
493 info->buffer.resize (
size, 0);
494 info->source->SetStatus (1);
495 info->source->SetAddress (address = &info->buffer[0]);
498 info->target->SetAddress (address);
499 info->source->GetEntry (wk()->treeEntry(), 1);