// ------------------------------------------------------------------------- // ----- FairAnaSelector source file ----- // ----- Created 14/10/11 by R. Karabowicz ----- // ------------------------------------------------------------------------- #include "FairAnaSelector.h" #include "TFile.h" #include "TProofOutputFile.h" #include "TH2.h" #include "TSystem.h" #include "TStyle.h" #include "TCanvas.h" #include "FairTask.h" #include "FairRunAna.h" #include "FairRuntimeDb.h" #include "FairParRootFileIo.h" #include "FairParAsciiFileIo.h" #include #include #include using std::cout; using std::cerr; using std::endl; using std::pair; using std::setw; using std::left; using std::right; using std::fixed; using std::setprecision; using std::map; //_____________________________________________________________________________ void FairAnaSelector::Init(TTree *tree) { // The Init() function is called when the selector needs to initialize // a new tree or chain. Typically here the branch addresses and branch // pointers of the tree will be set. // It is normally not necessary to make changes to the generated // code, but the routine can be extended by the user if needed. // Init() will be called many times when running on PROOF // (once per file to be processed). // Set branch addresses and branch pointers if (!tree) { cout << "there's no tree in Init()" << endl; return; } else { cout << "got the tree " << tree << endl; tree->Print(); } fChain = tree; // fChain->SetMakeClass(1); // fChain->SetBranchStatus("*",0); // fChain->SetBranchStatus("GEMDigi",1); // fChain->SetBranchStatus("GEMDigi.*",1); // fChain->SetBranchStatus("GEMDigi.fDefaultType",0); // fChain->SetBranchStatus("EventHeader.",1); // fChain->SetBranchStatus("EventHeader.*",1); // fChain->SetBranchStatus("*",0); fChain->SetBranchStatus("*.fDefaultType",0); /* fChain->SetBranchStatus("GEMDigi",1); fChain->SetBranchStatus("GEMDigi.*",1); fChain->SetBranchStatus("GEMPoint",1); fChain->SetBranchStatus("GEMPoint.*",1);*/ /* fChain->SetBranchStatus("GEMPoint.fUniqueID",0); fChain->SetBranchStatus("GEMPoint.fBits",0); fChain->SetBranchStatus("GEMPoint.fDefaultType",0); fChain->SetBranchStatus("GEMPoint.fLinks",0); fChain->SetBranchStatus("GEMPoint.fPersistanceCheck",0); fChain->SetBranchStatus("GEMPoint.fVerbose",0);*/ /* fChain->SetBranchStatus("GEMPoint.fTimeStamp",0); fChain->SetBranchStatus("GEMPoint.fTimeStampError",0); fChain->SetBranchStatus("GEMPoint.fDetectorID",1); fChain->SetBranchStatus("GEMPoint.fX",1); fChain->SetBranchStatus("GEMPoint.fY",1); fChain->SetBranchStatus("GEMPoint.fZ",1); fChain->SetBranchStatus("GEMPoint.fTrackID",0); fChain->SetBranchStatus("GEMPoint.fEventId",0); fChain->SetBranchStatus("GEMPoint.fPx",0); fChain->SetBranchStatus("GEMPoint.fPy",0); fChain->SetBranchStatus("GEMPoint.fPz",0); fChain->SetBranchStatus("GEMPoint.fTime",0); fChain->SetBranchStatus("GEMPoint.fLength",0); fChain->SetBranchStatus("GEMPoint.fELoss",0);*/ /* fChain->SetBranchStatus("GEMPoint.fXfOut",0); fChain->SetBranchStatus("GEMPoint.fYfOut",0); fChain->SetBranchStatus("GEMPoint.fZfOut",0); fChain->SetBranchStatus("GEMPoint.fPxfOut",0); fChain->SetBranchStatus("GEMPoint.fPyfOut",0); fChain->SetBranchStatus("GEMPoint.fPzfOut",0); fChain->SetBranchStatus("GEMPoint.fSensorId",1);*/ // fChain->SetBranchStatus("GEMPoint.*",1); // fChain->SetBranchStatus("GEMPoint.fDefaultType",0); // fChain->SetBranchStatus("MCEventHeader.",1); // fChain->SetBranchStatus("MCEventHeader.*",1); if ( !fRunAna->IsInitialized() ) { cout << "vvvvvvvvvvvvvvvvvvvvvvvvvvvvv fRunAna->Init() vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv" << endl; fRunAna->Init(); cout << "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ fRunAna->Init() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" << endl; } } //_____________________________________________________________________________ //_____________________________________________________________________________ Bool_t FairAnaSelector::Notify() { // The Notify() function is called when a new file is opened. This // can be either for a new TTree in a TChain or when when a new TTree // is started when using PROOF. It is normally not necessary to make changes // to the generated code, but the routine can be extended by the // user if needed. The return value is currently not used. return kTRUE; } //_____________________________________________________________________________ //_____________________________________________________________________________ void FairAnaSelector::Begin(TTree * /*tree*/) { // The Begin() function is called at the start of the query. // When running with PROOF Begin() is only called on the client. // The tree argument is deprecated (on PROOF 0 is passed). cout << "FairAnaSelector::Begin" << endl; TString option = GetOption(); } void FairAnaSelector::SlaveBegin(TTree *tree) { // The SlaveBegin() function is called after the Begin() function. // When running with PROOF SlaveBegin() is called on each slave server. // The tree argument is deprecated (on PROOF 0 is passed). TChain* inputChain = dynamic_cast(fInput->FindObject("cbmsim")); if ( !fRunAna ) { TString vmcPath = gSystem->Getenv("VMCWORKDIR"); TNamed* out = (TNamed*) fInput->FindObject("FAIRRUNANA_fOutputFileName"); TNamed* par1Name = (TNamed*) fInput->FindObject("FAIRRUNANA_fParInput1FName"); TNamed* par2Name = (TNamed*) fInput->FindObject("FAIRRUNANA_fParInput2FName"); TString par1Str = par1Name->GetTitle(); TString par2Str = par2Name->GetTitle(); cout << "FairAnaSelector::SlaveBegin, got chain with " << inputChain->GetEntriesFast() << " entries, output should go to file \"" << out->GetTitle() << "\"" << endl; if ( out ) { cout << "out exists with " << out->GetTitle() << endl; fProofFile = new TProofOutputFile(out->GetTitle(),"M"); } else { fProofFile = new TProofOutputFile("SimpleTreeW.root", "M"); fProofFile->SetOutputFileName(Form("%s/macro/proof/treeFairRunAnaOutput.root",vmcPath.Data())); } TDirectory *savedir = gDirectory; fFile = fProofFile->OpenFile("RECREATE"); if (fFile && fFile->IsZombie()) SafeDelete(fFile); savedir->cd(); TString option = GetOption(); fRunAna = new FairRunAna(); fRunAna->SetProofAnalysis(); fRunAna->SetInChain(inputChain); fRunAna->SetOutputFile(fFile); // ----- Parameter database -------------------------------------------- FairRuntimeDb* rtdb = fRunAna->GetRuntimeDb(); if ( par1Str.Contains(".root") ) { FairParRootFileIo* parInput1 = new FairParRootFileIo(); parInput1->open(par1Str.Data()); rtdb->setFirstInput (parInput1); } if ( par1Str.Contains(".par") ) { FairParAsciiFileIo* parInput1 = new FairParAsciiFileIo(); parInput1->open(par1Str.Data(),"in"); rtdb->setFirstInput (parInput1); } if ( par2Str.Contains(".root") ) { FairParRootFileIo* parInput2 = new FairParRootFileIo(); parInput2->open(par2Str.Data()); rtdb->setSecondInput(parInput2); } if ( par2Str.Contains(".par") ) { FairParAsciiFileIo* parInput2 = new FairParAsciiFileIo(); parInput2->open(par2Str.Data(),"in"); rtdb->setSecondInput(parInput2); } // ------------------------------------------------------------------------ FairTask* ddd = dynamic_cast(fInput->FindObject("FairTaskList")); cout << "we have FairTask \"" << ddd << "\"" << endl; fRunAna->AddTask(ddd); } Init(inputChain); fRunAna->SlaveBegin(); } Bool_t FairAnaSelector::Process(Long64_t entry) { // The Process() function is called for each entry in the tree (or possibly // keyed object in the case of PROOF) to be processed. The entry argument // specifies which entry in the currently loaded tree is to be processed. // It can be passed to either FairAnaSelector::GetEntry() or TBranch::GetEntry() // to read either all or the required parts of the data. When processing // keyed objects with PROOF, the object is already loaded and is available // via the fObject pointer. // // This function should contain the "body" of the analysis. It can contain // simple or elaborate selection criteria, run algorithms on the data // of the event and typically fill histograms. // // The processing can be stopped by calling Abort(). // // Use fStatus to set the return value of TTree::Process(). // // The return value is currently not used. // cout << "--------------------------------------------" << endl; // cout << "getting event " << entry << endl; // cout << "got array " << gemDigiArray << endl; // b_gemDigiArray->GetEntry(entry); // cout << "trying to fChain->GetEvent(" << entry << ")" << endl; // fChain->GetEvent(entry); // cout << "got event" << endl; fRunAna->RunEntry(entry); return kTRUE; } void FairAnaSelector::SlaveTerminate() { // The SlaveTerminate() function is called after all entries or objects // have been processed. When running with PROOF SlaveTerminate() is called // on each slave server. cout << "fRunAna->SlaveTerminate()" << endl; fRunAna->SlaveTerminate(); cout << "fProofFile = " << fProofFile << " and fFile = " << fFile << endl; if ( fProofFile ) { cout << "fOutput->Add(fProofFile);" << endl; fOutput->Add(fProofFile); cout << "fFile->Close()" << endl; fFile->Close(); TString vmcPath = gSystem->Getenv("VMCWORKDIR"); // TString outName = Form("%s/macro/proof/Gem_3Stations_15GeV_n1000_hitsSELECTOR.root",vmcPath.Data()); cout << "fFile->GetName() = " << fFile->GetName() << endl; TString fileName = fFile->GetName(); if ( fileName.Contains("0.0") ) { TString command = Form("cp %s %s/macro/proof/worker0.0_FRA.root",fileName.Data(),vmcPath.Data()); gSystem->Exec(command.Data()); } if ( fileName.Contains("0.1") ) { TString command = Form("cp %s %s/macro/proof/worker0.1_FRA.root",fileName.Data(),vmcPath.Data()); gSystem->Exec(command.Data()); } if ( fileName.Contains("0.2") ) { TString command = Form("cp %s %s/macro/proof/worker0.2_FRA.root",fileName.Data(),vmcPath.Data()); gSystem->Exec(command.Data()); } if ( fileName.Contains("0.3") ) { TString command = Form("cp %s %s/macro/proof/worker0.3_FRA.root",fileName.Data(),vmcPath.Data()); gSystem->Exec(command.Data()); } } cout << "finishing SlaveTerminate()" << endl; } void FairAnaSelector::Terminate() { // The Terminate() function is the last function to be called during // a query. It always runs on the client, it can be used to present // the results graphically or save the results to file. cout << "TERMINATE FairAnaSelector IN BASE" << endl; cout << "*********(((((((((((((((((**********" << endl; fOutput->ls(); cout << "*********)))))))))))))))))**********" << endl; /* // Get the ntuple form the file if ((fProofFile = dynamic_cast(fOutput->FindObject("SimpleTreeW.root")))) { TString outputFile = "/Users/konglaide/panda/pandaSEP11/trunk/macro/gem/FairAnaSelectorOutput.root"; // TString outputName(fProofFile->GetName()); cout << "outputFile \"" << outputFile.Data() << "\"" << endl; // cout << "outputName \"" << outputName.Data() << "\"" << endl; // outputName += ".root"; Printf("outputFile: %s", outputFile.Data()); // Read the ntuple from the file fFile = TFile::Open(outputFile); if (fFile) { Printf("Managed to open file: %s", outputFile.Data()); fOutTree = (TTree *) fFile->Get("demoTree"); } else { Error("Terminate", "could not open file: %s", outputFile.Data()); } if (!fFile) return; } else { cout << "no file in fOutput" << endl; Error("Terminate", "TProofOutputFile not found"); return; }*/ }