StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Example_tagDB.C
1 void Example_tagDB(const Char_t * selection = NULL)
2 {
4 // //
5 // Example_tagDB.C //
6 // //
7 // shows how to use the STAR MDC3 tag database //
8 // Input: selection //
9 // used to select all datasets with names matching the selection string //
10 // //
11 // what it does: //
12 // 1. selects tagDB files from the fileCatalog database in mysql //
13 // 2. creates ROOT TChain from all selected tagDB files //
14 // 3. loops over all events in the chain //
15 // //
16 // owner: Alexandre V. Vaniachine <AVVaniachine@lbl.gov> //
18 
19 
20  // start benchmarks
21  gBenchmark = new TBenchmark();
22  gBenchmark->Start("total");
23 
24  TSQLServer *db = TSQLServer::
25  Connect("mysql://duvall.star.bnl.gov/fcMDC3", "", "");
26 
27  TSQLResult *res;
28  TSQLRow *row;
29  TStringLong query= "SELECT CONCAT(path,'/',fName)
30  FROM fileCatalog
31  WHERE fName LIKE '%tags.root'
32  AND readable='Y'
33  AND dataset LIKE '%";
34  if (selection!=NULL)
35  query+=selection;
36  query+="%'";
37 
38  res = db->Query(query.Data());
39  Int_t nFiles = res->GetRowCount();
40 
41  cout<<"Selected "<<nFiles<<" files from the tagDB"<<endl;
42  if (nFiles==0) return;
43 
44  // set loop optimization level
45  gROOT->ProcessLine(".O4");
46  // gather all files from the same Run into one chain for loading to tagDB
47  TChain chain("Tag");
48  TString fileName;
49  UInt_t nF=0;
50  while ((row = res->Next())) {
51  fileName=row->GetField(0);
52  fileName.ReplaceAll("/home/starreco/reco","/star/rcf/GC/tags");
53 
54  nF++;
55  if (nF%100==0)
56  cout<<"Chained "<<nF<<" files, Total events = "
57  <<chain->GetEntries()<<endl;
58 
59  //cout<<"trying "<<fileName<<endl;
60  chain.Add(fileName.Data());
61  }
62  cout<<"chained "<<chain->GetEntries()<<" events from the tagDB"<<endl;
63 
64  //close the db connection
65  db->Close();
66 
67  TObjArray *files = chain.GetListOfFiles();
68  cout<<"chained "<<files->GetEntriesFast()<<" files from the tagDB"<<endl;
69 
70  // return;
71 
72  TObjArray *branches = chain.GetListOfBranches();
73  TObjArray *leaves = chain.GetListOfLeaves();
74  Int_t nleaves = leaves->GetEntriesFast();
75 
76  TString file;
77  Int_t l;
78  TLeaf *leaf;
79  TBranch *branch;
80 
81  //print out names of all tags
82  for (l=0;l<nleaves;l++) {
83  leaf = (TLeaf*)leaves->UncheckedAt(l);
84  branch = leaf->GetBranch();
85  cout<< "StructureName: "<<branch->GetName()
86  << ", tagName: "<<leaf->GetName()<<endl;
87  }
88 
89  //example of a histogram:
90  chain->Draw("NLa");
91  gPad->Update();
92  //return;
93 
94  gBenchmark->Start("loop");
95  //loop over all events
96  for (Int_t k=0;k<chain->GetEntries();k++) {
97 
98  //test that all events can be read in (read in all the tags)
99  chain->GetEntry(k);
100 
101  //print values only for the first event in each file of the chain
102  if (k == *(chain.GetTreeOffset()+chain.GetTreeNumber())) {
103  file = (files->UncheckedAt(chain.GetTreeNumber()))->GetTitle();
104  cout<<"chain event "<< k
105  <<", start of file "<< chain.GetTreeNumber()+1 <<endl;
106 // cout<<": "<< file.Data() <<endl;
107 
108  //printout NLa value
109  leaf = chain.GetLeaf("NLa");
110  cout<<leaf->GetName()<<" = "<<leaf->GetValue()<<endl;
111 
112  //uncomment lines below to printout all tag values
113 // branches = chain.GetListOfBranches();//renew pointers for each file
114 // leaves = chain.GetListOfLeaves();
115 // for (l=0;l<nleaves;l++) {
116 // leaf = (TLeaf*)leaves->UncheckedAt(nleaves-l-1);
117 // branch = leaf->GetBranch();
118 // if(branches->IndexOf(branch)!=2)
119 // cout<<leaf->GetName()<<" = "<<leaf->GetValue()<<endl;
120 // }
121  }
122  }
123 
124  // stop timer and print benchmarks
125  gBenchmark->Stop("loop");
126  gBenchmark->Print("loop");
127  gBenchmark->Stop("total");
128  gBenchmark->Print("total");
129 }
virtual const char * GetName() const
special overload
Definition: StMaker.cxx:237
virtual void Update()
Definition: TDataSet.cxx:864