42 NTupleSvc *
getNTupleSvc (IWorker *worker,
const std::string& outputStream,
43 const std::string& treeName)
47 NTupleSvc *
result =
dynamic_cast<NTupleSvc*
>
48 (worker->getAlg (
"NTupleSvc_" + outputStream +
"_" + treeName));
51 result =
dynamic_cast<NTupleSvc*
>
52 (worker->getAlg (
"NTupleSvc_" + outputStream));
61 testInvariant ()
const
69 NTupleSvc (
const std::string& val_outputName)
70 : m_file (0), m_tree (0), m_initialized (
false),
75 m_outputName = val_outputName;
91 copyBranch (
const std::string& name)
94 m_copyBranches.insert (name);
100 copyBranchList (
const std::string& fileName)
104 std::ifstream
file (fileName.c_str());
106 while (getline (
file, line))
108 while (!line.empty() && isspace (line[0]))
109 line = line.substr (1);
110 while (!line.empty() && isspace (line[line.size()-1]))
112 if (!line.empty() && line[0] !=
'#')
120 addWhiteFloat (
const std::string& varName)
123 m_whiteFloat.insert (varName);
129 addWhiteArray (
const std::string& varName)
132 m_whiteArray.insert (varName);
148 getFilterPassed ()
const
157 setFilterPassed (
bool passed)
166 const std::string& NTupleSvc ::
176 treeName (
const std::string& val_treeName)
179 m_treeName = val_treeName;
184 const char *NTupleSvc ::
188 return m_outputName.c_str();
193 StatusCode NTupleSvc ::
197 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());
198 return EL::StatusCode::SUCCESS;
208 return EL::StatusCode::SUCCESS;
217 m_file = wk()->getOutputFile (m_outputName);
218 std::string name = m_treeName;
219 if (name.empty()) name = wk()->tree()->GetName();
220 TDirectory *curDir = gDirectory;
224 m_tree =
new TTree (name.c_str(), m_tree_title.c_str());
232 m_tree->SetAutoSave(10000);
234 m_tree->SetAutoFlush( -30000000 );
235 TTree::SetBranchStyle(1);
237 return EL::StatusCode::SUCCESS;
247 return EL::StatusCode::SUCCESS;
259 return EL::StatusCode::SUCCESS;
263 for (whiteInfoMIter iter = m_whiteInfo.begin(),
264 end = m_whiteInfo.end(); iter != end; ++ iter)
270 m_whiteboard->getArray (iter->name, size, values);
271 std::vector<float>(values,values+size).swap (iter->buffer);
273 iter->buffer[0] = m_whiteboard->getFloat (iter->name);
277 return EL::StatusCode::SUCCESS;
283 hasName (
const std::string& name)
const
287 name ==
"NTupleSvc_" + m_outputName ||
288 name ==
"NTupleSvc_" + m_outputName +
"_" + m_treeName;
296 if (m_initialized ==
false)
298 m_initialized =
true;
300 std::set<std::string> branchList;
302 findBranches (branchList);
303 initOutput (branchList);
305 for (m_whiteFloatIter iter = m_whiteFloat.begin(),
306 end = m_whiteFloat.end(); iter != end; ++ iter)
311 m_whiteInfo.push_back (info);
313 for (m_whiteArrayIter iter = m_whiteArray.begin(),
314 end = m_whiteArray.end(); iter != end; ++ iter)
319 m_whiteInfo.push_back (info);
321 for (whiteInfoMIter iter = m_whiteInfo.begin(),
322 end = m_whiteInfo.end(); iter != end; ++ iter)
326 iter->pointer = &iter->buffer;
327 m_tree->Branch (iter->name.c_str(), &iter->pointer);
330 iter->buffer.resize (1);
331 m_tree->Branch (iter->name.c_str(), &iter->buffer[0], (iter->name +
"/F").c_str());
334 if (!m_whiteInfo.empty())
341 findBranches (std::set<std::string>& branchList)
343 for (m_copyBranchesIter iter = m_copyBranches.begin(),
344 end = m_copyBranches.end(); iter != end; ++ iter)
346 std::size_t
count = 0;
347 TRegexp pattern (*iter);
350 for (TIter branchIter (wk()->
tree()->GetListOfBranches());
351 (
object = branchIter());)
356 if (pattern.Index (
str, &len) == 0 && len ==
str.Length())
358 branchList.insert (
str.Data());
363 RCU_THROW_MSG (
"could not find any branch that matches pattern \"" + *iter +
"\"");
370 initOutput (
const std::string& branchName)
373 for (copyInfoMIter branch = m_copyInfo.begin(),
374 end = m_copyInfo.end(); branch != end; ++ branch)
376 if (branch->name == branchName)
381 info.name = branchName;
382 info.source = wk()->tree()->FindBranch (branchName.c_str());
383 if (info.source == 0)
384 RCU_THROW_MSG (
"could not find input branch: " + branchName);
386 const char *className = info.source->GetClassName();
387 if (strlen (className) > 0)
389 info.target = m_tree->Branch (branchName.c_str(), className,
390 static_cast<void*
>(0));
393 static std::map<std::string,std::string>
types;
396 types[
"Char_t"] =
"B";
397 types[
"UChar_t"] =
"b";
398 types[
"Short_t"] =
"S";
399 types[
"UShort_t"] =
"s";
400 types[
"Int_t"] =
"I";
401 types[
"UInt_t"] =
"i";
402 types[
"Float_t"] =
"F";
403 types[
"Double_t"] =
"D";
404 types[
"Long64_t"] =
"L";
405 types[
"ULong64_t"] =
"l";
406 types[
"Bool_t"] =
"O";
411 for (TIter iter = info.source->GetListOfLeaves(); (
object = iter()); )
413 TLeaf *myleaf =
dynamic_cast<TLeaf*
>(
object);
417 std::string typeName = myleaf->GetTypeName();
418 std::map<std::string,std::string>::const_iterator
type
419 =
types.find (typeName);
425 leaves = leaves + myleaf->GetTitle() +
"/" +
type->second;
428 RCU_THROW_MSG (
"failed to scan leaves of branch " + branchName);
430 for (std::string::size_type pos = 0;
431 (pos = leaves.find (
"[", pos)) != std::string::npos; )
434 std::string::size_type pos2 = leaves.find (
"]", pos);
435 if (pos2 == std::string::npos)
436 RCU_THROW_MSG (
"failed to scan leaf dimensions for " + leaves);
437 std::string dim = leaves.substr (pos, pos2 - pos);
438 if (!(dim[0] >=
'0' && dim[0] <=
'9'))
441 info.target = m_tree->Branch (branchName.c_str(),
static_cast<void*
>(0),
444 m_copyInfo.push_back (info);
450 initOutput (
const std::set<std::string>& branchList)
452 for (std::set<std::string>::const_iterator branch = branchList.begin(),
453 end = branchList.end(); branch != end; ++ branch)
454 initOutput (*branch);
464 for (copyInfoMIter info = m_copyInfo.begin(),
465 end = m_copyInfo.end(); info != end; ++ info)
467 info->source =
dynamic_cast<TBranch*
>
468 (wk()->tree()->GetBranch (info->name.c_str()));
469 RCU_ASSERT2_SOFT (info->source != 0, (
"source branch " + info->name +
" not found for copying").c_str());
474 for (copyInfoMIter info = m_copyInfo.begin(),
475 end = m_copyInfo.end(); info != end; ++ info)
477 void *address = info->source->GetAddress();
480 std::size_t size =
sizeof (
void*);
482 for (TIter iter = info->source->GetListOfLeaves();
485 TLeaf *myleaf =
dynamic_cast<TLeaf*
>(
object);
488 TLeaf *countleaf = myleaf->GetLeafCounter (countval);
490 countval = countleaf->GetMaximum();
492 RCU_THROW_MSG (std::string (
"could not determine size of leaf ") + myleaf->GetName() +
" in branch " + info->name);
495 const std::size_t mysize
496 = myleaf->GetOffset() + myleaf->GetLenType() * countval;
500 info->buffer.resize (size, 0);
501 info->source->SetStatus (1);
502 info->source->SetAddress (address = &info->buffer[0]);
505 info->target->SetAddress (address);
506 info->source->GetEntry (wk()->treeEntry(), 1);