ATLAS Offline Software
Loading...
Searching...
No Matches
SampleHandler.cxx File Reference
#include <SampleHandler/SampleHandler.h>
#include <iostream>
#include <sstream>
#include <TFile.h>
#include <TSystem.h>
#include <RootCoreUtils/Assert.h>
#include <RootCoreUtils/CheckRootVersion.h>
#include <RootCoreUtils/StringUtil.h>
#include <RootCoreUtils/ThrowMsg.h>
#include <SampleHandler/DiskListLocal.h>
#include <SampleHandler/MetaFields.h>
#include <SampleHandler/MetaObject.h>
#include <SampleHandler/Sample.h>
#include <SampleHandler/SamplePtr.h>

Go to the source code of this file.

Functions

 ClassImp (SH::SampleHandler) namespace SH

Function Documentation

◆ ClassImp()

ClassImp ( SH::SampleHandler )

Definition at line 39 of file SampleHandler.cxx.

42{
43 namespace
44 {
45 /*
46 bool isspace (const std::string& str)
47 {
48 for (std::string::const_iterator iter = str.begin(),
49 end = str.end(); iter != end; ++ iter)
50 {
51 if (!std::isspace (*iter))
52 return false;
53 };
54 return true;
55 }
56 */
57 }
58
59
60
61 std::string dbg (const SampleHandler& obj, unsigned verbosity)
62 {
63 std::ostringstream result;
64 result << "SampleHandler with " << obj.size() << " files";
65 if (verbosity % 10 > 0)
66 {
67 result << "\n";
68 for (SampleHandler::iterator sample = obj.begin(),
69 end = obj.end(); sample != end; ++ sample)
70 {
71 result << dbg (**sample, verbosity / 10) << "\n";
72 };
73 };
74 return result.str();
75 }
76
77
78
79 void swap (SampleHandler& a, SampleHandler& b)
80 {
81 swap (a.m_samples, b.m_samples);
82 swap (a.m_named, b.m_named);
83 }
84
85
86
87 void SampleHandler ::
88 testInvariant () const
89 {
90 }
91
92
93
94 SampleHandler ::
95 SampleHandler ()
96 {
98 RCU_NEW_INVARIANT (this);
99 }
100
101
102
103 SampleHandler ::
104 SampleHandler (const SampleHandler& that)
105 : TObject (that), m_samples (that.m_samples), m_named (that.m_named)
106 {
107 RCU_NEW_INVARIANT (this);
108 }
109
110
111
112 SampleHandler ::
113 ~SampleHandler ()
114 {
116 }
117
118
119
120 SampleHandler& SampleHandler ::
121 operator = (const SampleHandler& that)
122 {
123 // no invariant used
124 SampleHandler tmp (that);
125 swap (tmp, *this);
126 return *this;
127 }
128
129
130
131 void SampleHandler ::
132 add (Sample *sample)
133 {
134 SamplePtr mysample (sample);
135
136 // invariant not used
137 RCU_REQUIRE_SOFT (sample != 0);
138
139 add (mysample);
140 }
141
142
143
144 void SampleHandler ::
145 add (std::unique_ptr<Sample> sample)
146 {
147 // no invariant used
148 add (SamplePtr (std::move (sample)));
149 }
150
151
152
153 void SampleHandler ::
154 add (SamplePtr& sample)
155 {
157 RCU_REQUIRE_SOFT (!sample.empty());
158 RCU_REQUIRE_SOFT (!sample->name().empty());
159
160 if (!sample->name().empty() && m_named.find (sample->name()) != m_named.end())
161 RCU_THROW_MSG ("can't add sample of name " + sample->name() + "\na sample with that name already exists\nold sample:\n" + dbg (*m_named.find (sample->name())->second, 9999) + "\nnew sample:\n" + dbg (*sample, 9999));
162
163 try
164 {
165 m_samples.push_back (sample.get());
166 if (!sample->name().empty())
167 m_named[sample->name()] = sample;
168 } catch (...)
169 {
170 if (m_samples.back() == sample.get())
171 m_samples.pop_back();
172 };
173 }
174
175
176
177 void SampleHandler ::
178 add (SamplePtr&& sample)
179 {
180 add (sample);
181 }
182
183
184
185 void SampleHandler ::
186 add (const SampleHandler& sh)
187 {
188 // invariant not used
189 RCU_REQUIRE_SOFT (this != &sh);
190
191 for (iterator iter = sh.begin(), end2 = sh.end();
192 iter != end2; ++ iter)
193 {
194 add (*iter);
195 };
196 }
197
198
199
200 void SampleHandler ::
201 addWithPrefix (const SampleHandler& sh, const std::string& prefix)
202 {
203 // invariant not used
204 RCU_REQUIRE_SOFT (this != &sh);
205
206 for (iterator iter = sh.begin(), end2 = sh.end();
207 iter != end2; ++ iter)
208 {
209 std::unique_ptr<Sample> sample (dynamic_cast<Sample*>((*iter)->Clone ()));
210 RCU_ASSERT (sample != nullptr);
211 sample->name (prefix + (*iter)->name());
212 add (sample.release());
213 };
214 }
215
216
217
218 void SampleHandler ::
219 remove (const std::string& name)
220 {
221 // invariant not used
222 const Sample *sample = get (name);
223 if (sample == 0)
224 RCU_THROW_MSG ("sample " + name + " not found in SampleHandler");
225 remove (sample);
226 }
227
228
229
230 void SampleHandler ::
231 remove (const Sample *sample)
232 {
234 RCU_REQUIRE_SOFT (sample != 0);
235
236 NamedMIter nameIter = m_named.find (sample->name());
237 if (nameIter == m_named.end())
238 RCU_THROW_MSG ("sample " + sample->name() + " not found in SampleHandler");
239 if (nameIter->second.get() != sample)
240 RCU_THROW_MSG ("different sample of name " + sample->name() + " found in SampleHandler");
241 std::erase (m_samples, sample);
242 m_named.erase (nameIter);
243 }
244
245
246
247 Sample *SampleHandler ::
248 get (const std::string& name)
249 {
250 RCU_READ_INVARIANT (this);
251
252 NamedIter iter = m_named.find (name);
253 if (iter != m_named.end())
254 return iter->second.get();
255 return 0;
256 }
257
258
259 const Sample *SampleHandler ::
260 get (const std::string& name) const
261 {
262 RCU_READ_INVARIANT (this);
263
264 auto iter = m_named.find (name);
265 if (iter != m_named.end())
266 return iter->second.get();
267 return 0;
268 }
269
270
271
272 SampleHandler SampleHandler ::
273 find (const std::string& tags) const
274 {
275 // no invariant used
276 return find (TagList (tags, ','));
277 }
278
279
280
281 SampleHandler SampleHandler ::
282 find (const TagList& tags) const
283 {
284 RCU_READ_INVARIANT (this);
285
286 SampleHandler result;
287
288 for (SamplesIter sample = m_samples.begin(),
289 end = m_samples.end(); sample != end; ++ sample)
290 {
291 bool use = false;
292 for (TagList::iterator iter = tags.begin(),
293 end = tags.end(); !use && iter != end; ++ iter)
294 use = (*sample)->tags().has (*iter);
295 if (use)
296 result.add (*sample);
297 };
298 return result;
299 }
300
301
302
303 Sample *SampleHandler ::
304 findBySource (const std::string& name) const
305 {
306 RCU_READ_INVARIANT (this);
307
308 std::vector<Sample*> result;
309 for (iterator sample = begin(),
310 end2 = end(); sample != end2; ++ sample)
311 {
312 if (name == (*sample)->meta()->castString (MetaFields::sourceSample, (*sample)->name()))
313 result.push_back ((*sample));
314 };
315 if (result.size() > 1)
316 {
317 std::ostringstream message;
318 message << "multiple samples have " << name << " as a source:";
319 for (std::vector<Sample*>::const_iterator sample = result.begin(),
320 end = result.end(); sample != end; ++ sample)
321 message << " " << (*sample)->name();
322 RCU_THROW_MSG (message.str());
323 };
324 if (result.empty())
325 return 0;
326 return result.front();
327 }
328
329
330
331 SampleHandler SampleHandler ::
332 findByName (const std::string& pattern) const
333 {
334 RCU_READ_INVARIANT (this);
335 SampleHandler result;
336 std::regex expr (pattern);
337 for (iterator iter = begin(), end = this->end(); iter != end; ++ iter)
338 {
339 if (RCU::match_expr (expr, (*iter)->name()))
340 result.add (*iter);
341 }
342 return result;
343 }
344
345
346
347 void SampleHandler ::
348 print () const
349 {
350 RCU_READ_INVARIANT (this);
351 std::cout << dbg (*this, 9999) << std::endl;
352 }
353
354
355
356 void SampleHandler ::
357 printContent () const
358 {
359 // not using invariant
360 print ();
361 }
362
363
364
365 void SampleHandler ::
366 save (const std::string& directory) const
367 {
368 RCU_READ_INVARIANT (this);
369
370 // rationale: not checking the return status, since this is just a
371 // courtesy directory creation that is Ok to fail.
372 gSystem->MakeDirectory (directory.c_str());
373 for (iterator iter = this->begin(),
374 end = this->end(); iter != end; ++ iter)
375 {
376 TFile file ((directory + "/" + (*iter)->name() + ".root").c_str(), "RECREATE");
377 (*iter)->Write ("sample");
378 };
379 }
380
381
382
383 void SampleHandler ::
384 load (const std::string& directory)
385 {
387
388 DiskListLocal mydir (directory);
389 while (mydir.next())
390 {
391 const std::string file = mydir.fileName();
392
393 if (file.size() > 5 &&
394 file.rfind (".root") == file.size() - 5)
395 {
396 TFile myfile (mydir.path().c_str(), "READ");
397 Sample *const sample = dynamic_cast<Sample*>(myfile.Get ("sample"));
398 if (sample != 0)
399 add (sample);
400 };
401 };
402 }
403
404
405
406 void SampleHandler ::
407 updateLocation (const std::string& from, const std::string& to)
408 {
409 // no invariant used
410 RCU_REQUIRE_SOFT (!from.empty());
411 RCU_REQUIRE_SOFT (!to.empty());
412 for (iterator sample = begin(),
413 end = this->end(); sample != end; ++ sample)
414 (*sample)->updateLocation (from, to);
415 }
416
417
418
419 void SampleHandler ::
420 fetch (const SampleHandler& source)
421 {
422 // invariant not used
423
424 for (iterator sample = begin(),
425 end2 = end(); sample != end2; ++ sample)
426 {
427 const std::string name
428 = (*sample)->meta()->castString (MetaFields::sourceSample, (*sample)->name());
429 const Sample *const mysource = source.get (name);
430 if (mysource)
431 (*sample)->meta()->fetch (*mysource->meta());
432 };
433 }
434
435
436
437 void SampleHandler ::
438 fetchDefaults (const SampleHandler& source)
439 {
440 // invariant not used
441
442 for (iterator sample = begin(),
443 end2 = end(); sample != end2; ++ sample)
444 {
445 const std::string name
446 = (*sample)->meta()->castString (MetaFields::sourceSample, (*sample)->name());
447 const Sample *const mysource = source.get (name);
448 if (mysource)
449 (*sample)->meta()->fetchDefaults (*mysource->meta());
450 };
451 }
452
453
454
455 bool SampleHandler ::
456 check_complete (const SampleHandler& source) const
457 {
458 // invariant not used
459
460 std::set<std::string> names;
461 for (iterator sample = begin(),
462 end2 = end(); sample != end2; ++ sample)
463 {
464 names.insert ((*sample)->meta()->castString (MetaFields::sourceSample, (*sample)->name()));
465 };
466
467 for (iterator sample = source.begin(),
468 end2 = source.end(); sample != end2; ++ sample)
469 {
470 if (names.find ((*sample)->name()) == names.end())
471 return false;
472 };
473 return true;
474 }
475
476
477
478 void SampleHandler ::
479 setMetaDouble (const std::string& name, double value)
480 {
481 // no invariant used
482
483 for (iterator sample = begin(),
484 end2 = end(); sample != end2; ++ sample)
485 {
486 (*sample)->meta()->setDouble (name, value);
487 };
488 }
489
490
491
492 void SampleHandler ::
493 setMetaString (const std::string& name, const std::string& value)
494 {
495 // no invariant used
496
497 for (iterator sample = begin(),
498 end2 = end(); sample != end2; ++ sample)
499 {
500 (*sample)->meta()->setString (name, value);
501 };
502 }
503
504
505
506 void SampleHandler ::
507 setMetaDouble (const std::string& pattern, const std::string& name,
508 double value)
509 {
510 // no invariant used
511
512 std::regex mypattern (pattern);
513
514 for (iterator sample = begin(),
515 end2 = end(); sample != end2; ++ sample)
516 {
517 if (RCU::match_expr (mypattern, (*sample)->name()))
518 (*sample)->meta()->setDouble (name, value);
519 };
520 }
521
522
523
524 void SampleHandler ::
525 setMetaString (const std::string& pattern, const std::string& name,
526 const std::string& value)
527 {
528 // no invariant used
529
530 std::regex mypattern (pattern);
531
532 for (iterator sample = begin(),
533 end2 = end(); sample != end2; ++ sample)
534 {
535 if (RCU::match_expr (mypattern, (*sample)->name()))
536 (*sample)->meta()->setString (name, value);
537 };
538 }
539
540
541
542 SampleHandler::iterator SampleHandler ::
543 begin () const
544 {
545 RCU_READ_INVARIANT (this);
546 return m_samples.begin();
547 }
548
549
550
551 SampleHandler::iterator SampleHandler ::
552 end () const
553 {
554 RCU_READ_INVARIANT (this);
555 return m_samples.end();
556 }
557
558
559
560 std::size_t SampleHandler ::
561 size () const
562 {
563 RCU_READ_INVARIANT (this);
564 return m_samples.size();
565 }
566
567
568
569 Sample *SampleHandler ::
570 operator [] (std::size_t index) const
571 {
572 // no invariant used
573 return at (index);
574 }
575
576
577
578 Sample *SampleHandler ::
579 at (std::size_t index) const
580 {
581 RCU_READ_INVARIANT (this);
583 return m_samples[index];
584 }
585
586
587
588 void SampleHandler ::
589 Streamer (TBuffer& b)
590 {
591 if (b.IsReading())
592 {
594 SampleHandler sh;
595 ULong_t count = 0;
596 b.ReadULong (count);
597 for (ULong_t iter = 0; iter != count; ++ iter)
598 {
599 Sample *sample = 0;
600 b >> sample;
601 sh.add (sample);
602 }
603 swap (*this, sh);
604 } else
605 {
606 RCU_READ_INVARIANT (this);
607 ULong_t count = m_samples.size(), count2 = 0;
608 b.WriteULong (count);
609 for (SamplesIter iter = m_samples.begin(),
610 end = m_samples.end(); iter != end; ++ iter, ++ count2)
611 {
612 Sample *sample = *iter;
613 b << sample;
614 }
615 };
616 }
617}
#define RCU_ASSERT(x)
Definition Assert.h:222
#define RCU_DESTROY_INVARIANT(x)
Definition Assert.h:235
#define RCU_CHANGE_INVARIANT(x)
Definition Assert.h:231
#define RCU_NEW_INVARIANT(x)
Definition Assert.h:233
#define RCU_REQUIRE_SOFT(x)
Definition Assert.h:153
#define RCU_READ_INVARIANT(x)
Definition Assert.h:229
void swap(DataVector< T > &a, DataVector< T > &b)
See DataVector<T, BASE>::swap().
static Double_t a
#define RCU_THROW_MSG(message)
Definition PrintMsg.h:58
void print(char *figname, TCanvas *c1)
bool add(const std::string &hname, TKey *tobj)
Definition fastadd.cxx:55
std::vector< std::string > tags
Definition hcg.cxx:105
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition hcg.cxx:130
std::string find(const std::string &s)
return a remapped string
Definition hcg.cxx:138
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:146
CONT to(RANGE &&r)
Definition ranges.h:39
str directory
Definition DeMoScan.py:78
void check_root_version()
effects: check whether we are using a consistent root version guarantee: strong failures: version mis...
bool match_expr(const std::regex &expr, const std::string &str)
returns: whether we can match the entire string with the regular expression guarantee: strong failure...
Definition index.py:1
TFile * file