er  dev
ERRunAna.cxx
1 /********************************************************************************
2  * Copyright (C) Joint Institute for Nuclear Research *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence version 3 (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 #include "ERRunAna.h"
9 
10 #include <iostream>
11 #include "signal.h"
12 
13 #include "TObjArray.h"
14 #include "TG4RunConfiguration.h"
15 #include "TGeant4.h"
16 
17 #include "FairGeoLoader.h"
18 #include "FairGeoInterface.h"
19 #include "FairLogger.h"
20 #include "FairEventHeader.h"
21 #include "FairTask.h"
22 #include "FairTrajFilter.h"
23 
24 #include "ERRecoMCApplication.h"
25 
26 using namespace std;
27 
28 ERRunAna* ERRunAna::fInstance = NULL;
29 TCut ERRunAna::fUserCut = "";
30 TH1I* ERRunAna::fEventsForProcessing = NULL;
31 Bool_t gFRAIsInterrupted;
32 //--------------------------------------------------------------------------------------------------
33 ERRunAna* ERRunAna::Instance() {
34  if (!fInstance) {
35  fInstance = new ERRunAna();
36  }
37  return fInstance;
38 }
39 //--------------------------------------------------------------------------------------------------
40 ERRunAna::ERRunAna()
41 : FairRunAna(),
42 fHoldEventsCount(kFALSE)
43 {
44  LOG(DEBUG) << "ERRunAna constructor" << FairLogger::endl;
45 }
46 //--------------------------------------------------------------------------------------------------
47 void ERRunAna::Init(){
48 
49  FairGeoLoader* loader=new FairGeoLoader("TGeo", "Geo Loader");
50  FairGeoInterface* GeoInterFace=loader->getGeoInterface();
51  GeoInterFace->SetNoOfSets(0);
52  TString erPath = gSystem->Getenv("VMCWORKDIR");
53  GeoInterFace->setMediaFile(erPath+"/geometry/media.geo");
54  GeoInterFace->readMedia();
55  ERRecoMCApplication* fApp= new ERRecoMCApplication("Fair","The Fair VMC App",new TObjArray(), erPath+"/geometry/media.geo");
56  FairRunAna::Init();
57  // initialisation of FairRootManager for getting tree to implement
58  // user cuts for input data
59  FairRootManager* ioman = FairRootManager::Instance();
60  if ( ! ioman ) Fatal("Init", "No FairRootManager");
61  if (fUserCut != "") {
62  LOG(INFO) << "User cut " << fUserCut << " implementation" << FairLogger::endl;
63  TTree* tree = ioman->GetInTree();
64  fEventsForProcessing = new TH1I ("hist", "Events for processing", tree->GetEntries(), 1, tree->GetEntries());
65  tree->Draw("Entry$>>hist",fUserCut,"goff");
66  if (!fEventsForProcessing->GetEntries()) {
67  LOG(FATAL) << "ERRunAna: No data for analysis with defined user cut: "
68  << fUserCut << FairLogger::endl;
69  return;
70  }
71  TCut cut = fUserCut;
72  cut.Write();
73  }
74  TG4RunConfiguration* runConfiguration
75  = new TG4RunConfiguration("geomRoot", "emStandard", "specialCuts+stackPopper");
76  if (!fWithoutGeant) {
77  TGeant4* geant4 = new TGeant4("TGeant4", "The Geant4 Monte Carlo", runConfiguration);
78 
79  geant4->ProcessGeantMacro(erPath+"/gconfig/g4config.in");
80  geant4->Init();
81  geant4->ProcessRun(0);
82  }
83 }
84 
85 //--------------------------------------------------------------------------------------------------
86 void FRA_handler_ctrlc(int)
87 {
88  LOG(INFO) << "*********** CTRL C PRESSED *************";
89  gFRAIsInterrupted = kTRUE;
90 }
91 
92 //--------------------------------------------------------------------------------------------------
93 void ERRunAna::Run(Int_t Ev_start, Int_t Ev_end)
94 {
95  gFRAIsInterrupted = kFALSE;
96 
97  if (fTimeStamps) {
98  RunTSBuffers();
99  } else {
100  UInt_t tmpId =0;
101  // if (fInputFile==0) {
102  if (!fInFileIsOpen) {
103  DummyRun(Ev_start,Ev_end);
104  return;
105  }
106 
107  Int_t MaxAllowed=fRootManager->CheckMaxEventNo(Ev_end);
108  if ( MaxAllowed != -1 ) {
109  if (Ev_end==0) {
110  if (Ev_start==0) {
111  Ev_end=MaxAllowed;
112  } else {
113  Ev_end = Ev_start;
114  if ( Ev_end > MaxAllowed ) {
115  Ev_end = MaxAllowed;
116  }
117  Ev_start=0;
118  }
119  } else {
120  if (Ev_end > MaxAllowed) {
121  cout << "-------------------Warning---------------------------" << endl;
122  cout << " -W FairRunAna : File has less events than requested!!" << endl;
123  cout << " File contains : " << MaxAllowed << " Events" << endl;
124  cout << " Requested number of events = " << Ev_end << " Events"<< endl;
125  cout << " The number of events is set to " << MaxAllowed << " Events"<< endl;
126  cout << "-----------------------------------------------------" << endl;
127  Ev_end = MaxAllowed;
128  }
129  }
130  LOG(INFO) << "FairRunAna::Run() After checking, the run will run from event " << Ev_start << " to " << Ev_end << ".";
131  }
132  else {
133  LOG(INFO) << "FairRunAna::Run() continue running without stop";
134  }
135 
136  Int_t readEventReturn = 0;
137 
138  for (int i=Ev_start; i< Ev_end || MaxAllowed==-1 ; i++) {
139 
140  if (!ContentForAnalysis(i)) {
141  continue;
142  }
143 
144  gSystem->IgnoreInterrupt();
145  // gFRAIsInterrupted = kFALSE;
146  signal(SIGINT, FRA_handler_ctrlc);
147 
148  if ( gFRAIsInterrupted ) {
149  LOG(WARNING) << "FairRunAna::Run() Event loop was interrupted by the user!";
150  break;
151  }
152 
153  readEventReturn = fRootManager->ReadEvent(i);
154 
155  if ( readEventReturn != 0 ) {
156  LOG(WARNING) << "FairRunAna::Run() fRootManager->ReadEvent(" << i << ") returned " << readEventReturn << ". Breaking the event loop";
157  break;
158  }
159 
160  fRootManager->FillEventHeader(fEvtHeader);
161 
162  tmpId = fEvtHeader->GetRunId();
163  if ( tmpId != fRunId ) {
164  fRunId = tmpId;
165  if ( !fStatic ) {
166  Reinit( fRunId );
167  fTask->ReInitTask();
168  }
169  }
170  //std::cout << "WriteoutBufferData with time: " << fRootManager->GetEventTime();
171  fRootManager->StoreWriteoutBufferData(fRootManager->GetEventTime());
172  fTask->ExecuteTask("");
173  Fill();
174  fRootManager->DeleteOldWriteoutBufferData();
175  fTask->FinishEvent();
176 
177  if (NULL != FairTrajFilter::Instance()) {
178  FairTrajFilter::Instance()->Reset();
179  }
180 
181  }
182 
183  fRootManager->StoreAllWriteoutBufferData();
184  fTask->FinishTask();
185  fRootManager->LastFill();
186  fRootManager->Write();
187  }
188 }
189 //--------------------------------------------------------------------------------------------------
190 bool ERRunAna::ContentForAnalysis(Int_t iEvent) {
191  if (fUserCut != "") {
192  if (!fEventsForProcessing->GetBinContent(iEvent)){
193  LOG(INFO) << " Skip event with user cut"<< FairLogger::endl;
194  return kFALSE;
195  }
196  }
197  return kTRUE;
198 }
199 //--------------------------------------------------------------------------------------------------
200 void ERRunAna::MarkFill(Bool_t flag){
201  if (!fHoldEventsCount)
202  fMarkFill = flag;
203 }
204 ClassImp(ERRunAna)
205 
void Run(Int_t NStart=0, Int_t NStop=0)
Definition: ERRunAna.cxx:93