//////////////////////////////////////////////////////////////////// /// \file example_analysis.cc /// /// \brief Example of how to open and analysis a RAT ntuple file /// /// \author T Kaptanoglu /// /// REVISION HISTORY:\n /// 2019-06-29 T Kaptanoglu - Initial version /// /// \details /// Can be run in the root interpreter by doing: /// $ root /// root [0] .L example_analysis.cc++ /// root [1] example_analysis("name_of_ntuple_file") //////////////////////////////////////////////////////////////////// #include #include #include #include #include // Example datacleaning mask const ULong64_t ANALYSIS_MASK = 0xFB0000017FFE; bool is_monte_carlo(char* filename){ /* Check if the file is data or Monte Carlo */ TFile *f = TFile::Open(filename); RAT::DS::Meta *m = (RAT::DS::Meta*)f->Get("meta;"); int currentPass = m->GetCurrentPass(); std::vector test = m->GetContainsMCFlags(); bool mC = test[currentPass]; if(!mC){ printf("Analyzing data.\n"); } f->Close(); delete f; return mC; } double get_counts_meta(char* filename){ /* For Monte Carlo we get the number of simulated events from the meta information */ TFile *f = TFile::Open(filename); RAT::DS::Meta *m = (RAT::DS::Meta*)f->Get("meta;"); int currentPass = m->GetCurrentPass(); std::vector eventsG = m->GetEventsGeneratedCounts(); int gC = eventsG[currentPass]; printf("Analyzing %d simulated events.\n", gC); f->Close(); delete f; return gC; } void example_analysis(char* filename){ /* Open up an ntuple file and perform some analysis. @param filename: The name of the ntuple file */ // Open up the data or the MC for analysis TFile *f = TFile::Open(filename); TTree *t = (TTree*)f->Get("output"); ULong64_t dataCleaningMask; int runID, gtid; double energy; double posr; bool fitValid; // Documentation on what is contained within the standard // ntuple files can be found here: // https://snopl.us/docs/rat/user_manual/html/standard_ntuples.html t->SetBranchAddress("dcFlagged", &dataCleaningMask); t->SetBranchAddress("runID",&runID); t->SetBranchAddress("eventID",>id); t->SetBranchAddress("energy",&energy); t->SetBranchAddress("posr", &posr); t->SetBranchAddress("fitValid",&fitValid); // Whether or not we have a monte carlo file bool isMC = is_monte_carlo(filename); // Number of simulated events, for monte carlo if(isMC){ int nCounts = get_counts_meta(filename); } for(int iEV = 0; iEV < t->GetEntries(); iEV++){ t->GetEntry(iEV); // Apply data-cleaning if we have a data. Do not apply for monte carlo. if(!isMC && !((dataCleaningMask & ANALYSIS_MASK) == ANALYSIS_MASK)){ continue; } // Check we have a valid fit. if(!fitValid){ continue; } // Rest of the analysis code if(gtid == 0){ printf("Run %d event %d has energy %.2f MeV and position %.2f mm\n", runID, gtid, energy, posr); } } f->Close(); }