-
Notifications
You must be signed in to change notification settings - Fork 0
/
PseudoExp.cc
executable file
·211 lines (168 loc) · 6.23 KB
/
PseudoExp.cc
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
#include "PseudoExp.h"
PseudoExp::PseudoExp(){
fitInput = new MassAnaInput();
fitInput->GetParameters( "MuonCuts", &muonCuts );
fitInput->GetParameters( "JetCuts", &jetCuts );
}
PseudoExp::~PseudoExp(){
delete fitInput ;
}
bool ItIncreasing( int s1, int s2) { return ( s1 < s2 ); }
bool EtDescending( TLorentzVector v1, TLorentzVector v2 ) { return ( v1.Et() > v2.Et() ) ; }
vector<int> PseudoExp::GetEnsemble( string fileName, TString treeName, double pMean, int RandomSeed ) {
// get files and trees
TFile* file = NULL ;
TTree* tr1 = fitInput->GetTree( fileName, treeName, file );
// event solution tree
//int evtId1 ;
//tr1->SetBranchAddress( "evtId" , &evtId1 );
Int_t tsz = tr1->GetEntries();
cout<<" All Evt Size = "<< tsz << endl;
// set up the random number function
TRandom3* tRan = new TRandom3();
tRan->SetSeed( RandomSeed );
int NEvts = tRan->Poisson( pMean );
cout<<" N of Evts needed = "<< NEvts <<endl ;
vector<int> ensembles ;
for (int k=0; k < NEvts; k++) {
int selEntry = tRan->Integer( tsz );
// double check the selected entries, in case any repeat selection
bool repeat = false ;
for (size_t j = 0; j< ensembles.size(); j++ ) {
repeat = ( ensembles[j] == selEntry ) ? true : false ;
if ( repeat ) break ;
}
if ( repeat ) {
k-- ;
continue;
}
ensembles.push_back( selEntry );
cout<<k<<" == selEntry : " << selEntry <<endl;
}
sort( ensembles.begin(), ensembles.end(), ItIncreasing ) ;
return ensembles ;
}
vector<int> PseudoExp::EventShuffle( int theSize, int RandomSeed ) {
const int tsz = theSize;
// set up the random number function
TRandom3* tRan = new TRandom3();
tRan->SetSeed( RandomSeed );
vector<int> evtpool;
for(int i=0; i<tsz; i++) {
evtpool.push_back( i ) ;
}
for(int i=tsz-1; i>1; i--) {
int k = tRan->Integer( i );
swap( evtpool[k], evtpool[i]);//swaps the randomly picks character with n
}
delete tRan ;
return evtpool ;
}
void PseudoExp::JetEtReSort( vector<TLorentzVector>& vs ) {
// re-sort the Jets
vector<TLorentzVector> newVs ;
for (size_t i=0; i< vs.size()- 2; i++ ) {
newVs.push_back( vs[i] );
}
sort( newVs.begin(), newVs.end(), EtDescending ) ;
newVs.push_back( vs[ vs.size() -2 ] );
newVs.push_back( vs[ vs.size() -1 ] );
vs.clear();
for (size_t i=0; i< newVs.size(); i++ ) {
vs.push_back( newVs[i] ) ;
}
}
void PseudoExp::PhaseSmearing( vector<TLorentzVector>& vs, int RandomSeed, double jes, bool ReMET ) {
//cout<<" Start Smearing ~~~ "<<endl;
TRandom3* tRan = new TRandom3();
tRan->SetSeed( RandomSeed );
double rf = tRan->Uniform( -3.1415, 3.1415 ) ;
double bz = tRan->Uniform( -0.2, 0.2 ) ;
//double bx = tRan->Gaus( -0.1, 0.1 ) ;
//double by = tRan->Gaus( -0.1, 0.1 ) ;
TVector3 bv( 0, 0, bz ) ;
//cout<<" bv ("<<bx<<","<<by<<","<<bz<<")"<<" rotate : "<< rf <<endl;
//const size_t sz = vs.size() ;
//double sc[sz] ;
//tRan->RndmArray( sz, sc ) ;
double npx = 0.;
double npy = 0.;
for (size_t i=0; i< vs.size()- 2; i++ ) {
//cout<<" "<< i <<"* vs :"<<vs[i].Pt()<<" / "<< vs[i].Pz()<<endl;
//mA = mA + vs[i] ;
//if ( i < 3 ) m3A = m3A + vs[i] ;
int nS = 0 ;
//double minPt = ( vs[i].Pt() >= jetCuts[0] ) ? jetCuts[0] : 20. ;
TLorentzVector vs1( vs[i].Px(), vs[i].Py(), vs[i].Pz(), vs[i].E() );
if ( vs1.Pt() == 0 && vs1.Pz() == 0 ) continue ;
while( nS == 0 || vs1.Pt() < jetCuts[0] ) {
vs1 = TLorentzVector( vs[i].Px(), vs[i].Py(), vs[i].Pz(), vs[i].E() ) ;
tRan->SetSeed( i*17 + nS + RandomSeed ); // just change the random number seed
double sc = tRan->Rndm();
double scl = ( jes == 0 ) ? (sc-0.5)/10. : tRan->Gaus( 0, jes ) ;
if ( nS == 0 ) {
vs1.RotateZ( rf );
vs1 = vs1*( 1. + scl );
vs1.Boost( -bv );
} else {
vs1.RotateZ( rf );
}
nS++ ;
}
vs[i] = vs1 ;
npx = npx - vs[i].Px() ;
npy = npy - vs[i].Py() ;
//cout<<" -> vs :"<<vs[i].Pt()<<" / "<< vs[i].Pz() <<endl;
//mB = mB + vs[i] ;
//if ( i < 3 ) m3B = m3B + vs[i] ;
}
// smearing the muon and MET
//double sc0 = tRan->Rndm();
//double scl0 = (sc0-0.5)/10. ;
double scl0 = tRan->Gaus( 0, 0.01 ) ;
int im = vs.size() - 2 ;
int in = vs.size() - 1 ;
double dphi = vs[ im ].DeltaPhi( vs[ in ] ) ;
double Mt2 = 2.* vs[im].Pt() * vs[in].Pt() *( 1- cos(dphi) );
for (int i= im ; i < in+1 ; i++ ) {
//cout<<" "<< i <<"* vs :"<<vs[i].Pt()<<" / "<< vs[i].Pz()<<" M = "<< vs[i].M() <<endl;
double theM = vs[i].M() ;
TLorentzVector vs1( vs[i].Px(), vs[i].Py(), vs[i].Pz(), vs[i].E() );
if ( vs1.Pt() == 0 && vs1.Pz() == 0 ) continue ;
vs1.RotateZ( rf );
vs1.Boost( -bv );
if ( i == im ) {
vs1 = vs1*( 1. + scl0 );
if ( vs1.Pt() < muonCuts[0] || fabs(vs1.Eta()) > muonCuts[1] ) vs1 = vs[i] ;
}
if ( i == in ) {
double sc = tRan->Rndm();
scl0 = ( jes == 0 ) ? (sc-0.5)/10. : tRan->Gaus( 0, jes ) ;
dphi = vs[ im ].DeltaPhi( vs1 ) ;
double new_met = Mt2 / ( 2.*vs[ im ].Pt()*( 1- cos(dphi) ) );
double msc = new_met*(1.+scl0) / vs1.Pt() ;
//cout <<" new MET = "<< new_met <<" scale = "<< msc <<endl ;
double theE = msc*sqrt( vs1.Pt()*vs1.Pt() + theM*theM ) ;
vs1 = TLorentzVector( vs1.Px()* msc, vs1.Py()*msc, 0, theE ) ;
if ( vs1.Pt() < jetCuts[2] ) vs1 = vs1 * ( jetCuts[2]/vs1.Pt() ) ;
}
vs[i] = vs1 ;
npx = npx - vs[i].Px() ;
npy = npy - vs[i].Py() ;
//cout<<" -> vs :"<<vs[i].Pt()<<" / "<< vs[i].Pz() <<" M = "<< vs[i].M() <<endl;
}
// Re-do the MET, if desire ...
if ( ReMET ) {
// smearing the phi
TLorentzVector newMET( npx, npy, 0., sqrt( (npx*npx) + (npy*npy) ) ) ;
double s_phi = 0.69*pow( newMET.Pt(), -0.64 ) ;
double sf = tRan->Gaus( 0., s_phi ) ;
newMET.RotateZ( sf ) ;
// smearing the MET
double s_met = 0.7*sqrt( (5.6*5.6)/(newMET.Pt()*newMET.Pt() ) + ( 1.25*1.25/newMET.Pt() ) + (0.033*0.033) );
double sE = tRan->Gaus( 0., s_met ) ;
newMET = newMET*( 1. + sE ) ;
vs.push_back( newMET );
}
delete tRan ;
}