StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
write_trigger_definitions.C
1 int write_trigger_definitions(int runNumber = 13078009)
2 {
3  // Load all required libraries
4  gROOT->Macro("loadMuDst.C");
5  gROOT->Macro("LoadLogger.C");
6  //gSystem->Load("St_base.so");
7  gSystem->Load("libStDb_Tables.so");
8  gSystem->Load("StDbLib.so");
9 
10  gSystem->Load("StEmcRawMaker");
11  gSystem->Load("StEmcADCtoEMaker");
12  gSystem->Load("StEEmcUtil");
13  gSystem->Load("StEEmcDbMaker");
14  gSystem->Load("StTriggerUtilities");
15  //******//
16  gSystem->Setenv("DB_ACCESS_MODE","write");
17  //******//
18  // Initialize db manager
20  StDbConfigNode* node = mgr->initConfig("Calibrations_trg");
21  StDbTable* dbtable = node->addDbTable("triggerDefinition");
22  // beginTime timestamp in MySQL format: "YYYY-MM-DD HH:mm:ss"
23  ifstream intime(Form("beginTimes/%d.beginTimes.txt", runNumber));
24  if(!intime){
25  cout<<"can't open beginTime file"<<endl;
26  return 0;
27  }
28  char date[10];
29  char time[8];
30  intime >> date >> time;
31  TString storeTime(Form("%s %s", date, time));
32  mgr->setStoreTime(storeTime.Data());
33 
34  // Create your c-struct
35  triggerDefinition_st table;
36 
37  // Fill structure with data
38  // sample setup for a single channel, please add more channels!
39  strcpy(table.comments, Form("run%d triggerDefinition uploaded by zchang", runNumber));
40  cout<<"comments set to "<<table.comments<<endl;
41 
42  TObjArray objarr = readOnline(runNumber);
43  TBufferFile buf(TBuffer::kWrite);
44  buf << &objarr;
45  objarr.Delete();
46 
47  cout<<"Buffer size: "<<buf.BufferSize()<<endl;
48 
49  memset(table.trigdef, 0, buf.BufferSize());
50  memcpy(table.trigdef, buf.Buffer(), buf.BufferSize());
51  table.size = buf.BufferSize();
52 
53  //******//
54  // Store data to the StDbTable
55  dbtable->SetTable((char*)&table, 1);
56 
57  // uncomment next line to set "sim" flavor. "ofl" flavor is set by default, no need to set it.
58  // dbtable->setFlavor("sim");
59 
60  // Store table to database
61  cout<<"Storing Db table: "<< mgr->storeDbTable(dbtable) << endl;
62  //******//
63 
64  ofstream out(Form("buffer/%d.trigdef.buffer.out", runNumber));
65  assert(out);
66  out.write(buf.Buffer(),buf.BufferSize());
67  out.close();
68 
69  return 1;
70 }
71 TObjArray readOnline(int runNumber)
72 {
73  TObjArray array;
74 
75  // Open connection to online database
76  const char* database = "mysql://db04.star.bnl.gov:3411/Conditions_rts?timeout=60";
77  const char* user = "zchang";
78  const char* pass = "";
79  TMySQLServer* mysql = TMySQLServer::Connect(database,user,pass);
80 
81  if (!mysql) {
82  cerr << "Connection to " << database << " failed" << endl;
83  return;
84  }
85 
86  TString query;
87  TMySQLResult* result;
88 
89  query = Form("select idx_trigger,name,offlineBit from `Conditions_rts`.`triggers` where idx_rn = %d",runNumber);
90 
91  result = (TMySQLResult*)mysql->Query(query);
92  if (result) {
93  TMySQLRow* row;
94  while (row = (TMySQLRow*)result->Next()) {
95  int triggerIndex = atoi(row->GetField(0));
97  def->triggerIndex = triggerIndex;
98  def->name = row->GetField(1);
99  def->triggerId = atoi(row->GetField(2));
100  array.AddAtAndExpand(def,triggerIndex);
101  delete row;
102  }
103  result->Close();
104  }
105 
106  query = Form("select idx_idx,onbits,offbits,onbits1,onbits2,onbits3,offbits1,offbits2,offbits3 from `Conditions_rts`.`pwc` where idx_rn = %d",runNumber);
107 
108  result = (TMySQLResult*)mysql->Query(query);
109  if (result) {
110  TMySQLRow* row;
111  while (row = (TMySQLRow*)result->Next()) {
112  int triggerIndex = atoi(row->GetField(0));
113  StTriggerDefinition* def = (StTriggerDefinition*)array.At(triggerIndex);
114  if (def) {
115  sscanf(row->GetField(1), "%ud", &def->onbits);
116  sscanf(row->GetField(2), "%ud", &def->offbits);
117  sscanf(row->GetField(3), "%ud", &def->onbits1);
118  sscanf(row->GetField(4), "%ud", &def->onbits2);
119  sscanf(row->GetField(5), "%ud", &def->onbits3);
120  sscanf(row->GetField(6), "%ud", &def->offbits1);
121  sscanf(row->GetField(7), "%ud", &def->offbits2);
122  sscanf(row->GetField(8), "%ud", &def->offbits3);
123  //cout<<hex<<def->onbits<<" "<<def->offbits<<endl;
124  }
125  delete row;
126  }
127  result->Close();
128  }
129 
130  mysql->Close();
131 
132  array.Compress();
133 
134  for (int triggerIndex = 0; triggerIndex < array.GetEntriesFast(); ++triggerIndex) {
135  StTriggerDefinition* def = (StTriggerDefinition*)array.At(triggerIndex);
136  if (def) def->print();
137  }
138  return array;
139  /*
140  TBufferFile buf(TBuffer::kWrite);
141  buf << &a;
142  array.Delete();
143 
144  cout << "Serialized array of " << buf.BufferSize() << " : " << buf.Buffer() << endl;
145 
146  ofstream out("test.out");
147  assert(out);
148  out.write(buf.Buffer(),buf.BufferSize());
149  out.close();
150  */
151 }
virtual void SetTable(char *data, int nrows, int *idList=0)
calloc&#39;d version of data for StRoot
Definition: StDbTable.cc:550
static StDbManager * Instance()
strdup(..) is not ANSI
Definition: StDbManager.cc:155