Crombie Tools
YieldDump.cc
Go to the documentation of this file.
1 #include <set>
2 #include <utility>
3 
4 #include "TString.h"
5 
6 #include "YieldDump.h"
7 
9 
10 //--------------------------------------------------------------------
11 void
12 YieldDump::SimpleExecute(sqlite3* conn, const char* query)
13 {
14  int rc;
15  sqlite3_stmt *stmt;
16 
17  Message(eInfo, "About to execute:\n%s", query);
18  sqlite3_prepare_v2(conn, query, -1, &stmt, NULL);
19  rc = sqlite3_step(stmt);
20  Message(eInfo, "Return code: %i", rc);
21 
22  if (rc != SQLITE_OK and rc != SQLITE_DONE)
23  Message(eError, "Error from connection:\n%s", sqlite3_errmsg(conn));
24 
25  rc = sqlite3_finalize(stmt);
26  Message(eInfo, "Return code: %i", rc);
27 
28 }
29 
30 //--------------------------------------------------------------------
31 void
32 YieldDump::DumpYieldFiles(const char* out_file, Int_t NumXBins, Double_t *XBins)
33 {
34  std::vector<std::pair<FileType, std::string>> types_files = {
35  std::make_pair(kBackground, "background"),
36  std::make_pair(kSignal, "signal"),
37  std::make_pair(kData, "data")
38  };
39 
40  std::string delim = ",";
41 
42  sqlite3 *conn;
43  if(sqlite3_open(out_file, &conn) != SQLITE_OK) {
44  Message(eError, "Can't open database in %s", out_file);
45  sqlite3_close(conn);
46  exit(50);
47  }
48 
49  // Create table with all the uncertainties
50 
51  SimpleExecute(conn, R"SQL(
52 CREATE TABLE IF NOT EXISTS yields (
53 region VARCHAR(64),
54 process VARCHAR(64),
55 bin INT,
56 contents DOUBLE,
57 stat_unc DOUBLE,
58 type VARCHAR(32),
59 PRIMARY KEY (region, process, bin)
60 )
61 )SQL");
62 
63  int rc;
64 
65  for (auto iTypeFile = types_files.begin(); iTypeFile != types_files.end(); ++iTypeFile) {
66 
67  // Get the histogram
68  auto processes = ReturnTreeNames(iTypeFile->first);
69 
70  for (unsigned int iCut = 0; iCut != fRegionCuts.size(); ++iCut) {
71 
74  SetMCWeight(fMCWeights[iCut]);
75 
76  for (auto iProcess = processes.begin(); iProcess != processes.end(); ++iProcess) {
77 
78  Message(eInfo, "Getting histograms from %s for %s region.",
79  iProcess->Data(), fRegionCuts[iCut].GetName());
80 
81  auto proc_hist = GetHist(NumXBins, XBins, iTypeFile->first, *iProcess, kLimitName);
82 
83  // Get the contents of each histogram to dump into file
84  for (int iBin = 1; iBin < NumXBins + 1; iBin++) {
85 
86  // Dump into sqlite3 database
87  sqlite3_stmt *yield_stmt;
88  sqlite3_prepare_v2(conn, "INSERT INTO yields VALUES(?, ?, ?, ?, ?, ?)", -1, &yield_stmt, NULL);
89  sqlite3_bind_text(yield_stmt, 1, fRegionCuts[iCut].GetName(), -1, NULL);
90  sqlite3_bind_text(yield_stmt, 2, iProcess->Data(), -1, NULL);
91  sqlite3_bind_int(yield_stmt, 3, iBin);
92  sqlite3_bind_double(yield_stmt, 4, proc_hist->GetBinContent(iBin));
93  sqlite3_bind_double(yield_stmt, 5, proc_hist->GetBinError(iBin));
94  sqlite3_bind_text(yield_stmt, 6, iTypeFile->second.data(), -1, NULL);
95 
96  rc = sqlite3_step(yield_stmt);
97  if (rc != SQLITE_OK and rc != SQLITE_DONE)
98  Message(eError, "Error from connection:\n%s", sqlite3_errmsg(conn));
99 
100  sqlite3_finalize(yield_stmt);
101 
102  }
103 
104  delete proc_hist;
105  }
106  }
107  }
108 
109  sqlite3_close(conn);
110 }
111 
112 //--------------------------------------------------------------------
113 void
114 YieldDump::DumpYieldFiles(const char* out_file, Int_t NumXBins, Double_t MinX, Double_t MaxX)
115 {
116  Double_t XBins[NumXBins+1];
117  ConvertToArray(NumXBins, MinX, MaxX, XBins);
118  DumpYieldFiles(out_file, NumXBins, XBins);
119 }