ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/FGolf/Tools/histio.cc
Revision: 1.1
Committed: Mon Nov 8 03:16:15 2010 UTC (14 years, 5 months ago) by fgolf
Content type: text/plain
Branch: MAIN
CVS Tags: ss_summer2011approvalV2, ss_summer2011approval, synchMay2011v1, ss20May2011, HEAD
Log Message:
tool to load and save histograms

File Contents

# User Rev Content
1 fgolf 1.1 #include "TList.h"
2     #include "TIterator.h"
3     #include "TRegexp.h"
4     #include "TFile.h"
5     #include "TObject.h"
6     #include "TKey.h"
7     #include "TH1.h"
8     #include "TStyle.h"
9     #include "TClass.h"
10    
11     #include <iostream>
12    
13     #include "histio.h"
14    
15     void saveHist(const char* filename, const char* pat) {
16     TList* list = gDirectory->GetList();
17     TIterator* iter = list->MakeIterator();
18    
19     TRegexp re(pat, kTRUE);
20    
21     TFile outf(filename, "RECREATE");
22     TObject* obj;
23     while (obj = iter->Next()) {
24     if (TString(obj->GetName()).Index(re) >= 0) {
25     obj->Write() ;
26     std::cout << "." ;
27     std::cout.flush() ;
28     }
29     }
30    
31     std::cout << std::endl ;
32     outf.Close() ;
33    
34     delete iter ;
35     }
36    
37    
38     void loadHist(const char* filename, const char* pfx, const char* pat, Bool_t doAdd) {
39     TFile inf(filename);
40     TList* list = inf.GetListOfKeys();
41     TIterator* iter = list->MakeIterator();
42    
43     TRegexp re(pat, kTRUE);
44     std::cout << "pat = " << pat << std::endl;
45    
46     gDirectory->cd("Rint:");
47    
48     TObject* obj;
49     TKey* key;
50     std::cout << "doAdd = " << (doAdd ? "T" : "F") << std::endl;
51     std::cout << "loadHist: reading.";
52     while (key = (TKey*)iter->Next()) {
53    
54     Int_t ridx = TString(key->GetName()).Index(re);
55     if (ridx == -1)
56     continue;
57    
58     obj = inf.Get(key->GetName());
59     TObject* clone;
60     if (pfx) {
61    
62     // Find existing TH1-derived objects
63     TObject* oldObj = 0 ;
64     if (doAdd) {
65     oldObj = gDirectory->Get(Form("%s_%s", pfx, obj->GetName()));
66     if (oldObj && !oldObj->IsA()->InheritsFrom(TH1::Class()))
67     oldObj = 0;
68     }
69     if (oldObj) {
70     clone = oldObj;
71     ((TH1*)clone)->Add((TH1*)obj);
72     } else
73     clone = obj->Clone(Form("%s_%s", pfx, obj->GetName()));
74     } else {
75    
76     // Find existing TH1-derived objects
77     TObject* oldObj = 0;
78     if (doAdd) {
79     oldObj = gDirectory->Get(key->GetName());
80     if (oldObj && !oldObj->IsA()->InheritsFrom(TH1::Class()))
81     oldObj = 0;
82     }
83     if (oldObj) {
84     clone = oldObj;
85     ((TH1*)clone)->Add((TH1*)obj);
86     } else
87     clone = obj->Clone();
88     }
89     if (!gDirectory->GetList()->FindObject(clone))
90     gDirectory->Append(clone);
91    
92     std::cout << ".";
93     std::cout.flush();
94     }
95    
96     std::cout << std::endl;
97     inf.Close();
98     delete iter;
99     }