StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
transformBackupHistoToDBTable.C
1 #include "TFile.h"
2 //#include "TCanvas.h"
3 //#include "TH1.h"
4 //#include <smdGain.h>
5 //#include <St_smdGain_Table.h>
6 
7 void transformBackupHistoToDBTable(Char_t *hist_filename = "./backup.emconline_ped/bemcPed.20060302.084014.root", Char_t *tables_path = "./tables.emconline_ped/") {
8  gROOT->LoadMacro("$STAR/StRoot/StMuDSTMaker/COMMON/macros/loadSharedLibraries.C");
9  loadSharedLibraries();
10  gSystem->Load("StDbLib");
11  gSystem->Load("StEmcUtil");
12 
13  TString inputFilename(hist_filename);
14  cout << "Input filename is " << inputFilename.Data() << endl;
15 
16  TString outputFilename;
17  TString tableName;
18 
19  TObjArray *dirNames = inputFilename.Tokenize(TString("/"));
20 
21  if (dirNames) {
22  cout << "Input filename tokenized" << endl;
23  TObjString *lastFileName = (TObjString*)dirNames->Last();
24  if (lastFileName) {
25  outputFilename = lastFileName->GetString();
26  cout << "File is " << outputFilename.Data() << endl;
27  TObjArray *pieces = outputFilename.Tokenize(".");
28  if (pieces) {
29  cout << "File name tokenized" << endl;
30  TObjString *firstPiece = (TObjString*)pieces->First();
31  if (firstPiece) {
32  tableName = firstPiece->GetString();
33  cout << "Table name is " << tableName.Data() << endl;
34  }
35  delete pieces;
36  }
37  }
38  delete dirNames;
39  }
40 
41  if (outputFilename.Length() != 0) outputFilename.Prepend("/");
42 
43  int detector = 0;
44  if (tableName == "bemcPed") {
45  detector = 1;
46  if (outputFilename.Length() != 0) outputFilename.Prepend("/y3bemc");
47  }
48  if (tableName == "bsmdePed") {
49  detector = 2;
50  if (outputFilename.Length() != 0) outputFilename.Prepend("/y3bsmde");
51  }
52  if (tableName == "bsmdpPed") {
53  detector = 3;
54  if (outputFilename.Length() != 0) outputFilename.Prepend("/y3bsmdp");
55  }
56  if (tableName == "bprsPed") {
57  detector = 4;
58  if (outputFilename.Length() != 0) outputFilename.Prepend("/y3bprs");
59  }
60 
61  if (outputFilename.Length() != 0) outputFilename.Prepend(tables_path);
62 
63  cout << "Output filename is " << outputFilename.Data() << endl;
64  cout << "Output table name is " << tableName.Data() << endl;
65 
66  if (detector == 0) {
67  cout << "Unknown detector!" << endl;
68  return;
69  }
70 
71  TFile *hist_file = new TFile(hist_filename, "READ");
72  TH1F *ped = 0;
73  TH1F *rms = 0;
74  TH1F *chi = 0;
75  TH1F *status = 0;
76  if (hist_file && hist_file->IsOpen()) {
77  cout << "Input file is open" << endl;
78  ped = dynamic_cast<TH1F*>(hist_file->Get("mPed"));
79  cout << "Read ped = " << ped << endl;
80  rms = dynamic_cast<TH1F*>(hist_file->Get("mRms"));
81  cout << "Read rms = " << rms << endl;
82  chi = dynamic_cast<TH1F*>(hist_file->Get("mChi"));
83  cout << "Read chi = " << chi << endl;
84  status = dynamic_cast<TH1F*>(hist_file->Get("mStatus"));
85  cout << "Read status = " << status << endl;
86  }
87 
88  TFile *table_file = 0;
89  if (ped && rms && chi && status) table_file = new TFile(outputFilename.Data(), "RECREATE");
90 
91  if (table_file && table_file->IsOpen()) {
92  cout << "Output file open" << endl;
93 
94  if ((detector == 1) || (detector == 4)) {
95  St_emcPed *st = new St_emcPed(tableName.Data(), 1);
96  emcPed_st *rec = st->GetTable();
97  for (Int_t i = 0;i < 4800;i++) {
98  Float_t pedestal = (i < ped->GetXaxis()->GetNbins()) ? ped->GetBinContent(i + 1) : 0;
99  Float_t pedestalRms = (i < rms->GetXaxis()->GetNbins()) ? rms->GetBinContent(i + 1) : 0;
100  Float_t pedestalChi = (i < chi->GetXaxis()->GetNbins()) ? chi->GetBinContent(i + 1) : 0;
101  Float_t pedestalStatus = (i < status->GetXaxis()->GetNbins()) ? status->GetBinContent(i + 1) : 1;
102  cout << i << ": ped = " << pedestal << ", rms = " << pedestalRms << ", chi = " << pedestalChi << ", status = " << pedestalStatus << endl;
103  rec->AdcPedestal[i] = pedestal * 100;
104  rec->AdcPedestalRMS[i] = pedestalRms * 100;
105  rec->Status[i] = pedestalStatus;
106  }
107  st->AddAt(rec, 0);
108  st->Write();
109  }
110 
111  if ((detector == 2) || (detector == 3)) {
112  St_smdPed *st = new St_smdPed(tableName.Data(), 1);
113  smdPed_st *rec = st->GetTable();
114  for (Int_t i = 0;i < 18000;i++) {
115  Float_t pedestal = (i < ped->GetXaxis()->GetNbins()) ? ped->GetBinContent(i + 1) : 0;
116  Float_t pedestalRms = (i < rms->GetXaxis()->GetNbins()) ? rms->GetBinContent(i + 1) : 0;
117  Float_t pedestalChi = (i < chi->GetXaxis()->GetNbins()) ? chi->GetBinContent(i + 1) : 0;
118  Float_t pedestalStatus = (i < status->GetXaxis()->GetNbins()) ? status->GetBinContent(i + 1) : 1;
119  cout << i << ": ped = " << pedestal << ", rms = " << pedestalRms << ", chi = " << pedestalChi << ", status = " << pedestalStatus << endl;
120  rec->AdcPedestal[i][0] = pedestal * 100;
121  rec->AdcPedestalRMS[i][0] = pedestalRms * 100;
122  rec->Status[i] = pedestalStatus;
123  }
124  st->AddAt(rec, 0);
125  st->Write();
126  }
127 
128  table_file->Write();
129  cout << "Output file written" << endl;
130  }
131 
132  if (table_file) {
133  table_file->Close();
134  delete table_file;
135  cout << "Output file closed" << endl;
136  }
137 
138  if (hist_file) delete hist_file;
139  cout << "Finished." << endl;
140 }