36 NTupleSvc *
getNTupleSvc (IWorker *worker,
const std::string& outputStream,
37 const std::string& treeName)
41 NTupleSvc *result =
dynamic_cast<NTupleSvc*
>
42 (worker->getAlg (
"NTupleSvc_" + outputStream +
"_" + treeName));
45 result =
dynamic_cast<NTupleSvc*
>
46 (worker->getAlg (
"NTupleSvc_" + outputStream));
55 testInvariant ()
const
63 NTupleSvc (
const std::string& val_outputName)
64 : m_file (0), m_tree (0), m_initialized (
false),
69 m_outputName = val_outputName;
85 copyBranch (
const std::string& name)
88 m_copyBranches.insert (name);
94 copyBranchList (
const std::string& fileName)
98 std::ifstream
file (fileName.c_str());
100 while (getline (
file, line))
102 while (!line.empty() && isspace (line[0]))
103 line = line.substr (1);
104 while (!line.empty() && isspace (line[line.size()-1]))
106 if (!line.empty() && line[0] !=
'#')
114 addWhiteFloat (
const std::string& varName)
117 m_whiteFloat.insert (varName);
123 addWhiteArray (
const std::string& varName)
126 m_whiteArray.insert (varName);
142 getFilterPassed ()
const
151 setFilterPassed (
bool passed)
160 const std::string& NTupleSvc ::
170 treeName (
const std::string& val_treeName)
173 m_treeName = val_treeName;
178 const char *NTupleSvc ::
182 return m_outputName.c_str();
191 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());
192 return EL::StatusCode::SUCCESS;
202 return EL::StatusCode::SUCCESS;
211 m_file = wk()->getOutputFile (m_outputName);
212 std::string name = m_treeName;
213 if (name.empty()) name = wk()->tree()->GetName();
214 TDirectory *curDir = gDirectory;
218 m_tree =
new TTree (name.c_str(), m_tree_title.c_str());
226 m_tree->SetAutoSave(10000);
228 m_tree->SetAutoFlush( -30000000 );
229 TTree::SetBranchStyle(1);
231 return EL::StatusCode::SUCCESS;
241 return EL::StatusCode::SUCCESS;
253 return EL::StatusCode::SUCCESS;
257 for (whiteInfoMIter iter = m_whiteInfo.begin(),
258 end = m_whiteInfo.end(); iter != end; ++ iter)
264 m_whiteboard->getArray (iter->name,
size, values);
265 std::vector<float>(values,values+
size).swap (iter->buffer);
267 iter->buffer[0] = m_whiteboard->getFloat (iter->name);
271 return EL::StatusCode::SUCCESS;
277 hasName (
const std::string& name)
const
281 name ==
"NTupleSvc_" + m_outputName ||
282 name ==
"NTupleSvc_" + m_outputName +
"_" + m_treeName;
290 if (m_initialized ==
false)
292 m_initialized =
true;
294 std::set<std::string> branchList;
296 findBranches (branchList);
297 initOutput (branchList);
299 for (m_whiteFloatIter iter = m_whiteFloat.begin(),
300 end = m_whiteFloat.end(); iter != end; ++ iter)
305 m_whiteInfo.push_back (info);
307 for (m_whiteArrayIter iter = m_whiteArray.begin(),
308 end = m_whiteArray.end(); iter != end; ++ iter)
313 m_whiteInfo.push_back (info);
315 for (whiteInfoMIter iter = m_whiteInfo.begin(),
316 end = m_whiteInfo.end(); iter != end; ++ iter)
320 iter->pointer = &iter->buffer;
321 m_tree->Branch (iter->name.c_str(), &iter->pointer);
324 iter->buffer.resize (1);
325 m_tree->Branch (iter->name.c_str(), &iter->buffer[0], (iter->name +
"/F").c_str());
328 if (!m_whiteInfo.empty())
335 findBranches (std::set<std::string>& branchList)
337 for (m_copyBranchesIter iter = m_copyBranches.begin(),
338 end = m_copyBranches.end(); iter != end; ++ iter)
340 std::size_t
count = 0;
341 TRegexp pattern (*iter);
344 for (TIter branchIter (wk()->
tree()->GetListOfBranches());
345 (
object = branchIter());)
347 TString
str (object->GetName());
350 if (pattern.Index (
str, &len) == 0 && len ==
str.Length())
352 branchList.insert (
str.Data());
357 throw std::runtime_error (
"could not find any branch that matches pattern \"" + *iter +
"\"");
364 initOutput (
const std::string& branchName)
367 for (copyInfoMIter branch = m_copyInfo.begin(),
368 end = m_copyInfo.end(); branch != end; ++ branch)
370 if (branch->name == branchName)
375 info.name = branchName;
376 info.source = wk()->tree()->FindBranch (branchName.c_str());
377 if (info.source == 0)
378 throw std::runtime_error (
"could not find input branch: " + branchName);
380 const char *className = info.source->GetClassName();
381 if (strlen (className) > 0)
383 info.target = m_tree->Branch (branchName.c_str(), className,
384 static_cast<void*
>(0));
387 static std::map<std::string,std::string>
types;
390 types[
"Char_t"] =
"B";
391 types[
"UChar_t"] =
"b";
392 types[
"Short_t"] =
"S";
393 types[
"UShort_t"] =
"s";
394 types[
"Int_t"] =
"I";
395 types[
"UInt_t"] =
"i";
396 types[
"Float_t"] =
"F";
397 types[
"Double_t"] =
"D";
398 types[
"Long64_t"] =
"L";
399 types[
"ULong64_t"] =
"l";
400 types[
"Bool_t"] =
"O";
405 for (TIter iter = info.source->GetListOfLeaves(); (
object = iter()); )
407 TLeaf *myleaf =
dynamic_cast<TLeaf*
>(object);
409 throw std::runtime_error (
"found non-leaf object in leaf list");
411 std::string typeName = myleaf->GetTypeName();
412 std::map<std::string,std::string>::const_iterator
type
413 =
types.find (typeName);
415 throw std::runtime_error (
"unknown leaf type " + typeName);
419 leaves = leaves + myleaf->GetTitle() +
"/" +
type->second;
422 throw std::runtime_error (
"failed to scan leaves of branch " + branchName);
424 for (std::string::size_type pos = 0;
425 (pos = leaves.find (
"[", pos)) != std::string::npos; )
428 std::string::size_type pos2 = leaves.find (
"]", pos);
429 if (pos2 == std::string::npos)
430 throw std::runtime_error (
"failed to scan leaf dimensions for " + leaves);
431 std::string dim = leaves.substr (pos, pos2 - pos);
432 if (!(dim[0] >=
'0' && dim[0] <=
'9'))
435 info.target = m_tree->Branch (branchName.c_str(),
static_cast<void*
>(0),
438 m_copyInfo.push_back (info);
444 initOutput (
const std::set<std::string>& branchList)
446 for (std::set<std::string>::const_iterator branch = branchList.begin(),
447 end = branchList.end(); branch != end; ++ branch)
448 initOutput (*branch);
458 for (copyInfoMIter info = m_copyInfo.begin(),
459 end = m_copyInfo.end(); info != end; ++ info)
461 info->source =
dynamic_cast<TBranch*
>
462 (wk()->tree()->GetBranch (info->name.c_str()));
463 RCU_ASSERT2_SOFT (info->source != 0, (
"source branch " + info->name +
" not found for copying").c_str());
468 for (copyInfoMIter info = m_copyInfo.begin(),
469 end = m_copyInfo.end(); info != end; ++ info)
471 void *address = info->source->GetAddress();
474 std::size_t
size =
sizeof (
void*);
476 for (TIter iter = info->source->GetListOfLeaves();
479 TLeaf *myleaf =
dynamic_cast<TLeaf*
>(object);
482 TLeaf *countleaf = myleaf->GetLeafCounter (countval);
484 countval = countleaf->GetMaximum();
486 throw std::runtime_error (std::format (
"could not determine size of leaf {} in branch {}", myleaf->GetName(), info->name));
489 const std::size_t mysize
490 = myleaf->GetOffset() + myleaf->GetLenType() * countval;
494 info->buffer.resize (
size, 0);
495 info->source->SetStatus (1);
496 info->source->SetAddress (address = &info->buffer[0]);
499 info->target->SetAddress (address);
500 info->source->GetEntry (wk()->treeEntry(), 1);