-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReplayCore64.C
executable file
·415 lines (361 loc) · 10.5 KB
/
ReplayCore64.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//////////////////////////////////////////////////////////////////////////
//
// Hall A analyzer replay script core
//
// https://hallaweb.jlab.org/wiki/index.php/ReplayCore
//
// Call this script after loading all the detectors to run replay
//
// if there is no rootlogon.C, execute following command in analyzer first
// gSystem->Load("<your lib name>.so");
// if you want to compile this script then you will also need
// gSystem->AddIncludePath("-I<your include path>");
// gInterpreter->AddIncludePath("<your include path>");
//
//////////////////////////////////////////////////////////////////////////
//
// Author : Jin Huang ([email protected]) Aug 2007
//
// Modify History:
// Mar 2008 Jin Huang
// add physics module support
// Mar 2008 Jin Huang
// separate two modes: physics replay and detector replay
// also make it smarter
// Apr 2008 Jin Huang
// rootfile overwrite proof
// May 2008 Jin Huang
// Separate core code of replay script to ReplayCore.C
// May 2008 Jin Huang
// Support Start Analyzing from an arbitary event number
// Support Exit when getting an invalid answer.
// Fix Bugs
// Oct 2008 Jin Huang
// Support raw data file name which do not have an endfix index
// Nov 2008 Jin Huang
// Clear Cut list before run
// Nov 2008 Jin Huang
// Support >2G file size on older compilers
// Dec 2008 Jin Huang
// Auto Runs
//
// Feb 2011 Zhihong Ye
// Add Helicity
//
// Dec 2011 Ole Hansen
// Support compilation via ROOT's ACLic.
// Add exception handling.
//
//////////////////////////////////////////////////////////////////////////
#ifndef ROOT_REPLAY_CORE
#define ROOT_REPLAY_CORE
#include "def_coinc.h"
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <stdexcept>
#include <cassert>
#ifndef __CINT__
//header file for compile
#include "TSystem.h"
#include "TROOT.h"
//#include "THaEvent.h" // no longer needed
#include "THaRun.h"
#include "THaAnalyzer.h"
#include "THaGlobals.h"
#include "THaCutList.h"
#include "TTree.h"
#include "TFile.h"
#include "TString.h"
#endif//#ifdef __CINT__
Bool_t IsFileExist(const Char_t * fname);
using namespace std;
void ReplayCore(
Int_t runnumber=0, //run #
Int_t all=0, //-1=replay all;0=ask for a number
Int_t DefReplayNum=-1, //default replay event num
char* OutFileFormat="%s/left_gmp_%d.root", //output file format
char* OutDefineFile="HRS.odef", //out define
char* CutDefineFile="HRS.cdef", //cut define
Bool_t EnableScalar=false, //Enable Scalar?
Bool_t EnableHelicity=false, //Enable Helicity?
Int_t FirstEventNum=0, //First Event To Replay
Bool_t QuietRun = kFALSE //whether not ask question?
)
{
//general replay script core
//
// to do replay, use a script to load detectors then call this function
//
// it's also convenient to load detector manually in analyzer
// command line then call me
// step 1: Init analyzer
cout<<"replay: Init analyzer ..."<<endl;
THaAnalyzer* analyzer = THaAnalyzer::GetInstance();
if( analyzer ) {
analyzer->Close();
} else {
analyzer = new THaAnalyzer;
}
gHaCuts->Clear();
//Enable scalers
if (EnableScalar)
{
cout<<"replay: Enabling Scalars"<<endl;
analyzer->EnableScalers();
}
//Enable Helicity
if (EnableHelicity)
{
cout<<"replay: Enabling Helicity"<<endl;
analyzer->EnableHelicity();
}
// step 2: setup run information
cout<<"replay: configuring analyzer ..."<<endl;
int nrun, nev;
int found = 0;
const char** path = 0;
char filename[300],buf[300];
while( found==0 ) {
if (runnumber<=0)
{
if (QuietRun)
break;
else
{
cout << "\nreplay: Please enter a Run Number (-1 to exit):";
cin >> nrun;
fgets(buf,300,stdin);//get the extra '\n' from stdin
if( nrun<=0 ) break;
}
}
else nrun=runnumber;
path=PATHS;
// if (nrun < 15000) RAW_DATA_FORMAT="%s/dvcs14_%d.dat.%d";
while ( path && *path ) {
sprintf(filename,RAW_DATA_FORMAT,*path,nrun,0);
//if (nrun < 15000) sprintf(filename,"%s/dvcs14_%d.dat.%d",*path,nrun,0);
cout<<"replay: Try file "<<filename<<endl;
if (IsFileExist(filename)) {
found = 1;
cout << "replay: Will analyze file "<<filename<<endl;
break;
}
path++;
}
if ( (!path || !*path) && !found ) {
cout << "replay: File not found. \n";
if (runnumber>0) runnumber=0;
}
}
if(nrun<=0) {
gHaApps->Delete();
gHaPhysics->Delete();
gHaScalers->Delete();
analyzer->Close();
return;
}
if (all==0)
{
if (QuietRun)
{
nev=DefReplayNum;
}
else
{
cout << "\nreplay: Number of Events to analyze (use default for online check, -1 = replay all) "
<<" (default=";
if (DefReplayNum>0)
cout<<DefReplayNum<<"):";
else
cout<<"replay all):";
//trick to support blank inputs
fgets(buf,300,stdin);
if (sscanf(buf,"%d\n",&nev)<1)
nev=DefReplayNum;
}
}
else
nev=all;
char outname[300];
sprintf(outname,OutFileFormat,STD_REPLAY_OUTPUT_DIR,nrun);
found=0;
//rootfile overwrite proof
while (found==0 && !QuietRun)
{
cout << "replay: Testing file "<<outname<<" for overwrite proof."<<endl;
if (!IsFileExist(outname)) {found=1;break;}
else
{
Long64_t NEnt=0;
TFile *f=new TFile(outname);
if (f)
{
TTree *t=(TTree *)f->Get("T");
if (t!=0) NEnt = (t->GetEntries());
}
delete f;
TString DefOverWriting;
if (NEnt==DefReplayNum || DefReplayNum<0 || NEnt==0)
DefOverWriting="no";
else DefOverWriting="yes";
if (NEnt<=0) {
cout <<endl << "replay: "<<outname
<<" is an invalid root file, "
<<"or other people is replaying it. ";
}
else{
cout << "replay: "<<outname<<", which contains "<<NEnt
<<" events, already exists. ";
}
cout<<"Do you want to overwrite it? "
<<"(default="<<DefOverWriting.Data()<<"; enter \"c\" means exit):";
//trick to support blank inputs
fgets(buf,300,stdin);
TString s(buf);
s.Chop();
s.ToLower();
if (s.IsNull()) s=DefOverWriting;
if (s=="y" || s=="yes") {
found=1;
// Longwu: add to remove old rootfiles
TString rootfilebase = outname, rootfilename = outname;
rootfilebase.Remove(rootfilebase.Last('.'),5);
Long_t split = 0;
while ( !gSystem->AccessPathName(rootfilename.Data()) ) {
cout << "Removing old ROOT file " << rootfilename.Data() << endl;
gSystem->Exec(Form("rm %s",rootfilename.Data()));
split++;
rootfilename = rootfilebase + "_" + split + ".root";
}
break;
}
else if (s=="n" || s=="no"){
sprintf(outname,OutFileFormat,
CUSTOM_REPLAY_OUTPUT_DIR,nrun);
cout<<endl
<<"replay: please enter the output root file name. "<<endl
<<" leave blank = "<<outname<<endl
<<"Root File:";
//trick to support blank inputs
fgets(buf,300,stdin);
if (buf[0]!='\n' and buf[0]!=0){
strcpy(outname,buf);
}
//clear the last char if it's '\n'
if (outname[strlen(outname)-1]=='\n') outname[strlen(outname)-1]=0;
}
else {
cout<<"replay: "
<<s.Data()
<<"is not a valid input; Exiting."
<<endl;
gHaApps->Delete();
gHaPhysics->Delete();
analyzer->Close();
return;
}
}
}
cout<<endl<<"----------------------------------------------"<<endl;
cout<<"replay: Inputs Summary:"<<endl;
cout<<" Raw data: "<<filename<<endl;
cout<<" Event : ";
if (nev<0) cout <<"all events";
else cout << nev <<" events";
cout<<", starting from Event # "<<FirstEventNum<<endl;
cout<<" Outputs : "<<outname<<endl;
cout<<"----------------------------------------------"<<endl<<endl;
cout<<"replay: Setup run inputs/outputs ..."<<endl;
analyzer->SetOutFile( outname );
analyzer->SetOdefFile(OutDefineFile);
analyzer->SetCutFile(CutDefineFile);
char sumname[300];
sprintf(sumname,SUMMARY_PHYSICS_FORMAT,nrun);
analyzer->SetSummaryFile(sumname); // optional
// analyzer->SetEvent( new THaEvent ); // no longer needed
//analyzer->SetMarkInterval(ANA_MARK_INTERVAL);
//correct the offset on the last event if first event is above 0
if (nev>=0) nev+=FirstEventNum;
// step 3: run it
cout<<"replay: Start Process ..."<<endl;
TString oldfilename="";
THaRun *oldrun=0, *run, *runlist[30]={0};Int_t runidx=0;
Bool_t exit=false;
//UInt_t NeveAna=0;
for (Int_t nsplit=0;!exit;nsplit++)
{
sprintf(filename,RAW_DATA_FORMAT,"raw data paths",nrun,nsplit);
cout<<"replay: trying file "<<filename<<endl;
path=PATHS;found=0;
while ( path && *path ) {
sprintf(filename,RAW_DATA_FORMAT,*path,nrun,nsplit);
cout<<"replay: Try file "<<filename<<endl;
if (IsFileExist(filename)) {
found = 1;
cout << "replay: Will analyze file "<<filename<<endl;
break;
}
path++;
}
//cout<<"======================\n"<<oldfilename<<endl<<filename<<endl;
if ( ((!path || !*path) && !found) || oldfilename==filename) {
cout << "replay: no more raw data file to analyze"<<endl;
exit=true;
}
else{
oldfilename=filename;
//do the analysis
if( oldrun ) {
run = new THaRun(*oldrun);
run->SetFilename(filename);
} else {
run = new THaRun(filename);
}
runlist[runidx]=run; runidx++;
if(nev>=0) run->SetLastEvent(nev);
run->SetFirstEvent(FirstEventNum);
//run->Print();
try {
analyzer->Process(run);
}
catch( exception& e ) {
cerr << "Unhandled exception during replay: " << e.what() << endl;
cerr << "Exiting." << endl;
run->Close();
break;
}
//NeveAna=run->GetNumAnalyzed();
//cout << "replay: "<<NeveAna<<"events were analyzed."<<endl;
//if (NeveAna-1>=(UInt_t)nev) exit=true;
run->Close();
if (!oldrun) oldrun = run;
//else delete run;
}
}
// step 3: clean up
cout<<"replay: Cleaning up ... "<<endl;
for (runidx--;runidx>=0;runidx--){
assert(runlist[runidx]);
delete runlist[runidx];
}
gHaApps->Delete();
gHaPhysics->Delete();
gHaScalers->Delete();
analyzer->Close();
cout<<"replay: YOU JUST ANALYZED RUN number "<<nrun<<"."<<endl;
}
/////////////////////////////////////////////////////////////////
Bool_t IsFileExist(const Char_t * fname)
{
fstream testfile;
testfile.open(fname,ios_base::in);
Bool_t isopen=testfile.is_open();
testfile.close();
return isopen;
}
// DO NOT DELETE!
#endif//#define ROOT_REPLAY_CORE