//a simple code (template) for offline analysis
//made by Laszlo, serves as a simple demonstration for enthusiastic shifters
//it creates a "no double counting Si 2D pos" histo
//usage:
//
//save the file as eg. "simple_code.c"
//root -l
//.L simple_code.c++
//run()
//when counter finished: ".q"
#define INPUT1 "input.root"//first unpack the lmd, then give the path of the unpacked .root file.
#define OUTPUT "./"//folder of the output. minimum input: "./"
#define ROOT_NAME "dummy.root"//name of the output
#include <cmath>
#include <string>
#include <sstream>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <fstream>
#include <iostream>
#include <stdint.h>
#include "TROOT.h"
#include "TAttText.h"
#include "TAxis.h"
#include "TCanvas.h"
#include "TChain.h"
#include "TCut.h"
#include "TF1.h"
#include "TFile.h"
#include "TGraph.h"
#include "TGraphAsymmErrors.h"
#include "TGraphErrors.h"
#include "TH1.h"
#include "TH2.h"
#include "THistPainter.h"
#include "TKey.h"
#include "TLatex.h"
#include "TLegend.h"
#include "TMath.h"
#include "TMatrixD.h"
#include "TMinuit.h"
#include "TMultiGraph.h"
#include "TNtuple.h"
#include "TPave.h"
#include "TPaveText.h"
#include "TPoint.h"
#include "TRandom.h"
#include "TRint.h"
#include "TStyle.h"
#include "TString.h"
#include "TTree.h"
#include "TH1F.h"
#include "TH2F.h"
#include "TSystem.h"
#include "TProfile.h"
#include "TVirtualFitter.h"
#include "TCanvas.h"
#include "TLegend.h"
#include "TColor.h"
#include <time.h>
using namespace std;
inline bool exists_test0 (const std::string& name) {
ifstream f(name.c_str());
return f.good();
}
///////////////////////////////////////////////////////////////////////////////////////////////
void loop(TChain *fChain){
//setting pedestal values
int PEDESTAL_LOW=400;
int PEDESTAL_HIGH=8000;
//this normally should be in a separate header, branches are defined.
UInt_t trigger;
fChain->SetBranchAddress("TRIGGER",&trigger);
UInt_t E_SiY[17];
UInt_t E_SiX[17];
UInt_t t_SiY[17];
UInt_t t_SiX[17];
for(int a=1;a<17;a++){
fChain->SetBranchAddress(Form("E_SiY%d",a),&E_SiY[a]);
fChain->SetBranchAddress(Form("E_SiX%d",a),&E_SiX[a]);
fChain->SetBranchAddress(Form("t_SiY%d",a),&t_SiY[a]);
fChain->SetBranchAddress(Form("t_SiX%d",a),&t_SiX[a]);
}
//creating histos
TH2D *h_pos_si_xy=new TH2D("h_pos_si_xy", "h_pos_si_xy",16,0.5,16.5,16,0.5,16.5);
//creating and initializing some variables used in the event loop (for "no double counting")
int r_pos_x=0,r_pos_y=0;
int dc_Ex_max=-999;
int dc_Ey_max=-999;
Long64_t nentries = fChain->GetEntries();
Long64_t nbytes = 0;
//starting the entry loop
for (Long64_t i=0; i<nentries;i++){
nbytes += fChain->GetEntry(i);
//event countdown
if ((float(i)/100000.)==int(i/100000)){cout << "event: " << i << " \tof " << nentries << endl;}
if(trigger==1){//trigger 1 = TargetON
dc_Ex_max=-999;
dc_Ey_max=-999;
for(int i_x=1;i_x<17;i_x++){
for(int i_y=1;i_y<17;i_y++){
if( ((int)t_SiX[i_x])>0 && ((int)t_SiY[i_y])>0){
if( PEDESTAL_LOW<((int)E_SiX[i_x]) && PEDESTAL_HIGH>((int)E_SiX[i_x]) &&
PEDESTAL_LOW<((int)E_SiY[i_y]) && PEDESTAL_HIGH>((int)E_SiY[i_y])){
//assign the hit to StripX and StripY where the most energy is deposited (rel. Ecalibration is needed, but roughly ok)
if(dc_Ex_max<((int)E_SiX[i_x]) && dc_Ey_max<((int)E_SiY[i_y])){
r_pos_x=i_x;
r_pos_y=i_y;
dc_Ex_max=E_SiX[i_x];
dc_Ey_max=E_SiY[i_y];
}
}
}
}
}
//Filling pos. histo
if(dc_Ex_max!=-999 && dc_Ey_max!=-999){h_pos_si_xy -> Fill(r_pos_x,r_pos_y);}
}//trigger==1
}//entry loop
//writing out the root output file
TFile *graphfile = TFile::Open((OUTPUT + (string)("") + ROOT_NAME).c_str(), "RECREATE");
graphfile -> mkdir("map");
graphfile -> cd("map");
h_pos_si_xy -> Write();
graphfile -> Close();
}//loop
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
void run(){
const char *command = new char[1000];
char filename[100];
TChain *fChain = new TChain("h101");
sprintf(filename,INPUT1);
if(exists_test0(INPUT1) && exists_test0(OUTPUT)){
cout<<"\033[0;37m//loading run: "<<filename << "\033[0m" <<endl;
fChain->Add(filename);
loop(fChain);
}
else{cout<<"\033[0;31mError 404: non-existing INPUT1 or OUTPUT file!\033[0m" <<endl;}
command = "rm *.so";
gSystem->Exec(command);
command = "rm *.d";
gSystem->Exec(command);
return;
}
int main(){
run();
return(0);
} |