All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
flatten_caf.cc
Go to the documentation of this file.
3 #include "sbnanaobj/StandardRecord/Flat/FlatRecord.h"
4 
6 
7 #include "TFile.h"
8 #include "TH1.h"
9 #include "TSystem.h"
10 #include "TTree.h"
11 
12 #include <iostream>
13 
14 int main(int argc, char** argv)
15 {
16  if(argc != 3){
17  std::cout << "Usage: flatten_caf input.events.root output.flat.root"
18  << std::endl;
19  return 1;
20  }
21 
22  const std::string inname = argv[1];
23  const std::string outname = argv[2];
24 
25  TFile* fin = TFile::Open(inname.c_str());
26 
27  TTree* tr = (TTree*)fin->Get("recTree");
28  if(!tr){
29  std::cout << "Couldn't find tree 'recTree' in " << inname << std::endl;
30  return 1;
31  }
32 
33  caf::StandardRecord* event = 0;
34  tr->SetBranchAddress("rec", &event);
35 
36  // LZ4 is the fastest format to decompress. I get 3x faster loading with
37  // this compared to the default, and the files are only slightly larger.
38  TFile fout(outname.c_str(), "RECREATE", "",
39  ROOT::CompressionSettings(ROOT::kLZ4, 1));
40 
41  TTree* trout = new TTree("recTree", "recTree");
42  // On NOvA, had trouble with memory usage (because several trees are open at
43  // once?). Setting the maximum buffer usage (per tree) to 3MB (10x less than
44  // default) fixed it. But it doesn't seem necessary for now on SBN.
45  // trout->SetAutoFlush(-3*1000*1000);
46 
47  flat::Flat<caf::StandardRecord> rec(trout, "rec", "", 0);//policy);
48 
49  ana::Progress prog("Converting '"+inname+"' to '"+outname+"'");
50  for(int i = 0; i < tr->GetEntries(); ++i){
51  prog.SetProgress(double(i)/tr->GetEntries());
52 
53  tr->GetEntry(i);
54 
55  rec.Clear();
56  rec.Fill(*event);
57  trout->Fill();
58  }
59  prog.Done();
60 
61  trout->Write();
62 
63  // Don't bother with a flat version for now, this info is tiny and read once
64  TTree* globalIn = (TTree*)fin->Get("globalTree");
65  if(globalIn){
66  // Copy globalTree verbatim from input to output
67  caf::SRGlobal global;
68  caf::SRGlobal* pglobal = &global;
69  globalIn->SetBranchAddress("global", &pglobal);
70  fout.cd();
71  TTree globalOut("globalTree", "globalTree");
72  globalOut.Branch("global", "caf::SRGlobal", &global);
73  assert(globalIn->GetEntries() == 1);
74  // TODO check that the globalTree is the same in every file
75  globalIn->GetEntry(0);
76  globalOut.Fill();
77  globalOut.Write();
78  }
79 
80  TH1* hPOT = (TH1*)fin->Get("TotalPOT");
81  TH1* hEvts = (TH1*)fin->Get("TotalEvents");
82  fout.cd();
83  hPOT->Write("TotalPOT");
84  hEvts->Write("TotalEvents");
85 
86  TTree* metain = (TTree*)fin->Get("metadata/metatree");
87  if(metain){
88  TDirectory* metadir = fout.mkdir("metadata");
89  metadir->cd();
90  std::string key, value;
91  std::string *pkey = &key, *pvalue = &value;
92  TTree* metaout = new TTree("metatree", "metatree");
93  metaout->Branch("key", &key);
94  metaout->Branch("value", &value);
95  metain->SetBranchAddress("key", &pkey);
96  metain->SetBranchAddress("value", &pvalue);
97  for(int i = 0; i < metain->GetEntries(); ++i){
98  metain->GetEntry(i);
99 
100  if(key == "data_tier") value = "\"flat_caf\"";
101  if(key == "file_format") value = "\"flat_caf\"";
102 
103  metaout->Fill();
104  }
105  metaout->Write();
106  }
107 
108  return 0;
109 }
The StandardRecord is the primary top-level object in the Common Analysis File trees.
void SetProgress(double frac)
Update the progress fraction between zero and one.
Definition: Progress.cxx:44
A simple ascii-art progress bar.
Definition: Progress.h:9
temporary value
int main(int argc, char **argv)
prog
Definition: just_cmake.sh:3
void Done()
Call this when action is completed.
Definition: Progress.cxx:91
BEGIN_PROLOG could also be cout