forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRooDataSet.cxx
More file actions
2059 lines (1669 loc) · 73 KB
/
RooDataSet.cxx
File metadata and controls
2059 lines (1669 loc) · 73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
* Project: RooFit *
* Package: RooFitCore *
* @(#)root/roofitcore:$Id$
* Authors: *
* WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
* DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
* *
* Copyright (c) 2000-2005, Regents of the University of California *
* and Stanford University. All rights reserved. *
* *
* Redistribution and use in source and binary forms, *
* with or without modification, are permitted according to the terms *
* listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
*****************************************************************************/
/**
\file RooDataSet.cxx
\class RooDataSet
\ingroup Roofitcore
RooDataSet is a container class to hold unbinned data. Each data point
in N-dimensional space is represented by a RooArgSet of RooRealVar, RooCategory
or RooStringVar objects.
There are two storage backends:
- RooVectorDataStore (default): std::vectors in memory. They are fast, but they
cannot be serialised if the dataset exceeds a size of 1 Gb
- RooTreeDataStore: Uses a TTree, which can be file backed if a file is opened
before creating the dataset. This significantly reduces the memory pressure, as the
baskets of the tree can be written to a file, and only the basket that's currently
being read stays in RAM.
- Enable tree-backed storage similar to this:
```
TFile outputFile("filename.root", "RECREATE");
RooAbsData::setDefaultStorageType(RooAbsData::Tree);
RooDataSet mydata(...);
```
- Or convert an existing memory-backed data storage:
```
RooDataSet mydata(...);
TFile outputFile("filename.root", "RECREATE");
mydata.convertToTreeStore();
```
For the inverse conversion, see `RooAbsData::convertToVectorStore()`.
**/
#include "RooDataSet.h"
#include "Riostream.h"
#include "RooPlot.h"
#include "RooAbsReal.h"
#include "Roo1DTable.h"
#include "RooCategory.h"
#include "RooFormulaVar.h"
#include "RooArgList.h"
#include "RooAbsRealLValue.h"
#include "RooRealVar.h"
#include "RooDataHist.h"
#include "RooMsgService.h"
#include "RooCmdConfig.h"
#include "RooHist.h"
#include "RooTreeDataStore.h"
#include "RooVectorDataStore.h"
#include "RooCompositeDataStore.h"
#include "RooSentinel.h"
#include "RooTrace.h"
#include "RooHelpers.h"
#include "TTree.h"
#include "TH2.h"
#include "TDirectory.h"
#include "TROOT.h"
#include "TFile.h"
#include "TBuffer.h"
#include "ROOT/RMakeUnique.hxx"
#include <fstream>
#if (__GNUC__==3&&__GNUC_MINOR__==2&&__GNUC_PATCHLEVEL__==3)
char* operator+( streampos&, char* );
#endif
using namespace std;
ClassImp(RooDataSet);
#ifndef USEMEMPOOLFORDATASET
void RooDataSet::cleanup() {}
#else
#include "MemPoolForRooSets.h"
RooDataSet::MemPool* RooDataSet::memPool() {
RooSentinel::activate();
static auto * memPool = new RooDataSet::MemPool();
return memPool;
}
void RooDataSet::cleanup() {
auto pool = memPool();
pool->teardown();
//The pool will have to leak if it's not empty at this point.
if (pool->empty())
delete pool;
}
////////////////////////////////////////////////////////////////////////////////
/// Overloaded new operator guarantees that all RooDataSets allocated with new
/// have a unique address, a property that is exploited in several places
/// in roofit to quickly index contents on normalization set pointers.
/// The memory pool only allocates space for the class itself. The elements
/// stored in the set are stored outside the pool.
void* RooDataSet::operator new (size_t bytes)
{
//This will fail if a derived class uses this operator
assert(sizeof(RooDataSet) == bytes);
return memPool()->allocate(bytes);
}
////////////////////////////////////////////////////////////////////////////////
/// Memory is owned by pool, we need to do nothing to release it
void RooDataSet::operator delete (void* ptr)
{
// Decrease use count in pool that ptr is on
if (memPool()->deallocate(ptr))
return;
std::cerr << __func__ << " " << ptr << " is not in any of the pools." << std::endl;
// Not part of any pool; use global op delete:
::operator delete(ptr);
}
#endif
////////////////////////////////////////////////////////////////////////////////
/// Default constructor for persistence
RooDataSet::RooDataSet() : _wgtVar(0)
{
TRACE_CREATE
}
////////////////////////////////////////////////////////////////////////////////
/// Construct an unbinned dataset from a RooArgSet defining the dimensions of the data space. Optionally, data
/// can be imported at the time of construction.
///
/// <table>
/// <tr><th> %RooCmdArg <th> Effect
/// <tr><td> Import(TTree*) <td> Import contents of given TTree. Only braches of the TTree that have names
/// corresponding to those of the RooAbsArgs that define the RooDataSet are
/// imported.
/// <tr><td> ImportFromFile(const char* fileName, const char* treeName) <td> Import tree with given name from file with given name.
/// <tr><td> Import(RooDataSet&)
/// <td> Import contents of given RooDataSet. Only observables that are common with the definition of this dataset will be imported
/// <tr><td> Index(RooCategory&) <td> Prepare import of datasets into a N+1 dimensional RooDataSet
/// where the extra discrete dimension labels the source of the imported histogram.
/// <tr><td> Import(const char*, RooDataSet&)
/// <td> Import a dataset to be associated with the given state name of the index category
/// specified in Index(). If the given state name is not yet defined in the index
/// category it will be added on the fly. The import command can be specified multiple times.
/// <tr><td> Link(const char*, RooDataSet&) <td> Link contents of supplied RooDataSet to this dataset for given index category state name.
/// In this mode, no data is copied and the linked dataset must be remain live for the duration
/// of this dataset. Note that link is active for both reading and writing, so modifications
/// to the aggregate dataset will also modify its components. Link() and Import() are mutually exclusive.
/// <tr><td> OwnLinked() <td> Take ownership of all linked datasets
/// <tr><td> Import(map<string,RooDataSet*>&) <td> As above, but allows specification of many imports in a single operation
/// <tr><td> Link(map<string,RooDataSet*>&) <td> As above, but allows specification of many links in a single operation
/// <tr><td> Cut(const char*) <br>
/// Cut(RooFormulaVar&)
/// <td> Apply the given cut specification when importing data
/// <tr><td> CutRange(const char*) <td> Only accept events in the observable range with the given name
/// <tr><td> WeightVar(const char*) <br>
/// WeightVar(const RooAbsArg&)
/// <td> Interpret the given variable as event weight rather than as observable
/// <tr><td> StoreError(const RooArgSet&) <td> Store symmetric error along with value for given subset of observables
/// <tr><td> StoreAsymError(const RooArgSet&) <td> Store asymmetric error along with value for given subset of observables
/// </table>
///
RooDataSet::RooDataSet(const char* name, const char* title, const RooArgSet& vars, const RooCmdArg& arg1, const RooCmdArg& arg2, const RooCmdArg& arg3,
const RooCmdArg& arg4,const RooCmdArg& arg5,const RooCmdArg& arg6,const RooCmdArg& arg7,const RooCmdArg& arg8) :
RooAbsData(name,title,RooArgSet(vars,(RooAbsArg*)RooCmdConfig::decodeObjOnTheFly("RooDataSet::RooDataSet", "IndexCat",0,0,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8)))
{
// Define configuration for this method
RooCmdConfig pc(Form("RooDataSet::ctor(%s)",GetName())) ;
pc.defineInt("ownLinked","OwnLinked",0) ;
pc.defineObject("impTree","ImportTree",0) ;
pc.defineObject("impData","ImportData",0) ;
pc.defineObject("indexCat","IndexCat",0) ;
pc.defineObject("impSliceData","ImportDataSlice",0,0,kTRUE) ; // array
pc.defineString("impSliceState","ImportDataSlice",0,"",kTRUE) ; // array
pc.defineObject("lnkSliceData","LinkDataSlice",0,0,kTRUE) ; // array
pc.defineString("lnkSliceState","LinkDataSlice",0,"",kTRUE) ; // array
pc.defineString("cutSpec","CutSpec",0,"") ;
pc.defineObject("cutVar","CutVar",0) ;
pc.defineString("cutRange","CutRange",0,"") ;
pc.defineString("wgtVarName","WeightVarName",0,"") ;
pc.defineInt("newWeight1","WeightVarName",0,0) ;
pc.defineString("fname","ImportFromFile",0,"") ;
pc.defineString("tname","ImportFromFile",1,"") ;
pc.defineObject("wgtVar","WeightVar",0) ;
pc.defineInt("newWeight2","WeightVar",0,0) ;
pc.defineObject("dummy1","ImportDataSliceMany",0) ;
pc.defineObject("dummy2","LinkDataSliceMany",0) ;
pc.defineSet("errorSet","StoreError",0) ;
pc.defineSet("asymErrSet","StoreAsymError",0) ;
pc.defineMutex("ImportTree","ImportData","ImportDataSlice","LinkDataSlice","ImportFromFile") ;
pc.defineMutex("CutSpec","CutVar") ;
pc.defineMutex("WeightVarName","WeightVar") ;
pc.defineDependency("ImportDataSlice","IndexCat") ;
pc.defineDependency("LinkDataSlice","IndexCat") ;
pc.defineDependency("OwnLinked","LinkDataSlice") ;
RooLinkedList l ;
l.Add((TObject*)&arg1) ; l.Add((TObject*)&arg2) ;
l.Add((TObject*)&arg3) ; l.Add((TObject*)&arg4) ;
l.Add((TObject*)&arg5) ; l.Add((TObject*)&arg6) ;
l.Add((TObject*)&arg7) ; l.Add((TObject*)&arg8) ;
// Process & check varargs
pc.process(l) ;
if (!pc.ok(kTRUE)) {
assert(0) ;
return ;
}
// Extract relevant objects
TTree* impTree = static_cast<TTree*>(pc.getObject("impTree")) ;
RooDataSet* impData = static_cast<RooDataSet*>(pc.getObject("impData")) ;
RooFormulaVar* cutVar = static_cast<RooFormulaVar*>(pc.getObject("cutVar")) ;
const char* cutSpec = pc.getString("cutSpec","",kTRUE) ;
const char* cutRange = pc.getString("cutRange","",kTRUE) ;
const char* wgtVarName = pc.getString("wgtVarName","",kTRUE) ;
RooRealVar* wgtVar = static_cast<RooRealVar*>(pc.getObject("wgtVar")) ;
const char* impSliceNames = pc.getString("impSliceState","",kTRUE) ;
const RooLinkedList& impSliceData = pc.getObjectList("impSliceData") ;
const char* lnkSliceNames = pc.getString("lnkSliceState","",kTRUE) ;
const RooLinkedList& lnkSliceData = pc.getObjectList("lnkSliceData") ;
RooCategory* indexCat = static_cast<RooCategory*>(pc.getObject("indexCat")) ;
RooArgSet* errorSet = pc.getSet("errorSet") ;
RooArgSet* asymErrorSet = pc.getSet("asymErrSet") ;
const char* fname = pc.getString("fname") ;
const char* tname = pc.getString("tname") ;
Int_t ownLinked = pc.getInt("ownLinked") ;
Int_t newWeight = pc.getInt("newWeight1") + pc.getInt("newWeight2") ;
// Case 1 --- Link multiple dataset as slices
if (lnkSliceNames) {
// Make import mapping if index category is specified
map<string,RooAbsData*> hmap ;
if (indexCat) {
char tmp[64000];
strlcpy(tmp, lnkSliceNames, 64000);
char *token = strtok(tmp, ",");
TIterator *hiter = lnkSliceData.MakeIterator();
while (token) {
hmap[token] = (RooAbsData *)hiter->Next();
token = strtok(0, ",");
}
delete hiter ;
}
// Lookup name of weight variable if it was specified by object reference
if (wgtVar) {
// coverity[UNUSED_VALUE]
wgtVarName = wgtVar->GetName() ;
}
appendToDir(this,kTRUE) ;
// Initialize RooDataSet with optional weight variable
initialize(0) ;
map<string,RooAbsDataStore*> storeMap ;
RooCategory* icat = (RooCategory*) (indexCat ? _vars.find(indexCat->GetName()) : 0 ) ;
if (!icat) {
throw std::string("RooDataSet::RooDataSet() ERROR in constructor, cannot find index category") ;
}
for (map<string,RooAbsData*>::iterator hiter = hmap.begin() ; hiter!=hmap.end() ; ++hiter) {
// Define state labels in index category (both in provided indexCat and in internal copy in dataset)
if (indexCat && !indexCat->hasLabel(hiter->first)) {
indexCat->defineType(hiter->first) ;
coutI(InputArguments) << "RooDataSet::ctor(" << GetName() << ") defining state \"" << hiter->first << "\" in index category " << indexCat->GetName() << endl ;
}
if (icat && !icat->hasLabel(hiter->first)) {
icat->defineType(hiter->first) ;
}
icat->setLabel(hiter->first.c_str()) ;
storeMap[icat->getCurrentLabel()]=hiter->second->store() ;
// Take ownership of slice if requested
if (ownLinked) {
addOwnedComponent(hiter->first.c_str(),*hiter->second) ;
}
}
// Create composite datastore
_dstore = new RooCompositeDataStore(name,title,_vars,*icat,storeMap) ;
} else {
if (wgtVar) {
wgtVarName = wgtVar->GetName() ;
}
// Clone weight variable of imported dataset if we are not weighted
if (!wgtVar && !wgtVarName && impData && impData->_wgtVar) {
_wgtVar = (RooRealVar*) impData->_wgtVar->createFundamental() ;
_vars.addOwned(*_wgtVar) ;
wgtVarName = _wgtVar->GetName() ;
}
// Create empty datastore
RooTreeDataStore* tstore(0) ;
RooVectorDataStore* vstore(0) ;
if (defaultStorageType==Tree) {
tstore = new RooTreeDataStore(name,title,_vars,wgtVarName) ;
_dstore = tstore ;
} else if (defaultStorageType==Vector) {
if (wgtVarName && newWeight) {
RooAbsArg* wgttmp = _vars.find(wgtVarName) ;
if (wgttmp) {
wgttmp->setAttribute("NewWeight") ;
}
}
vstore = new RooVectorDataStore(name,title,_vars,wgtVarName) ;
_dstore = vstore ;
} else {
_dstore = 0 ;
}
// Make import mapping if index category is specified
map<string,RooDataSet*> hmap ;
if (indexCat) {
char tmp[100000] ;
strlcpy(tmp,impSliceNames,100000) ;
char* token = strtok(tmp,",") ;
TIterator* hiter = impSliceData.MakeIterator() ;
while(token) {
hmap[token] = (RooDataSet*) hiter->Next() ;
token = strtok(0,",") ;
}
delete hiter ;
}
// process StoreError requests
if (errorSet) {
RooArgSet* intErrorSet = (RooArgSet*) _vars.selectCommon(*errorSet) ;
intErrorSet->setAttribAll("StoreError") ;
TIterator* iter = intErrorSet->createIterator() ;
RooAbsArg* arg ;
while((arg=(RooAbsArg*)iter->Next())) {
arg->attachToStore(*_dstore) ;
}
delete iter ;
delete intErrorSet ;
}
if (asymErrorSet) {
RooArgSet* intAsymErrorSet = (RooArgSet*) _vars.selectCommon(*asymErrorSet) ;
intAsymErrorSet->setAttribAll("StoreAsymError") ;
TIterator* iter = intAsymErrorSet->createIterator() ;
RooAbsArg* arg ;
while((arg=(RooAbsArg*)iter->Next())) {
arg->attachToStore(*_dstore) ;
}
delete iter ;
delete intAsymErrorSet ;
}
// Lookup name of weight variable if it was specified by object reference
if (wgtVar) {
wgtVarName = wgtVar->GetName() ;
}
appendToDir(this,kTRUE) ;
// Initialize RooDataSet with optional weight variable
if (wgtVarName && *wgtVarName) {
// Use the supplied weight column
initialize(wgtVarName) ;
} else {
if (impData && impData->_wgtVar && vars.find(impData->_wgtVar->GetName())) {
// Use the weight column of the source data set
initialize(impData->_wgtVar->GetName()) ;
} else if (indexCat) {
RooDataSet* firstDS = hmap.begin()->second ;
if (firstDS->_wgtVar && vars.find(firstDS->_wgtVar->GetName())) {
initialize(firstDS->_wgtVar->GetName()) ;
} else {
initialize(0) ;
}
} else {
initialize(0) ;
}
}
// Import one or more datasets with a cut specification
if (cutSpec && *cutSpec) {
// Create a RooFormulaVar cut from given cut expression
if (indexCat) {
// Case 2a --- Import multiple RooDataSets as slices with cutspec
RooCategory* icat = (RooCategory*) _vars.find(indexCat->GetName()) ;
for (map<string,RooDataSet*>::iterator hiter = hmap.begin() ; hiter!=hmap.end() ; ++hiter) {
// Define state labels in index category (both in provided indexCat and in internal copy in dataset)
if (!indexCat->hasLabel(hiter->first)) {
indexCat->defineType(hiter->first) ;
coutI(InputArguments) << "RooDataSet::ctor(" << GetName() << ") defining state \"" << hiter->first << "\" in index category " << indexCat->GetName() << endl ;
}
if (!icat->hasLabel(hiter->first)) {
icat->defineType(hiter->first) ;
}
icat->setLabel(hiter->first.c_str()) ;
RooFormulaVar cutVarTmp(cutSpec,cutSpec,hiter->second->_vars) ;
_dstore->loadValues(hiter->second->store(),&cutVarTmp,cutRange) ;
}
} else if (impData) {
// Case 3a --- Import RooDataSet with cutspec
RooFormulaVar cutVarTmp(cutSpec,cutSpec,impData->_vars) ;
_dstore->loadValues(impData->store(),&cutVarTmp,cutRange);
} else if (impTree) {
// Case 4a --- Import TTree from memory with cutspec
RooFormulaVar cutVarTmp(cutSpec,cutSpec,_vars) ;
if (tstore) {
tstore->loadValues(impTree,&cutVarTmp,cutRange);
} else {
RooTreeDataStore tmpstore(name,title,_vars,wgtVarName) ;
tmpstore.loadValues(impTree,&cutVarTmp,cutRange) ;
_dstore->append(tmpstore) ;
}
} else if (fname && strlen(fname)) {
// Case 5a --- Import TTree from file with cutspec
TFile *f = TFile::Open(fname) ;
if (!f) {
coutE(InputArguments) << "RooDataSet::ctor(" << GetName() << ") ERROR file '" << fname << "' cannot be opened or does not exist" << endl ;
throw string(Form("RooDataSet::ctor(%s) ERROR file %s cannot be opened or does not exist",GetName(),fname)) ;
}
TTree* t = dynamic_cast<TTree*>(f->Get(tname)) ;
if (!t) {
coutE(InputArguments) << "RooDataSet::ctor(" << GetName() << ") ERROR file '" << fname << "' does not contain a TTree named '" << tname << "'" << endl ;
throw string(Form("RooDataSet::ctor(%s) ERROR file %s does not contain a TTree named %s",GetName(),fname,tname)) ;
}
RooFormulaVar cutVarTmp(cutSpec,cutSpec,_vars) ;
if (tstore) {
tstore->loadValues(t,&cutVarTmp,cutRange);
} else {
RooTreeDataStore tmpstore(name,title,_vars,wgtVarName) ;
tmpstore.loadValues(t,&cutVarTmp,cutRange) ;
_dstore->append(tmpstore) ;
}
f->Close() ;
}
// Import one or more datasets with a cut formula
} else if (cutVar) {
if (indexCat) {
// Case 2b --- Import multiple RooDataSets as slices with cutvar
RooCategory* icat = (RooCategory*) _vars.find(indexCat->GetName()) ;
for (map<string,RooDataSet*>::iterator hiter = hmap.begin() ; hiter!=hmap.end() ; ++hiter) {
// Define state labels in index category (both in provided indexCat and in internal copy in dataset)
if (!indexCat->hasLabel(hiter->first)) {
indexCat->defineType(hiter->first) ;
coutI(InputArguments) << "RooDataSet::ctor(" << GetName() << ") defining state \"" << hiter->first << "\" in index category " << indexCat->GetName() << endl ;
}
if (!icat->hasLabel(hiter->first)) {
icat->defineType(hiter->first) ;
}
icat->setLabel(hiter->first.c_str()) ;
_dstore->loadValues(hiter->second->store(),cutVar,cutRange) ;
}
} else if (impData) {
// Case 3b --- Import RooDataSet with cutvar
_dstore->loadValues(impData->store(),cutVar,cutRange);
} else if (impTree) {
// Case 4b --- Import TTree from memory with cutvar
if (tstore) {
tstore->loadValues(impTree,cutVar,cutRange);
} else {
RooTreeDataStore tmpstore(name,title,_vars,wgtVarName) ;
tmpstore.loadValues(impTree,cutVar,cutRange) ;
_dstore->append(tmpstore) ;
}
} else if (fname && strlen(fname)) {
// Case 5b --- Import TTree from file with cutvar
TFile *f = TFile::Open(fname) ;
if (!f) {
coutE(InputArguments) << "RooDataSet::ctor(" << GetName() << ") ERROR file '" << fname << "' cannot be opened or does not exist" << endl ;
throw string(Form("RooDataSet::ctor(%s) ERROR file %s cannot be opened or does not exist",GetName(),fname)) ;
}
TTree* t = dynamic_cast<TTree*>(f->Get(tname)) ;
if (!t) {
coutE(InputArguments) << "RooDataSet::ctor(" << GetName() << ") ERROR file '" << fname << "' does not contain a TTree named '" << tname << "'" << endl ;
throw string(Form("RooDataSet::ctor(%s) ERROR file %s does not contain a TTree named %s",GetName(),fname,tname)) ;
}
if (tstore) {
tstore->loadValues(t,cutVar,cutRange);
} else {
RooTreeDataStore tmpstore(name,title,_vars,wgtVarName) ;
tmpstore.loadValues(t,cutVar,cutRange) ;
_dstore->append(tmpstore) ;
}
f->Close() ;
}
// Import one or more datasets without cuts
} else {
if (indexCat) {
RooCategory* icat = (RooCategory*) _vars.find(indexCat->GetName()) ;
for (map<string,RooDataSet*>::iterator hiter = hmap.begin() ; hiter!=hmap.end() ; ++hiter) {
// Define state labels in index category (both in provided indexCat and in internal copy in dataset)
if (!indexCat->hasLabel(hiter->first)) {
indexCat->defineType(hiter->first) ;
coutI(InputArguments) << "RooDataSet::ctor(" << GetName() << ") defining state \"" << hiter->first << "\" in index category " << indexCat->GetName() << endl ;
}
if (!icat->hasLabel(hiter->first)) {
icat->defineType(hiter->first) ;
}
icat->setLabel(hiter->first.c_str()) ;
// Case 2c --- Import multiple RooDataSets as slices
_dstore->loadValues(hiter->second->store(),0,cutRange) ;
}
} else if (impData) {
// Case 3c --- Import RooDataSet
_dstore->loadValues(impData->store(),0,cutRange);
} else if (impTree || (fname && strlen(fname))) {
// Case 4c --- Import TTree from memory / file
std::unique_ptr<TFile> file;
if (impTree == nullptr) {
file.reset(TFile::Open(fname));
if (!file) {
coutE(InputArguments) << "RooDataSet::ctor(" << GetName() << ") ERROR file '" << fname << "' cannot be opened or does not exist" << endl ;
throw std::invalid_argument(Form("RooDataSet::ctor(%s) ERROR file %s cannot be opened or does not exist",GetName(),fname)) ;
}
file->GetObject(tname, impTree);
if (!impTree) {
coutE(InputArguments) << "RooDataSet::ctor(" << GetName() << ") ERROR file '" << fname << "' does not contain a TTree named '" << tname << "'" << endl ;
throw std::invalid_argument(Form("RooDataSet::ctor(%s) ERROR file %s does not contain a TTree named %s",GetName(),fname,tname)) ;
}
}
if (tstore) {
tstore->loadValues(impTree,0,cutRange);
} else {
RooTreeDataStore tmpstore(name,title,_vars,wgtVarName) ;
tmpstore.loadValues(impTree,0,cutRange) ;
_dstore->append(tmpstore) ;
}
}
}
}
TRACE_CREATE
}
////////////////////////////////////////////////////////////////////////////////
/// Constructor of an empty data set from a RooArgSet defining the dimensions
/// of the data space.
RooDataSet::RooDataSet(const char *name, const char *title, const RooArgSet& vars, const char* wgtVarName) :
RooAbsData(name,title,vars)
{
// cout << "RooDataSet::ctor(" << this << ") storageType = " << ((defaultStorageType==Tree)?"Tree":"Vector") << endl ;
_dstore = (defaultStorageType==Tree) ? ((RooAbsDataStore*) new RooTreeDataStore(name,title,_vars,wgtVarName)) :
((RooAbsDataStore*) new RooVectorDataStore(name,title,_vars,wgtVarName)) ;
appendToDir(this,kTRUE) ;
initialize(wgtVarName) ;
TRACE_CREATE
}
////////////////////////////////////////////////////////////////////////////////
/// Constructor of a data set from (part of) an existing data
/// set. The dimensions of the data set are defined by the 'vars'
/// RooArgSet, which can be identical to 'dset' dimensions, or a
/// subset thereof. The 'cuts' string is an optional RooFormula
/// expression and can be used to select the subset of the data
/// points in 'dset' to be copied. The cut expression can refer to
/// any variable in the source dataset. For cuts involving variables
/// other than those contained in the source data set, such as
/// intermediate formula objects, use the equivalent constructor
/// accepting RooFormulaVar reference as cut specification.
///
/// This constructor will internally store the data in a TTree.
///
/// For most uses the RooAbsData::reduce() wrapper function, which
/// uses this constructor, is the most convenient way to create a
/// subset of an existing data
///
RooDataSet::RooDataSet(const char *name, const char *title, RooDataSet *dset,
const RooArgSet& vars, const char *cuts, const char* wgtVarName) :
RooAbsData(name,title,vars)
{
// Initialize datastore
_dstore = new RooTreeDataStore(name,title,_vars,*dset->_dstore,cuts,wgtVarName) ;
appendToDir(this,kTRUE) ;
if (wgtVarName) {
// Use the supplied weight column
initialize(wgtVarName) ;
} else {
if (dset->_wgtVar && vars.find(dset->_wgtVar->GetName())) {
// Use the weight column of the source data set
initialize(dset->_wgtVar->GetName()) ;
} else {
initialize(0) ;
}
}
TRACE_CREATE
}
////////////////////////////////////////////////////////////////////////////////
/// Constructor of a data set from (part of) an existing data
/// set. The dimensions of the data set are defined by the 'vars'
/// RooArgSet, which can be identical to 'dset' dimensions, or a
/// subset thereof. The 'cutVar' formula variable is used to select
/// the subset of data points to be copied. For subsets without
/// selection on the data points, or involving cuts operating
/// exclusively and directly on the data set dimensions, the
/// equivalent constructor with a string based cut expression is
/// recommended.
///
/// This constructor will internally store the data in a TTree.
///
/// For most uses the RooAbsData::reduce() wrapper function, which
/// uses this constructor, is the most convenient way to create a
/// subset of an existing data
RooDataSet::RooDataSet(const char *name, const char *title, RooDataSet *dset,
const RooArgSet& vars, const RooFormulaVar& cutVar, const char* wgtVarName) :
RooAbsData(name,title,vars)
{
// Initialize datastore
_dstore = new RooTreeDataStore(name,title,_vars,*dset->_dstore,cutVar,wgtVarName) ;
appendToDir(this,kTRUE) ;
if (wgtVarName) {
// Use the supplied weight column
initialize(wgtVarName) ;
} else {
if (dset->_wgtVar && vars.find(dset->_wgtVar->GetName())) {
// Use the weight column of the source data set
initialize(dset->_wgtVar->GetName()) ;
} else {
initialize(0) ;
}
}
TRACE_CREATE
}
////////////////////////////////////////////////////////////////////////////////
/// Constructor of a data set from (part of) an ROOT TTRee. The dimensions
/// of the data set are defined by the 'vars' RooArgSet. For each dimension
/// specified, the TTree must have a branch with the same name. For category
/// branches, this branch should contain the numeric index value. Real dimensions
/// can be constructed from either 'Double_t' or 'Float_t' tree branches. In the
/// latter case, an automatic conversion is applied.
///
/// The 'cutVar' formula variable
/// is used to select the subset of data points to be copied.
/// For subsets without selection on the data points, or involving cuts
/// operating exclusively and directly on the data set dimensions, the equivalent
/// constructor with a string based cut expression is recommended.
RooDataSet::RooDataSet(const char *name, const char *title, TTree *theTree,
const RooArgSet& vars, const RooFormulaVar& cutVar, const char* wgtVarName) :
RooAbsData(name,title,vars)
{
// Create tree version of datastore
RooTreeDataStore* tstore = new RooTreeDataStore(name,title,_vars,*theTree,cutVar,wgtVarName) ;
// Convert to vector datastore if needed
if (defaultStorageType==Tree) {
_dstore = tstore ;
} else if (defaultStorageType==Vector) {
RooVectorDataStore* vstore = new RooVectorDataStore(name,title,_vars,wgtVarName) ;
_dstore = vstore ;
_dstore->append(*tstore) ;
delete tstore ;
} else {
_dstore = 0 ;
}
appendToDir(this,kTRUE) ;
initialize(wgtVarName) ;
TRACE_CREATE
}
////////////////////////////////////////////////////////////////////////////////
/// Constructor of a data set from (part of) a ROOT TTree.
///
/// \param[in] name Name of this dataset.
/// \param[in] title Title for e.g. plotting.
/// \param[in] tree Tree to be imported.
/// \param[in] vars Defines the columns of the data set. For each dimension
/// specified, the TTree must have a branch with the same name. For category
/// branches, this branch should contain the numeric index value. Real dimensions
/// can be constructed from either 'Double_t' or 'Float_t' tree branches. In the
/// latter case, an automatic conversion is applied.
/// \param[in] cuts Optional RooFormula expression to select the subset of the data points
/// to be imported. The cut expression can refer to any variable in `vars`.
/// \warning The expression only evaluates variables that are also in `vars`.
/// Passing e.g.
/// ```
/// RooDataSet("data", "data", tree, RooArgSet(x), "x>y")
/// ```
/// Will load `x` from the tree, but leave `y` at an undefined value.
/// If other expressions are needed, such as intermediate formula objects, use
/// RooDataSet::RooDataSet(const char*,const char*,TTree*,const RooArgSet&,const RooFormulaVar&,const char*)
/// \param[in] wgtVarName Name of the variable in `vars` that represents an event weight.
RooDataSet::RooDataSet(const char* name, const char* title, TTree* theTree,
const RooArgSet& vars, const char* cuts, const char* wgtVarName) :
RooAbsData(name,title,vars)
{
// Create tree version of datastore
RooTreeDataStore* tstore = new RooTreeDataStore(name,title,_vars,*theTree,cuts,wgtVarName);
// Convert to vector datastore if needed
if (defaultStorageType==Tree) {
_dstore = tstore ;
} else if (defaultStorageType==Vector) {
RooVectorDataStore* vstore = new RooVectorDataStore(name,title,_vars,wgtVarName) ;
_dstore = vstore ;
_dstore->append(*tstore) ;
delete tstore ;
} else {
_dstore = 0 ;
}
appendToDir(this,kTRUE) ;
initialize(wgtVarName) ;
TRACE_CREATE
}
////////////////////////////////////////////////////////////////////////////////
/// Copy constructor
RooDataSet::RooDataSet(RooDataSet const & other, const char* newname) :
RooAbsData(other,newname), RooDirItem()
{
appendToDir(this,kTRUE) ;
initialize(other._wgtVar?other._wgtVar->GetName():0) ;
TRACE_CREATE
}
////////////////////////////////////////////////////////////////////////////////
/// Protected constructor for internal use only
RooDataSet::RooDataSet(const char *name, const char *title, RooDataSet *dset,
const RooArgSet& vars, const RooFormulaVar* cutVar, const char* cutRange,
std::size_t nStart, std::size_t nStop, Bool_t copyCache, const char* wgtVarName) :
RooAbsData(name,title,vars)
{
_dstore =
(defaultStorageType == Tree)
? ((RooAbsDataStore *)new RooTreeDataStore(name, title, *dset->_dstore, _vars, cutVar, cutRange, nStart, nStop,
copyCache, wgtVarName))
: (
// ( dset->_dstore->IsA()==RooCompositeDataStore::Class() )?
// ((RooAbsDataStore*) new
// RooCompositeDataStore(name,title,(RooCompositeDataStore&)(*dset->_dstore),_vars,cutVar,cutRange,nStart,nStop,copyCache,wgtVarName))
// :
((RooAbsDataStore *)new RooVectorDataStore(name, title, *dset->_dstore, _vars, cutVar, cutRange, nStart,
nStop, copyCache, wgtVarName)));
_cachedVars.add(_dstore->cachedVars());
appendToDir(this, kTRUE);
initialize(dset->_wgtVar ? dset->_wgtVar->GetName() : 0);
TRACE_CREATE
}
////////////////////////////////////////////////////////////////////////////////
/// Helper function for constructor that adds optional weight variable to construct
/// total set of observables
RooArgSet RooDataSet::addWgtVar(const RooArgSet& origVars, const RooAbsArg* wgtVar)
{
RooArgSet tmp(origVars) ;
if (wgtVar) tmp.add(*wgtVar) ;
return tmp ;
}
////////////////////////////////////////////////////////////////////////////////
/// Return a clone of this dataset containing only the cached variables
RooAbsData* RooDataSet::cacheClone(const RooAbsArg* newCacheOwner, const RooArgSet* newCacheVars, const char* newName)
{
RooDataSet* dset = new RooDataSet(newName?newName:GetName(),GetTitle(),this,_vars,(RooFormulaVar*)0,0,0,2000000000,kTRUE,_wgtVar?_wgtVar->GetName():0) ;
//if (_wgtVar) dset->setWeightVar(_wgtVar->GetName()) ;
RooArgSet* selCacheVars = (RooArgSet*) newCacheVars->selectCommon(dset->_cachedVars) ;
dset->attachCache(newCacheOwner, *selCacheVars) ;
delete selCacheVars ;
return dset ;
}
////////////////////////////////////////////////////////////////////////////////
/// Return an empty clone of this dataset. If vars is not null, only the variables in vars
/// are added to the definition of the empty clone
RooAbsData* RooDataSet::emptyClone(const char* newName, const char* newTitle, const RooArgSet* vars, const char* wgtVarName) const
{
// If variables are given, be sure to include weight variable if it exists and is not included
RooArgSet vars2 ;
RooRealVar* tmpWgtVar = _wgtVar ;
if (wgtVarName && vars && !_wgtVar) {
tmpWgtVar = (RooRealVar*) vars->find(wgtVarName) ;
}
if (vars) {
vars2.add(*vars) ;
if (_wgtVar && !vars2.find(_wgtVar->GetName())) {
vars2.add(*_wgtVar) ;
}
} else {
vars2.add(_vars) ;
}
RooDataSet* dset = new RooDataSet(newName?newName:GetName(),newTitle?newTitle:GetTitle(),vars2,tmpWgtVar?tmpWgtVar->GetName():0) ;
//if (_wgtVar) dset->setWeightVar(_wgtVar->GetName()) ;
return dset ;
}
////////////////////////////////////////////////////////////////////////////////
/// Initialize the dataset. If wgtVarName is not null, interpret the observable
/// with that name as event weight
void RooDataSet::initialize(const char* wgtVarName)
{
_varsNoWgt.removeAll() ;
_varsNoWgt.add(_vars) ;
_wgtVar = 0 ;
if (wgtVarName) {
RooAbsArg* wgt = _varsNoWgt.find(wgtVarName) ;
if (!wgt) {
coutW(DataHandling) << "RooDataSet::RooDataSet(" << GetName() << ") WARNING: designated weight variable "
<< wgtVarName << " not found in set of variables, no weighting will be assigned" << endl ;
} else if (!dynamic_cast<RooRealVar*>(wgt)) {
coutW(DataHandling) << "RooDataSet::RooDataSet(" << GetName() << ") WARNING: designated weight variable "
<< wgtVarName << " is not of type RooRealVar, no weighting will be assigned" << endl ;
} else {
_varsNoWgt.remove(*wgt) ;
_wgtVar = (RooRealVar*) wgt ;
}
}
}
////////////////////////////////////////////////////////////////////////////////
/// Implementation of RooAbsData virtual method that drives the RooAbsData::reduce() methods
RooAbsData* RooDataSet::reduceEng(const RooArgSet& varSubset, const RooFormulaVar* cutVar, const char* cutRange,
std::size_t nStart, std::size_t nStop, Bool_t copyCache)
{
checkInit() ;
//cout << "reduceEng varSubset = " << varSubset << " _wgtVar = " << (_wgtVar ? _wgtVar->GetName() : "") << endl;
RooArgSet tmp(varSubset) ;
if (_wgtVar) {
tmp.add(*_wgtVar) ;
}
RooDataSet* ret = new RooDataSet(GetName(), GetTitle(), this, tmp, cutVar, cutRange, nStart, nStop, copyCache,_wgtVar?_wgtVar->GetName():0) ;
// WVE - propagate optional weight variable
// check behaviour in plotting.
// if (_wgtVar) {
// ret->setWeightVar(_wgtVar->GetName()) ;
// }
return ret ;
}
////////////////////////////////////////////////////////////////////////////////
/// Destructor
RooDataSet::~RooDataSet()
{
removeFromDir(this) ;
TRACE_DESTROY
}
////////////////////////////////////////////////////////////////////////////////
/// Return binned clone of this dataset
RooDataHist* RooDataSet::binnedClone(const char* newName, const char* newTitle) const
{
TString title, name ;
if (newName) {
name = newName ;
} else {
name = Form("%s_binned",GetName()) ;
}
if (newTitle) {
title = newTitle ;
} else {
title = Form("%s_binned",GetTitle()) ;
}
return new RooDataHist(name,title,*get(),*this) ;
}
////////////////////////////////////////////////////////////////////////////////
/// Return event weight of current event
Double_t RooDataSet::weight() const
{
return store()->weight() ;
}
////////////////////////////////////////////////////////////////////////////////
/// Return squared event weight of current event
Double_t RooDataSet::weightSquared() const
{
return store()->weight()*store()->weight() ;
}
// See base class.
RooSpan<const double> RooDataSet::getWeightBatch(std::size_t first, std::size_t len) const {
return _dstore->getWeightBatch(first, len);