35{
37 const std::string& treeName)
38 {
42 (worker->getAlg ("NTupleSvc_" + outputStream + "_" + treeName));
43 if (result == 0)
44 {
46 (worker->getAlg ("NTupleSvc_" + outputStream));
47 }
50 }
51
52
53
54 void NTupleSvc ::
55 testInvariant () const
56 {
58 }
59
60
61
62 NTupleSvc ::
63 NTupleSvc (const std::string& val_outputName)
64 : m_file (0),
m_tree (0), m_initialized (false),
65 m_whiteboard (0)
66 {
68
69 m_outputName = val_outputName;
70
72 }
73
74
75
76 NTupleSvc ::
77 ~NTupleSvc ()
78 {
80 }
81
82
83
84 void NTupleSvc ::
85 copyBranch (const std::string& name)
86 {
88 m_copyBranches.insert (name);
89 }
90
91
92
93 void NTupleSvc ::
94 copyBranchList (const std::string& fileName)
95 {
96
97
100 while (getline (
file, line))
101 {
102 while (!
line.empty() && isspace (line[0]))
104 while (!
line.empty() && isspace (line[
line.size()-1]))
106 if (!
line.empty() && line[0] !=
'#')
107 copyBranch (line);
108 }
109 }
110
111
112
113 void NTupleSvc ::
114 addWhiteFloat (const std::string& varName)
115 {
117 m_whiteFloat.insert (varName);
118 }
119
120
121
122 void NTupleSvc ::
123 addWhiteArray (const std::string& varName)
124 {
126 m_whiteArray.insert (varName);
127 }
128
129
130
131 TTree *NTupleSvc ::
132 tree () const
133 {
137 }
138
139
140
141 bool NTupleSvc ::
142 getFilterPassed () const
143 {
145 return m_taken;
146 }
147
148
149
150 void NTupleSvc ::
151 setFilterPassed (
bool passed)
152 {
155 m_taken = true;
156 }
157
158
159
160 const std::string& NTupleSvc ::
161 treeName () const
162 {
164 return m_treeName;
165 }
166
167
168
169 void NTupleSvc ::
170 treeName (const std::string& val_treeName)
171 {
173 m_treeName = val_treeName;
174 }
175
176
177
178 const char *NTupleSvc ::
179 GetName () const
180 {
182 return m_outputName.c_str();
183 }
184
185
186
188 setupJob (Job& job)
189 {
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;
193 }
194
195
196
198 {
200
201 m_connected = false;
202 return EL::StatusCode::SUCCESS;
203 }
204
205
206
208 {
210
211 m_file = wk()->getOutputFile (m_outputName);
212 std::string
name = m_treeName;
213 if (
name.empty())
name = wk()->tree()->GetName();
215 try
216 {
217 m_file->cd ();
218 m_tree =
new TTree (
name.c_str(), m_tree_title.c_str());
219 curDir->cd ();
220 } catch (...)
221 {
222 curDir->cd ();
223 }
224
225
226 m_tree->SetAutoSave(10000);
227
228 m_tree->SetAutoFlush( -30000000 );
229 TTree::SetBranchStyle(1);
230
231 return EL::StatusCode::SUCCESS;
232 }
233
234
235
237 {
239
240 m_taken = false;
241 return EL::StatusCode::SUCCESS;
242 }
243
244
245
247 {
249
250 initBranches ();
251
252 if (!m_taken)
253 return EL::StatusCode::SUCCESS;
254
255 copyInput ();
256
257 for (whiteInfoMIter iter = m_whiteInfo.begin(),
258 end = m_whiteInfo.end(); iter != end; ++ iter)
259 {
261 {
264 m_whiteboard->getArray (
iter->name,
size, values);
265 std::vector<float>(values,values+
size).swap (
iter->buffer);
266 } else
267 iter->buffer[0] = m_whiteboard->getFloat (
iter->name);
268 }
269
271 return EL::StatusCode::SUCCESS;
272 }
273
274
275
276 bool NTupleSvc ::
277 hasName (const std::string& name) const
278 {
280 return
281 name ==
"NTupleSvc_" + m_outputName ||
282 name ==
"NTupleSvc_" + m_outputName +
"_" + m_treeName;
283 }
284
285
286
287 void NTupleSvc ::
288 initBranches ()
289 {
290 if (m_initialized == false)
291 {
292 m_initialized = true;
293
294 std::set<std::string> branchList;
295
296 findBranches (branchList);
297 initOutput (branchList);
298
299 for (m_whiteFloatIter iter = m_whiteFloat.begin(),
300 end = m_whiteFloat.end(); iter != end; ++ iter)
301 {
305 m_whiteInfo.push_back (info);
306 }
307 for (m_whiteArrayIter iter = m_whiteArray.begin(),
308 end = m_whiteArray.end(); iter != end; ++ iter)
309 {
313 m_whiteInfo.push_back (info);
314 }
315 for (whiteInfoMIter iter = m_whiteInfo.begin(),
316 end = m_whiteInfo.end(); iter != end; ++ iter)
317 {
319 {
322 } else
323 {
324 iter->buffer.resize (1);
326 }
327 }
328 if (!m_whiteInfo.empty())
330 }
331 }
332
333
334 void NTupleSvc ::
335 findBranches (std::set<std::string>& branchList)
336 {
337 for (m_copyBranchesIter iter = m_copyBranches.begin(),
338 end = m_copyBranches.end(); iter != end; ++ iter)
339 {
340 std::size_t
count = 0;
342 TObject *object = 0;
343
344 for (TIter branchIter (wk()->
tree()->GetListOfBranches());
345 (object = branchIter());)
346 {
348 Ssiz_t len = 0;
349
351 {
352 branchList.insert (
str.Data());
354 }
355 }
357 throw std::runtime_error ("could not find any branch that matches pattern \"" + *iter + "\"");
358 }
359 }
360
361
362
363 void NTupleSvc ::
364 initOutput (const std::string& branchName)
365 {
366
367 for (copyInfoMIter branch = m_copyInfo.begin(),
368 end = m_copyInfo.end(); branch != end; ++ branch)
369 {
370 if (branch->name == branchName)
371 return;
372 }
373
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);
379
380 const char *className =
info.source->GetClassName();
381 if (strlen (className) > 0)
382 {
383 info.target =
m_tree->Branch (branchName.c_str(), className,
384 static_cast<void*>(0));
385 } else
386 {
387 static std::map<std::string,std::string>
types;
389 {
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";
401 }
402
403 std::string leaves;
405 for (TIter iter =
info.source->GetListOfLeaves(); (
object =
iter()); )
406 {
407 TLeaf *myleaf =
dynamic_cast<TLeaf*
>(
object);
408 if (myleaf == 0)
409 throw std::runtime_error ("found non-leaf object in leaf list");
410
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);
416
417 if (!leaves.empty())
418 leaves += ":";
419 leaves = leaves + myleaf->GetTitle() +
"/" +
type->second;
420 }
421 if (leaves.empty())
422 throw std::runtime_error ("failed to scan leaves of branch " + branchName);
423
424 for (std::string::size_type pos = 0;
425 (
pos = leaves.find (
"[", pos)) != std::string::npos; )
426 {
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'))
433 initOutput (dim);
434 }
435 info.target =
m_tree->Branch (branchName.c_str(),
static_cast<void*
>(0),
436 leaves.c_str());
437 }
438 m_copyInfo.push_back (info);
439 }
440
441
442
443 void NTupleSvc ::
444 initOutput (const std::set<std::string>& branchList)
445 {
446 for (std::set<std::string>::const_iterator branch = branchList.begin(),
447 end = branchList.end(); branch != end; ++ branch)
448 initOutput (*branch);
449 }
450
451
452
453 void NTupleSvc ::
454 copyInput ()
455 {
456 if (!m_connected)
457 {
458 for (copyInfoMIter info = m_copyInfo.begin(),
459 end = m_copyInfo.end(); info != end; ++ info)
460 {
461 info->source =
dynamic_cast<TBranch*
>
462 (wk()->tree()->GetBranch (
info->name.c_str()));
464 }
465 m_connected = true;
466 }
467
468 for (copyInfoMIter info = m_copyInfo.begin(),
469 end = m_copyInfo.end(); info != end; ++ info)
470 {
471 void *address =
info->source->GetAddress();
472 if (address == 0)
473 {
474 std::size_t
size =
sizeof (
void*);
476 for (TIter iter =
info->source->GetListOfLeaves();
478 {
479 TLeaf *myleaf =
dynamic_cast<TLeaf*
>(
object);
481 Int_t countval;
482 TLeaf *countleaf = myleaf->GetLeafCounter (countval);
483 if (countleaf)
484 countval = countleaf->GetMaximum();
485 if (countval < 0)
486 throw std::runtime_error (std::format (
"could not determine size of leaf {} in branch {}", myleaf->GetName(),
info->name));
487 if (countval == 0)
488 countval = 1;
489 const std::size_t mysize
490 = myleaf->GetOffset() + myleaf->GetLenType() * countval;
493 }
495 info->source->SetStatus (1);
496 info->source->SetAddress (address = &
info->buffer[0]);
497 }
499 info->target->SetAddress (address);
500 info->source->GetEntry (wk()->treeEntry(), 1);
501 }
502 }
503}
#define RCU_DESTROY_INVARIANT(x)
#define RCU_ASSERT2_SOFT(x, y)
#define RCU_CHANGE_INVARIANT(x)
#define RCU_REQUIRE2_SOFT(x, y)
#define RCU_NEW_INVARIANT(x)
#define RCU_REQUIRE_SOFT(x)
#define RCU_READ_INVARIANT(x)
static const std::vector< std::string > types
size_t size() const
Number of registered mappings.
int count(std::string s, const std::string ®x)
count how many occurances of a regx are in a string
ID3PD * m_tree
Pointer to the ID3PD object used.
NTupleSvc * getNTupleSvc(IWorker *worker, const std::string &outputStream, const std::string &treeName="")
effects: get the skimming algorithm for the given output for this worker guarantee: strong failures: ...
::StatusCode StatusCode
StatusCode definition for legacy code.
WhiteBoardSvc * getWhiteBoardSvc(IWorker *worker)
effects: get the whiteboard service for this worker guarantee: strong failures: formula service not c...