StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
QAmergeRefHists.C
1 /*
2 
3  QAmergeRefHists.C
4  Author: G. Van Buren, BNL (Dec. 2010)
5 
6  Purpose: Merge two QA Reference histogram files oldFile & modFile into
7  newFile. It does so by taking only those histograms listed in
8  listFile from modFile, otherwise taking from oldFile.
9 
10 */
11 
12 void QAmergeRefHists(char* listFile, char* oldFile, char* modFile, char* newFile) {
13 
14  // STAR libs needed for StMultiH*F histograms
15  gSystem->Load("St_base");
16  gSystem->Load("StarClassLibrary");
17  gSystem->Load("StUtilities");
18 
19  TFile oldFi(oldFile,"READ");
20  TFile modFi(modFile,"READ");
21  TFile newFi(newFile,"RECREATE");
22 
23  TList* oldKeys = oldFi.GetListOfKeys();
24  TList* modKeys = modFi.GetListOfKeys();
25  TList newKeys;
26 
27  Char_t buffer[512];
28  TString histName,modName;
29  TMap modNames;
30  Int_t nkeys,i;
31 
32  nkeys = oldKeys->GetSize();
33  // Start with the old keys
34  for (i = 0; i < nkeys; i++) newKeys.Add(oldKeys->At(i));
35 
36  ifstream listFi(listFile);
37  while (listFi.good()) {
38  listFi >> buffer;
39  histName = buffer;
40  Bool_t modifyName = histName.BeginsWith("__");
41  if (modifyName) {
42  // Will modify the histogram name
43  // to whatever is between __*__x__
44  Ssiz_t endNewName = histName.Index("__x__",5,2);
45  if (endNewName > 0 && endNewName < histName.Length()-5) {
46  modName = histName(2,endNewName-2);
47  histName.Remove(0,endNewName+5);
48  } else modifyName = kFALSE;
49  }
50  TKey* modKey = (TKey*) (modKeys->FindObject(histName.Data()));
51  if (!modKey) {
52  histName.ReplaceAll('_',' ');
53  if (modifyName) modName.ReplaceAll('_',' ');
54  modKey = (TKey*) (modKeys->FindObject(histName.Data()));
55  }
56  if (modKey) {
57  // Don't add the same key twice
58  if (newKeys.FindObject(modKey)) continue;
59 
60  // Look to see if we're replacing an old one
61  TObject* oldKey = newKeys.FindObject(modifyName ?
62  modName.Data() : histName.Data());
63  if (oldKey) {
64  newKeys.AddAfter(oldKey,modKey);
65  newKeys.Remove(oldKey);
66  } else {
67  newKeys.Add(modKey);
68  }
69  if (modifyName) modNames.Add(modKey, new TNamed(modName,modName));
70  }
71  }
72  listFi.close();
73 
74  // Read and write the actual histogram objects
75  nkeys = newKeys.GetSize();
76  for (i = 0; i < nkeys; i++) {
77  TKey* key = (TKey*) (newKeys.At(i));
78  TObject* obj = key->ReadObj();
79  TObject* modValue = modNames.GetValue(key);
80  // Modify name if necessary
81  if (modValue) ((TNamed*) obj)->SetName(modValue->GetName());
82  newFi.cd();
83  obj->Write();
84  }
85 
86  oldFi.Close();
87  modFi.Close();
88  //newFi.Close(); // causes a seg fault?
89 
90 }
91 
93 // $Id: QAmergeRefHists.C,v 1.4 2012/05/01 20:44:16 genevb Exp $
94 // $Log: QAmergeRefHists.C,v $
95 // Revision 1.4 2012/05/01 20:44:16 genevb
96 // Account for when underscore represent space
97 //
98 // Revision 1.3 2011/03/15 23:02:12 genevb
99 // Use __ delimeters, more careful about modifier spec
100 //
101 // Revision 1.2 2011/02/25 23:00:47 genevb
102 // Allow histogram name modification
103 //
104 // Revision 1.1 2010/12/23 01:10:09 genevb
105 // Introduce macro
106 //
107 //