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
61 NTupleSvc (
const std::string& val_outputName)
62 : m_file (0), m_tree (0), m_initialized (
false),
67 m_outputName = val_outputName;
83 copyBranch (
const std::string& name)
86 m_copyBranches.insert (name);
92 copyBranchList (
const std::string& fileName)
96 std::ifstream
file (fileName.c_str());
98 while (getline (
file, line))
100 while (!line.empty() && isspace (line[0]))
101 line = line.substr (1);
102 while (!line.empty() && isspace (line[line.size()-1]))
104 if (!line.empty() && line[0] !=
'#')
112 addWhiteFloat (
const std::string& varName)
115 m_whiteFloat.insert (varName);
121 addWhiteArray (
const std::string& varName)
124 m_whiteArray.insert (varName);
140 getFilterPassed ()
const
149 setFilterPassed (
bool passed)
158 const std::string& NTupleSvc ::
168 treeName (
const std::string& val_treeName)
171 m_treeName = val_treeName;
176 const char *NTupleSvc ::
180 return m_outputName.c_str();
189 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());
190 return EL::StatusCode::SUCCESS;
200 return EL::StatusCode::SUCCESS;
209 m_file = wk()->getOutputFile (m_outputName);
210 std::string name = m_treeName;
211 if (name.empty()) name = wk()->tree()->GetName();
212 TDirectory *curDir = gDirectory;
216 m_tree =
new TTree (name.c_str(), m_tree_title.c_str());
224 m_tree->SetAutoSave(10000);
226 m_tree->SetAutoFlush( -30000000 );
227 TTree::SetBranchStyle(1);
229 return EL::StatusCode::SUCCESS;
239 return EL::StatusCode::SUCCESS;
251 return EL::StatusCode::SUCCESS;
255 for (whiteInfoMIter iter = m_whiteInfo.begin(),
256 end = m_whiteInfo.end(); iter != end; ++ iter)
262 m_whiteboard->getArray (iter->name,
size, values);
263 std::vector<float>(values,values+
size).swap (iter->buffer);
265 iter->buffer[0] = m_whiteboard->getFloat (iter->name);
269 return EL::StatusCode::SUCCESS;
275 hasName (
const std::string& name)
const
279 name ==
"NTupleSvc_" + m_outputName ||
280 name ==
"NTupleSvc_" + m_outputName +
"_" + m_treeName;
288 if (m_initialized ==
false)
290 m_initialized =
true;
292 std::set<std::string> branchList;
294 findBranches (branchList);
295 initOutput (branchList);
297 for (m_whiteFloatIter iter = m_whiteFloat.begin(),
298 end = m_whiteFloat.end(); iter != end; ++ iter)
303 m_whiteInfo.push_back (info);
305 for (m_whiteArrayIter iter = m_whiteArray.begin(),
306 end = m_whiteArray.end(); iter != end; ++ iter)
311 m_whiteInfo.push_back (info);
313 for (whiteInfoMIter iter = m_whiteInfo.begin(),
314 end = m_whiteInfo.end(); iter != end; ++ iter)
318 iter->pointer = &iter->buffer;
319 m_tree->Branch (iter->name.c_str(), &iter->pointer);
322 iter->buffer.resize (1);
323 m_tree->Branch (iter->name.c_str(), &iter->buffer[0], (iter->name +
"/F").c_str());
326 if (!m_whiteInfo.empty())
333 findBranches (std::set<std::string>& branchList)
335 for (m_copyBranchesIter iter = m_copyBranches.begin(),
336 end = m_copyBranches.end(); iter != end; ++ iter)
338 std::size_t
count = 0;
339 TRegexp pattern (*iter);
342 for (TIter branchIter (wk()->
tree()->GetListOfBranches());
343 (
object = branchIter());)
345 TString
str (object->GetName());
348 if (pattern.Index (
str, &len) == 0 && len ==
str.Length())
350 branchList.insert (
str.Data());
355 throw std::runtime_error (
"could not find any branch that matches pattern \"" + *iter +
"\"");
362 initOutput (
const std::string& branchName)
365 for (copyInfoMIter branch = m_copyInfo.begin(),
366 end = m_copyInfo.end(); branch != end; ++ branch)
368 if (branch->name == branchName)
373 info.name = branchName;
374 info.source = wk()->tree()->FindBranch (branchName.c_str());
375 if (info.source == 0)
376 throw std::runtime_error (
"could not find input branch: " + branchName);
378 const char *className = info.source->GetClassName();
379 if (strlen (className) > 0)
381 info.target = m_tree->Branch (branchName.c_str(), className,
382 static_cast<void*
>(0));
385 static std::map<std::string,std::string>
types;
388 types[
"Char_t"] =
"B";
389 types[
"UChar_t"] =
"b";
390 types[
"Short_t"] =
"S";
391 types[
"UShort_t"] =
"s";
392 types[
"Int_t"] =
"I";
393 types[
"UInt_t"] =
"i";
394 types[
"Float_t"] =
"F";
395 types[
"Double_t"] =
"D";
396 types[
"Long64_t"] =
"L";
397 types[
"ULong64_t"] =
"l";
398 types[
"Bool_t"] =
"O";
403 for (TIter iter = info.source->GetListOfLeaves(); (
object = iter()); )
405 TLeaf *myleaf =
dynamic_cast<TLeaf*
>(object);
407 throw std::runtime_error (
"found non-leaf object in leaf list");
409 std::string typeName = myleaf->GetTypeName();
410 std::map<std::string,std::string>::const_iterator
type
411 =
types.find (typeName);
413 throw std::runtime_error (
"unknown leaf type " + typeName);
417 leaves = leaves + myleaf->GetTitle() +
"/" +
type->second;
420 throw std::runtime_error (
"failed to scan leaves of branch " + branchName);
422 for (std::string::size_type pos = 0;
423 (pos = leaves.find (
"[", pos)) != std::string::npos; )
426 std::string::size_type pos2 = leaves.find (
"]", pos);
427 if (pos2 == std::string::npos)
428 throw std::runtime_error (
"failed to scan leaf dimensions for " + leaves);
429 std::string dim = leaves.substr (pos, pos2 - pos);
430 if (!(dim[0] >=
'0' && dim[0] <=
'9'))
433 info.target = m_tree->Branch (branchName.c_str(),
static_cast<void*
>(0),
436 m_copyInfo.push_back (info);
442 initOutput (
const std::set<std::string>& branchList)
444 for (std::set<std::string>::const_iterator branch = branchList.begin(),
445 end = branchList.end(); branch != end; ++ branch)
446 initOutput (*branch);
456 for (copyInfoMIter info = m_copyInfo.begin(),
457 end = m_copyInfo.end(); info != end; ++ info)
459 info->source =
dynamic_cast<TBranch*
>
460 (wk()->tree()->GetBranch (info->name.c_str()));
461 RCU_ASSERT2_SOFT (info->source != 0, (
"source branch " + info->name +
" not found for copying").c_str());
466 for (copyInfoMIter info = m_copyInfo.begin(),
467 end = m_copyInfo.end(); info != end; ++ info)
469 void *address = info->source->GetAddress();
472 std::size_t
size =
sizeof (
void*);
474 for (TIter iter = info->source->GetListOfLeaves();
477 TLeaf *myleaf =
dynamic_cast<TLeaf*
>(object);
480 TLeaf *countleaf = myleaf->GetLeafCounter (countval);
482 countval = countleaf->GetMaximum();
484 throw std::runtime_error (std::format (
"could not determine size of leaf {} in branch {}", myleaf->GetName(), info->name));
487 const std::size_t mysize
488 = myleaf->GetOffset() + myleaf->GetLenType() * countval;
492 info->buffer.resize (
size, 0);
493 info->source->SetStatus (1);
494 info->source->SetAddress (address = &info->buffer[0]);
497 info->target->SetAddress (address);
498 info->source->GetEntry (wk()->treeEntry(), 1);