/
MakeCombinedCFs.C
311 lines (269 loc) · 9.51 KB
/
MakeCombinedCFs.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// ******** Workflow *******
// Needs to be run using Root 6
//
// (0. Make CFs using MakeCFs.C)
// 1. Run MakeCombinedCFs() <- Handles all combining
//
// *************************
#include <iostream>
#include <vector>
#include "TDirectory.h"
#include "TFile.h"
#include "TString.h"
#include "TH1D.h"
#include "TVectorD.h"
#include "TKey.h"
#include "/home/jai/Analysis/lambda/AliAnalysisLambda/Results/macros/GetDataDirectories.C"
using namespace std;
vector<TDirectory*> GetDirectories(TDirectory *f, vector<TString> dataSetNames, TString dirName)
{
vector<TDirectory*> dirs;
UInt_t nDirs = dataSetNames.size();
for(UInt_t i = 0; i < nDirs; i++) {
TString fullDirName = dataSetNames[i] + "/" + dirName;
TDirectory *dir = (TDirectory*) f->Get(fullDirName);
if(!dir) {
cout << "MakeCombinedCFs -- Could not find directory named " << fullDirName << endl;
continue;
}
dirs.push_back(dir);
}
return dirs;
}
TH1D *CombineCFs(vector<TH1D*> cfs, vector<Double_t> counts)
{
// Take in a vector of cfs and a vector of counts, and use them to
// make an averaged correlation function
UInt_t nCFs = cfs.size();
if(nCFs == 0) {
cout << "No CFs found in collection. Cannot combine" << endl;
return NULL;
} else if (nCFs == 1) {
cout<<"Only one cf found in collection."<<endl
<<"Cannot combine."<<endl
<<"Returning the found CF."<<endl;;
return cfs[0];
}
assert(nCFs == counts.size());
TH1D *combinedCF = (TH1D*)cfs[0]->Clone();
combinedCF->Scale(counts[0]);
Double_t totalCounts = counts[0];
// Add together weighted cfs
for(UInt_t i = 1; i < nCFs; i++) {
TH1D *copyCF = (TH1D*) cfs[i]->Clone();
Double_t thisCount = counts[i];
copyCF->Scale(thisCount);
combinedCF->Add(copyCF);
totalCounts += thisCount;
delete copyCF; copyCF = NULL;
}
// Now get average
combinedCF->Scale(1./totalCounts);
return combinedCF;
}
void CombineCFsInDataDir(TDirectory *f, vector<TString> dataSetNames)
{
TString cfDirName = "CF";
TString countDirName = "Count";
vector<TDirectory*> cfDirs = GetDirectories(f, dataSetNames, cfDirName);
vector<TDirectory*> countDirs = GetDirectories(f, dataSetNames, countDirName);
TDirectory *mergeDir = (TDirectory*)f->GetDirectory("Merged");
if(!mergeDir) {
mergeDir = f->mkdir("Merged");
}
UInt_t nDirs = cfDirs.size();
// Merge each centrality one at a time
// Load the cfs and counts into vectors for that centrality
// Find all the cfs and counts that match the name of the CF
// in the first directory
TIter iter(cfDirs[0]->GetListOfKeys());
TObject *obj = NULL;
while ( (obj = iter()) ) {
TKey *key = dynamic_cast<TKey*>(obj);
TH1D *cf = dynamic_cast<TH1D*>(key->ReadObj());
if(!cf) {
cout<<"Could not find a CF hist"<<endl;
return;
}
//Figure out which CF we are trying to grab
TString cfName = cf->GetName();
TString countName = cfName;
countName.ReplaceAll("CF","Count");
//Grab the corresponding CFs and counts
vector<TH1D*> cfs;
vector<Double_t> counts;
Double_t totalCounts = 0;
for(UInt_t iDir = 0; iDir < nDirs; iDir++) {
TH1D *thisCF = (TH1D*) cfDirs[iDir]->Get(cfName);
if(!thisCF) {
cout<<"Could not find CF matching "<<cfName
<<" in "<<cfDirs[iDir]->GetName()<<endl;
}
// TVectorD *count = dynamic_cast<TVectorD*>(countDirs[iDir]->Get(countName));
TVectorD *count = (TVectorD*)countDirs[iDir]->Get(countName);
// cout<<count->ClassName()<<endl;
Double_t myCount = count[0](0);
cfs.push_back(thisCF);
counts.push_back(myCount);
totalCounts += myCount;
}
// Finally, combine the CFs
TH1D *combinedCF = CombineCFs(cfs, counts);
if (!combinedCF) {
cout << "Combine CF returned nothing. Continuing loop."
<<endl;
continue;
}
TVectorD finalCount(1);
finalCount[0] = totalCounts;
cout<<"Writing combined CF "<<combinedCF->GetName()
<<" to "<<mergeDir->GetName()<<endl;
combinedCF->SetDirectory(0);
mergeDir->cd();
combinedCF->Write(combinedCF->GetName(), TObject::kOverwrite);
finalCount.Write(countName, TObject::kOverwrite);
}
}
void CombineCentralitiesForDirectory(TString pairType, TDirectory *dataDir)
{
// Gather the cfs and counts to combine centrality bins
vector<TString> centBins010 = {"05", "510"};
vector<TString> centBins1030 = {"1015", "1520", "2025", "2530"};
vector<TString> centBins3050 = {"3035", "3540", "4045", "4550"};
vector<TString> finalCentBins = {"010", "1030", "3050"};
vector<vector<TString> > centBins;
centBins.push_back(centBins010);
centBins.push_back(centBins1030);
centBins.push_back(centBins3050);
TDirectory *mergeDir = dataDir->GetDirectory("Merged");
if(!mergeDir) {
cout<<"Merge directory does not exist. Cannot merge."<<endl;
return;
}
//For each merge group, get the necessary CFs and counts
for(UInt_t iMerge = 0; iMerge < centBins.size(); iMerge++) {
vector<TH1D*> cfs;
vector<Double_t> counts;
Double_t totalCounts = 0;
for(UInt_t iCF = 0; iCF < centBins[iMerge].size(); iCF++) {
TString cfName = "CF" + pairType + centBins[iMerge][iCF];
TH1D *cf = (TH1D*)mergeDir->Get(cfName);
if(!cf) {
cout<<"Could not find CF named "<<cfName<<" in "<<mergeDir->GetName()<<endl;
return;
}
cfs.push_back(cf);
TString countName = "Count" + pairType + centBins[iMerge][iCF];
TVectorD *count = (TVectorD*) mergeDir->Get(countName);
totalCounts += count[0](0);
counts.push_back(count[0](0));
}
// Finally, combine the CFs
TH1D *combinedCF = CombineCFs(cfs, counts);
if (!combinedCF) {
cout << "Combine CF returned nothing. Continuing loop."
<<endl;
continue;
}
TVectorD finalCount(1);
finalCount[0] = totalCounts;
// Set names
TString combinedCFName = "CF" + pairType + finalCentBins[iMerge];
TString combinedCountName = "Count" + pairType + finalCentBins[iMerge];
combinedCF->SetName(combinedCFName);
combinedCF->SetTitle(combinedCFName);
// Set axis ranges
combinedCF->SetAxisRange(0.9, 1.1, "Y");
combinedCF->SetAxisRange(0., .5, "X");
combinedCF->SetLabelSize(0.05, "X");
combinedCF->SetLabelSize(0.05, "Y");
combinedCF->SetTitleSize(0.05, "X");
combinedCF->SetNdivisions(505, "X");
combinedCF->SetNdivisions(505, "Y");
cout<<"Writing combined CF "<<combinedCF->GetName()
<<" to "<<mergeDir->GetName()<<endl;
combinedCF->SetDirectory(0);
mergeDir->cd();
combinedCF->Write(combinedCF->GetName(), TObject::kOverwrite);
finalCount.Write(combinedCountName, TObject::kOverwrite);
}
}
void CombineLLAAForDirectory(TDirectory *dataDir)
{
// Run after merging centralities
vector<TString> centBins = {"010", "1030", "3050"};
vector<TString> pairTypes = {"LamLam", "ALamALam"};
TString finalPairType = "LLAA";
// Get merge dir
TDirectory *mergeDir = dataDir->GetDirectory("Merged");
if(!mergeDir) {
cout<<"Merge directory does not exist. Cannot merge."<<endl;
return;
}
for(UInt_t iCent = 0; iCent < centBins.size(); iCent++) {
vector<TH1D*> cfs;
vector<Double_t> counts;
Double_t totalCounts = 0;
for(UInt_t iType = 0; iType < pairTypes.size(); iType++) {
TString cfName = "CF" + pairTypes[iType] + centBins[iCent];
TH1D *cf = (TH1D*)mergeDir->Get(cfName);
if(!cf) {
cout<<"Could not find CF named "<<cfName<<" in "<<mergeDir->GetName()<<endl;
return;
}
cfs.push_back(cf);
TString countName = "Count" + pairTypes[iType] + centBins[iCent];
TVectorD *count = (TVectorD*) mergeDir->Get(countName);
totalCounts += count[0](0);
counts.push_back(count[0](0));
}
// Finally, combine the CFs
TH1D *combinedCF = CombineCFs(cfs, counts);
if (!combinedCF) {
cout << "Combine CF returned nothing. Continuing loop."
<<endl;
continue;
}
TVectorD finalCount(1);
finalCount[0] = totalCounts;
// Set names
TString combinedCFName = "CF" + finalPairType + centBins[iCent];
TString combinedCountName = "Count" + finalPairType + centBins[iCent];
combinedCF->SetName(combinedCFName);
combinedCF->SetTitle(combinedCFName);
// Set axis ranges
combinedCF->SetAxisRange(0.9, 1.1, "Y");
combinedCF->SetAxisRange(0., 1., "X");
cout<<"Writing combined CF "<<combinedCF->GetName()
<<" to "<<mergeDir->GetName()<<endl;
combinedCF->SetDirectory(0);
mergeDir->cd();
combinedCF->Write(combinedCF->GetName(), TObject::kOverwrite);
finalCount.Write(combinedCountName, TObject::kOverwrite);
}
}
void MakeCombinedCFs(Bool_t isDataCompact, Bool_t isTrainResult)
{
cout<<"Making combined cfs"<<endl;
// Get the directories for the cfs and counts
vector<TString> dataSetNames;
if (!isTrainResult) {
dataSetNames.push_back("Local");
} else if(!isDataCompact) {
TString dataSetNamesArr[5] = {"mm1", "mm2", "mm3", "pp1", "pp2"};
dataSetNames.assign(dataSetNamesArr, dataSetNamesArr+5);
} else {
TString dataSetNamesArr[2] = {"mm", "pp"};
dataSetNames.assign(dataSetNamesArr, dataSetNamesArr+2);
}
vector<TString> pairNames = {"LamLam", "ALamALam", "LamALam"};
TFile cfFile("CFs.root", "update");
vector<TDirectory*> dataDirs = GetDataDirectories(cfFile);
for(UInt_t iDir = 0; iDir < dataDirs.size(); iDir++) {
CombineCFsInDataDir(dataDirs[iDir], dataSetNames);
for(UInt_t iPair = 0; iPair < pairNames.size(); iPair++) {
CombineCentralitiesForDirectory(pairNames[iPair], dataDirs[iDir]);
}
CombineLLAAForDirectory(dataDirs[iDir]);
}
}