source: trunk/test/MatchingSTDHEPConverter.cpp@ 9

Last change on this file since 9 was 2, checked in by Pavel Demin, 16 years ago

first commit

File size: 4.8 KB
RevLine 
[2]1#include <iostream>
2#include <sstream>
3#include <fstream>
4
5#include <stdlib.h>
6
7#include "stdhep_mcfio.h"
8#include "stdhep_declarations.h"
9
10#include "TROOT.h"
11#include "TApplication.h"
12
13#include "TFile.h"
14#include "TChain.h"
15#include "TString.h"
16
17#include "TH2.h"
18#include "THStack.h"
19#include "TLegend.h"
20#include "TPaveText.h"
21#include "TLorentzVector.h"
22
23#include "LHEF.h"
24
25#include "ExRootAnalysis/ExRootClasses.h"
26
27#include "ExRootAnalysis/ExRootTreeWriter.h"
28#include "ExRootAnalysis/ExRootTreeBranch.h"
29
30#include "ExRootAnalysis/ExRootUtilities.h"
31#include "ExRootAnalysis/ExRootProgressBar.h"
32
33using namespace std;
34
35//---------------------------------------------------------------------------
36
37static void AnalyseEvent(ExRootTreeBranch *branch, Long64_t eventNumber)
38{
39 ExRootGenEvent *element;
40
41 element = static_cast<ExRootGenEvent*>(branch->NewEntry());
42
43 element->Number = eventNumber;
44}
45
46//---------------------------------------------------------------------------
47
48static void AnalyseParticles(ExRootTreeBranch *branch)
49{
50 ExRootGenParticle *element;
51
52 Double_t signPz;
53 TLorentzVector momentum;
54 Int_t number;
55
56 for(number = 0; number < myhepevt.nhep; ++number)
57 {
58
59 element = static_cast<ExRootGenParticle*>(branch->NewEntry());
60
61 element->PID = myhepevt.idhep[number];
62 element->Status = myhepevt.isthep[number];
63 element->M1 = myhepevt.jmohep[number][0] - 1;
64 element->M2 = myhepevt.jmohep[number][1] - 1;
65 element->D1 = myhepevt.jdahep[number][0] - 1;
66 element->D2 = myhepevt.jdahep[number][1] - 1;
67
68 element->E = myhepevt.phep[number][3];
69 element->Px = myhepevt.phep[number][0];
70 element->Py = myhepevt.phep[number][1];
71 element->Pz = myhepevt.phep[number][2];
72
73 momentum.SetPxPyPzE(element->Px, element->Py, element->Pz, element->E);
74 element->PT = momentum.Perp();
75 signPz = (element->Pz >= 0.0) ? 1.0 : -1.0;
76 element->Eta = element->PT == 0.0 ? signPz*999.9 : momentum.Eta();
77 element->Phi = momentum.Phi();
78
79 element->Rapidity = element->PT == 0.0 ? signPz*999.9 : momentum.Rapidity();
80
81 element->T = myhepevt.vhep[number][3];
82 element->X = myhepevt.vhep[number][0];
83 element->Y = myhepevt.vhep[number][1];
84 element->Z = myhepevt.vhep[number][2];
85 }
86}
87
88//---------------------------------------------------------------------------
89
90int main(int argc, char *argv[])
91{
92 int ierr, entryType;
93 int istr = 0;
94 int nevt = 0;
95 char *appName = "ExRootSTDHEPConverter";
96
97 if(argc != 4)
98 {
99 cout << " Usage: " << appName << " input_file";
100 cout << " output_root_file" << " output_text_file" << endl;
101 cout << " input_file - input file in STDHEP format," << endl;
102 cout << " output_root_file - output file in ROOT format." << endl;
103 cout << " output_text_file - output file in TEXT format." << endl;
104 return 1;
105 }
106
107 gROOT->SetBatch();
108
109 int appargc = 1;
110 char *appargv[] = {appName};
111 TApplication app(appName, &appargc, appargv);
112
113 // Open a stream connected to an event file:
114 char inputFileName[80];
115 strcpy(inputFileName, argv[1]);
116 ierr = StdHepXdrReadInit(inputFileName, &nevt, istr);
117
118 if(ierr != 0)
119 {
120 cerr << "** ERROR: Can't open '" << argv[1] << "' for input" << endl;
121 return 1;
122 }
123
124 Long64_t allEntries = nevt;
125 cout << "** Input file contains " << allEntries << " entries" << endl;
126
127 TFile *outputRootFile = TFile::Open(argv[2], "RECREATE");
128 ExRootTreeWriter *treeWriter = new ExRootTreeWriter(outputRootFile, "STDHEP");
129
130 ofstream outputTextFile(argv[3], ios::out);
131
132 if(outputTextFile == 0)
133 {
134 cerr << "** ERROR: Can't open '" << argv[3] << "' for ouput" << endl;
135 return 1;
136 }
137
138 // information about generated event
139 ExRootTreeBranch *branchGenEvent = treeWriter->NewBranch("Event", ExRootGenEvent::Class());
140 // generated particles from HEPEVT
141 ExRootTreeBranch *branchGenParticle = treeWriter->NewBranch("GenParticle", ExRootGenParticle::Class());
142
143 if(allEntries > 0)
144 {
145 ExRootProgressBar progressBar(allEntries);
146
147 // Loop over all objects
148 Long64_t entry = 0;
149 Long64_t recordNumber = 1;
150 for(entry = 0; entry < allEntries; ++entry)
151 {
152 ierr = StdHepXdrRead(&entryType, istr);
153
154 if(ierr != 0)
155 {
156 cerr << "** ERROR: Unexpected end of file after " << entry << " entries" << endl;
157 break;
158 }
159
160 if(entryType == 200)
161 {
162 outputTextFile << "$stdxsec = " << stdcm1_.stdxsec << ";" <<endl;
163 }
164
165 // analyse only entries with standard HEPEVT common block
166 if(entryType == 1)
167 {
168 treeWriter->Clear();
169
170 AnalyseEvent(branchGenEvent, myhepevt.nevhep);
171 AnalyseParticles(branchGenParticle);
172
173 treeWriter->Fill();
174
175 ++recordNumber;
176
177 }
178
179 progressBar.Update(entry);
180 }
181
182 progressBar.Finish();
183 }
184
185 treeWriter->Write();
186
187 cout << "** Exiting..." << endl;
188
189 outputTextFile.close();
190 delete treeWriter;
191 StdHepXdrEnd(istr);
192}
193
Note: See TracBrowser for help on using the repository browser.