babymaker  e95a6a9342d4604277fe7cc6149b6b5b24447d89
json_dataset.cxx
Go to the documentation of this file.
1 // json_ntuple: Writes out a JSON file with the runs and lumis in ntuple
2 
3 #include <ctime>
4 
5 #include <vector>
6 #include <fstream>
7 #include <iostream>
8 #include <set>
9 #include <map>
10 #include <unistd.h> // getopt
11 #include <iomanip> // setw
12 
13 #include "TString.h"
14 #include "TChain.h"
15 #include "TTree.h"
16 #include "TFile.h"
17 #include "TSystem.h"
18 
19 #include "utilities.hh"
20 
21 using namespace std;
22 
23 int main(int argc, char *argv[]){
24  time_t startTime, curTime;
25 
26  TString infolder(""), outfolder("out/"), tag("");
27  int c(0);
28  while((c=getopt(argc, argv, "f:i:t:"))!=-1){
29  switch(c){
30  case 'i':
31  infolder=optarg;
32  break;
33  case 't':
34  tag=optarg;
35  break;
36  default:
37  break;
38  }
39  }
40  if(tag=="" || infolder==""){
41  cout<<endl<<"Specify input folder and tag: "
42  <<"./run/json_ntuple.exe -i <infolder> -o <outfolder=out> -t <tag>"<<endl<<endl;
43  return 1;
44  }
45  gSystem->mkdir(outfolder, kTRUE);
46 
47  map<int, set<int> > lumis;
48  int lumi;
49  int run;
50 
51  TChain chain("tree");
52  TString filename(infolder+"/*"+tag+"*.root");
53  int files = chain.Add(filename);
54  if(files<1) {
55  cout<<endl<<"No files found for "<<filename<<". Exiting"<<endl<<endl;
56  return 0;
57  }
58 
59  TBranch *b_lumi(NULL), *b_run(NULL);
60  chain.SetBranchAddress("lumiblock", &lumi, &b_lumi);
61  chain.SetBranchAddress("run", &run, &b_run);
62 
63  long entries(chain.GetEntries()), tree_entry;
64 
65  time(&startTime);
66  cout<<endl<<"Doing "<<files<<" files in "<<filename<<" with "<<entries<<" entries"<<endl;
67  for(int entry(0); entry<entries; entry++){
68  if(entry!=0 && entry%1000000==0) {
69  time(&curTime);
70  int seconds(difftime(curTime,startTime));
71 
72  cout<<"Doing entry "<<setw(10)<<addCommas(entry)<<" of "<<addCommas(entries)
73  <<" Took "<<setw(6)<<seconds<<" seconds at "
74  <<setw(4)<<roundNumber(entry,1,seconds*1000.)<<" kHz"<<endl;
75  }
76 
77  tree_entry = chain.LoadTree(entry);
78  b_run->GetEntry(tree_entry);
79  b_lumi->GetEntry(tree_entry);
80 
81  if(lumis.find(run) == lumis.end()) lumis[run] = set<int>(); // New run
82  lumis[run].insert(lumi);
83  } // Loop over entries
84 
85  TString txtname(outfolder+"/json_"+tag+".txt");
86  ofstream txtfile(txtname);
87  txtfile<<"{";
88  for(map<int, set<int> >::const_iterator it = lumis.begin(); it != lumis.end(); ++it) {
89  if(it != lumis.begin()) txtfile<<"],"<<endl;
90  run = it->first;
91  txtfile<<"\""<<run<<"\": [";
92  int prev_lumi = -99;
93  size_t ind = 0;
94  for (set<int>::iterator itlumi = lumis[run].begin(); itlumi != lumis[run].end(); ++itlumi){
95  lumi = *itlumi;
96  if(itlumi == lumis[run].begin()) txtfile<<"["<<lumi<<", ";
97  else {
98  if(lumi > prev_lumi+1) {
99  txtfile<<prev_lumi<<"], ["<<lumi<<", ";
100  }
101  } // if not first lumi
102  if(ind == lumis[run].size()-1) txtfile<<lumi<<"]";
103  prev_lumi = lumi;
104  ind++;
105  } // Loop over lumis
106 
107  }
108  txtfile<<"]}"<<endl;
109  txtfile.close();
110  time(&curTime);
111  int seconds(difftime(curTime,startTime));
112  cout<<endl<<"Written json in "<<txtname<<". Took "<<seconds<<" seconds"<<endl;
113  cout<<endl<<endl;
114 
115  return 0;
116 }
int main(int argc, char *argv[])
STL namespace.
tuple run
Parsing run from file name.
TString addCommas(double num)
Definition: utilities.cc:499
TString roundNumber(double num, int decimals, double denom=1.)
Definition: utilities.cc:478
tuple ind
Definition: resubmit.py:140