00001 #include "AtNuDataNtuple.h"
00002 #include "AtNuRunTimes.h"
00003
00004 #include "AtNuEvent/AtmosEvent.h"
00005
00006 #include <iostream>
00007
00008 ClassImp(AtNuDataNtuple)
00009
00010 AtNuDataNtuple::AtNuDataNtuple() :
00011 fChain(0),
00012 fEvent(0)
00013 {
00014 this->Reset();
00015 }
00016
00017 AtNuDataNtuple::~AtNuDataNtuple()
00018 {
00019
00020 }
00021
00022 void AtNuDataNtuple::Reset()
00023 {
00024 if( !fChain ){
00025 fChain = new TChain("ntp","chain");
00026 }
00027
00028 fChain->Reset();
00029 fChain->SetBranchAddress("evt",&fEvent);
00030
00031 fAtNuDataFile = 0;
00032 fAtNuDataTree = 0;
00033
00034 fFileName = "ntpcheck.root";
00035
00036 return;
00037 }
00038
00039 void AtNuDataNtuple::AddFile(const char* file)
00040 {
00041 std::cout << " adding files from: " << file << std::endl;
00042
00043 fChain->Add(file);
00044
00045 std::cout << " ... total entries=" << fChain->GetEntries() << std::endl;
00046
00047 return;
00048 }
00049
00050 void AtNuDataNtuple::SetFileName(const char* file)
00051 {
00052 std::cout << " *** AtNuDataNtuple::SetFileName(...) *** " << std::endl;
00053
00054 fFileName = file;
00055
00056 std::cout << " output file = " << fFileName.Data() << std::endl;
00057 }
00058
00059 void AtNuDataNtuple::OpenFile()
00060 {
00061 if( !fAtNuDataFile ){
00062 std::cout << " opening file: " << fFileName.Data() << std::endl;
00063 TDirectory* tmpd = gDirectory;
00064 fAtNuDataFile = new TFile(fFileName.Data(),"recreate");
00065 fAtNuDataTree = new TTree("ntuple","minos monitor ntuple");
00066 fAtNuDataTree->SetDirectory(fAtNuDataFile);
00067
00068 fAtNuDataTree->Branch("run",&fRun,"run/I");
00069 fAtNuDataTree->Branch("subrun",&fSubRun,"subrun/I");
00070 fAtNuDataTree->Branch("unixtime",&fUnixTime,"unixtime/I");
00071 fAtNuDataTree->Branch("date",&fDate,"date/I");
00072 fAtNuDataTree->Branch("time",&fTime,"time/I");
00073 fAtNuDataTree->Branch("rate",&fRate,"rate/I");
00074 fAtNuDataTree->Branch("good",&fGood,"good/I");
00075
00076 fAtNuDataTree->SetAutoSave(100);
00077 gDirectory = tmpd;
00078 }
00079 }
00080
00081 void AtNuDataNtuple::CloseFile()
00082 {
00083 if( fAtNuDataFile ){
00084 std::cout << " closing file: " << fAtNuDataFile->GetName() << std::endl;
00085 TDirectory* tmpd = gDirectory;
00086 fAtNuDataFile->cd();
00087 fAtNuDataTree->Write();
00088 fAtNuDataFile->Close();
00089 gDirectory = tmpd;
00090 }
00091 }
00092
00093 void AtNuDataNtuple::WriteToFile()
00094 {
00095 if( fAtNuDataFile ){
00096 TDirectory* tmpd = gDirectory;
00097 fAtNuDataFile->cd();
00098 fAtNuDataTree->Fill();
00099 gDirectory = tmpd;
00100 }
00101 }
00102
00103 void AtNuDataNtuple::Run()
00104 {
00105 std::cout << " *** AtNuDataNtuple::Run() *** " << std::endl;
00106
00107
00108 this->OpenFile();
00109
00110
00111 fUnixTime = -1;
00112 fRun = -1;
00113 fSubRun = -1;
00114 fDate = -1;
00115 fTime = -1;
00116 fRate = 0;
00117 fGood = 0;
00118
00119 for( Int_t i=0; i<fChain->GetEntries(); i++ ){
00120 fChain->GetEntry(i);
00121
00122 if( !(fEvent->Run==fRun
00123 && fEvent->SubRun==fSubRun) ){
00124 std::cout << " Run: " << fEvent->Run << std::endl;
00125 }
00126
00127 if( fEvent->UnixTime!=fUnixTime ){
00128
00129 if( fUnixTime>=0 ){
00130 this->WriteToFile();
00131 }
00132
00133 fUnixTime = fEvent->UnixTime;
00134 fRun = fEvent->Run;
00135 fSubRun = fEvent->SubRun;
00136 fDate = fEvent->Date;
00137 fTime = fEvent->Time;
00138 fRate = 0;
00139 fGood = AtNuRunTimes::GoodData(fUnixTime);
00140 }
00141
00142 fRate++;
00143 }
00144
00145
00146 if( fUnixTime>=0 ){
00147 this->WriteToFile();
00148 }
00149
00150
00151 this->CloseFile();
00152
00153 return;
00154 }