00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 #ifndef __CINT__
00064 #include "TApplication.h"
00065 #include "TPythia6.h"
00066 #include "TFile.h"
00067 #include "TError.h"
00068 #include "TTree.h"
00069 #include "TClonesArray.h"
00070 #include "TH1.h"
00071 #include "TF1.h"
00072 #include "TStyle.h"
00073 #include "TLatex.h"
00074 #include "TCanvas.h"
00075 #include "Riostream.h"
00076 #include <cstdlib>
00077 using namespace std;
00078 #endif
00079
00080 #define FILENAME "pythia.root"
00081 #define TREENAME "tree"
00082 #define BRANCHNAME "particles"
00083 #define HISTNAME "ptSpectra"
00084 #define PDGNUMBER 211
00085
00086
00087
00088 void loadLibraries()
00089 {
00090 #ifdef __CINT__
00091
00092
00093 gSystem->Load("libEG");
00094 gSystem->Load("$ROOTSYS/../pythia6/libPythia6");
00095 gSystem->Load("libEGPythia6");
00096 #endif
00097 }
00098
00099
00100 int makeEventSample(Int_t nEvents)
00101 {
00102
00103 loadLibraries();
00104
00105
00106 TPythia6* pythia = new TPythia6;
00107
00108
00109 pythia->Initialize("cms", "p", "p", 200);
00110
00111
00112 TFile* file = TFile::Open(FILENAME, "RECREATE");
00113 if (!file || !file->IsOpen()) {
00114 Error("makeEventSample", "Couldn;t open file %s", FILENAME);
00115 return 1;
00116 }
00117
00118
00119 TTree* tree = new TTree(TREENAME, "Pythia 6 tree");
00120
00121
00122
00123 TClonesArray* particles = (TClonesArray*)pythia->GetListOfParticles();
00124 tree->Branch(BRANCHNAME, &particles);
00125
00126
00127 for (Int_t i = 0; i < nEvents; i++) {
00128
00129 if (i % 100 == 0)
00130 cout << "Event # " << i << endl;
00131
00132
00133 pythia->GenerateEvent();
00134
00135
00136
00137
00138
00139
00140 tree->Fill();
00141 }
00142
00143
00144 tree->Print();
00145
00146
00147 TH1D* hist = new TH1D(HISTNAME, "p_{#perp} spectrum for #pi^{+}",
00148 100, 0, 3);
00149 hist->SetXTitle("p_{#perp}");
00150 hist->SetYTitle("dN/dp_{#perp}");
00151 char expression[64];
00152 sprintf(expression,"sqrt(pow(%s.fPx,2)+pow(%s.fPy,2))>>%s",
00153 BRANCHNAME, BRANCHNAME, HISTNAME);
00154 char selection[64];
00155 sprintf(selection,"%s.fKF==%d", BRANCHNAME, PDGNUMBER);
00156 tree->Draw(expression,selection);
00157
00158
00159 hist->Sumw2();
00160 hist->Scale(3 / 100. / hist->Integral());
00161 hist->Fit("expo", "QO+", "", .25, 1.75);
00162 TF1* func = hist->GetFunction("expo");
00163 func->SetParNames("A", "- 1 / T");
00164
00165 file->Write();
00166 file->Close();
00167
00168 return 0;
00169 }
00170
00171
00172 int showEventSample()
00173 {
00174
00175 loadLibraries();
00176
00177
00178 TFile* file = TFile::Open(FILENAME, "READ");
00179 if (!file || !file->IsOpen()) {
00180 Error("showEventSample", "Couldn;t open file %s", FILENAME);
00181 return 1;
00182 }
00183
00184
00185 TTree* tree = (TTree*)file->Get(TREENAME);
00186 if (!tree) {
00187 Error("showEventSample", "couldn't get TTree %s", TREENAME);
00188 return 2;
00189 }
00190
00191
00192 tree->StartViewer();
00193
00194
00195 TH1D* hist = (TH1D*)file->Get(HISTNAME);
00196 if (!hist) {
00197 Error("showEventSample", "couldn't get TH1D %s", HISTNAME);
00198 return 4;
00199 }
00200
00201
00202 gStyle->SetOptStat(1);
00203 TCanvas* canvas = new TCanvas("canvas", "canvas");
00204 canvas->SetLogy();
00205 hist->Draw("e1");
00206 TF1* func = hist->GetFunction("expo");
00207
00208 char expression[64];
00209 sprintf(expression,"T #approx %5.1f", -1000 / func->GetParameter(1));
00210 TLatex* latex = new TLatex(1.5, 1e-4, expression);
00211 latex->SetTextSize(.1);
00212 latex->SetTextColor(4);
00213 latex->Draw();
00214
00215 return 0;
00216 }
00217
00218 void pythiaExample(Int_t n=1000) {
00219 makeEventSample(n);
00220 showEventSample();
00221 }
00222
00223 #ifndef __CINT__
00224 int main(int argc, char** argv)
00225 {
00226 TApplication app("app", &argc, argv);
00227
00228 Int_t n = 100;
00229 if (argc > 1)
00230 n = strtol(argv[1], NULL, 0);
00231
00232 int retVal = 0;
00233 if (n > 0)
00234 retVal = makeEventSample(n);
00235 else {
00236 retVal = showEventSample();
00237 app.Run();
00238 }
00239
00240 return retVal;
00241 }
00242 #endif
00243
00244
00245
00246
00247
00248