コード例 #1
0
ファイル: H4Reco.cpp プロジェクト: H4VFE/H4Analysis
//**********MAIN**************************************************************************
int main(int argc, char* argv[])
{
    if(argc < 2)
    {
        cout << argv[0] << " cfg file " << "[run]" << endl; 
        return -1;
    }

    //---memory consumption tracking---
    float cpu[2]{0}, mem[2]={0}, vsz[2]={0}, rss[2]={0};

    //---load options---    
    CfgManager opts;
    opts.ParseConfigFile(argv[1]);

    //-----input setup-----    
    if(argc > 2)
    {
        vector<string> run(1, argv[2]);
        opts.SetOpt("h4reco.run", run);
    }
    string outSuffix = opts.GetOpt<string>("h4reco.outNameSuffix");
    string run = opts.GetOpt<string>("h4reco.run");
    TChain* inTree = new TChain("H4tree");
    ReadInputFiles(opts, inTree);
    H4Tree h4Tree(inTree);

    //-----output setup-----
    uint64 index=stoul(run)*1e9;
    TFile* outROOT = new TFile("ntuples/"+outSuffix+TString(run)+".root", "RECREATE");
    outROOT->cd();
    RecoTree mainTree(&index);

    //---Get plugin sequence---
    PluginLoader<PluginBase>* loader;
    vector<PluginLoader<PluginBase>* > pluginLoaders;    
    map<string, PluginBase*> pluginMap;
    vector<PluginBase*> pluginSequence;
    vector<string> pluginList = opts.GetOpt<vector<string> >("h4reco.pluginList");    
    //---plugin creation
    pluginLoaders.reserve(pluginList.size());
    for(auto& plugin : pluginList)
    {
        cout << ">>> Loading plugin <" << plugin << ">" << endl;
        //---create loader 
        loader = new PluginLoader<PluginBase>(opts.GetOpt<string>(plugin+".pluginType"));
        pluginLoaders.push_back(loader);
        pluginLoaders.back()->Create();
        //---get instance and put it in the plugin sequence   
        PluginBase* newPlugin = pluginLoaders.back()->CreateInstance();
        if(newPlugin)
        {
            pluginSequence.push_back(newPlugin);
            pluginSequence.back()->SetInstanceName(plugin);
            pluginMap[plugin] = pluginSequence.back();
        }
        else
        {
            cout << ">>> ERROR: plugin type " << opts.GetOpt<string>(plugin+".pluginType") << " is not defined." << endl;
            return 0;
        }
    }

    //---begin
    for(auto& plugin : pluginSequence)
    {
        plugin->Begin(opts, &index);
        for(auto& shared : plugin->GetSharedData("", "TTree", true))
        {
            TTree* tree = (TTree*)shared.obj;
            tree->SetMaxVirtualSize(10000);
            tree->SetDirectory(outROOT);
        }
    }
            
    //---events loop
    int maxEvents=opts.GetOpt<int>("h4reco.maxEvents");
    cout << ">>> Processing H4DAQ run #" << run << " <<<" << endl;
    while(h4Tree.NextEntry() && (index-stoul(run)*1e9<maxEvents || maxEvents==-1))
    {
        if(index % 1000 == 0)
        {
            cout << ">>>Processed events: " << index-stoul(run)*1e9 << "/"
                 << (maxEvents<0 ? h4Tree.GetEntries() : min((int)h4Tree.GetEntries(), maxEvents))
                 << endl;
            TrackProcess(cpu, mem, vsz, rss);
        }

        //---call ProcessEvent for each plugin
        for(auto& plugin : pluginSequence)
            plugin->ProcessEvent(h4Tree, pluginMap, opts);

        //---fill the main tree with info variables and increase event counter
        mainTree.time_stamp = h4Tree.evtTimeStart;
        mainTree.run = h4Tree.runNumber;
        mainTree.spill = h4Tree.spillNumber;
        mainTree.event = h4Tree.evtNumber;
        mainTree.Fill();
        ++index;
    }

    //---end
    for(auto& plugin : pluginSequence)
    {
        //---call endjob for each plugin
        plugin->End(opts);
        //---get permanent data from each plugin and store them in the out file
        for(auto& shared : plugin->GetSharedData())
        {
            if(shared.obj->IsA()->GetName() == string("TTree"))
            {
                TTree* currentTree = (TTree*)shared.obj;
                outROOT->cd();
                currentTree->BuildIndex("index");
                currentTree->Write(currentTree->GetName(), TObject::kOverwrite);
                mainTree.AddFriend(currentTree->GetName());
            }
            else
            {
                outROOT->cd();
                shared.obj->Write(shared.tag.c_str(), TObject::kOverwrite);
            }
        }
    }
    
    //---close
    mainTree.Write();
    opts.Write("cfg");
    outROOT->Close();
    for(auto& loader : pluginLoaders)
        loader->Destroy();

    //---info
    TrackProcess(cpu, mem, vsz, rss);

    exit(0);
}    
コード例 #2
0
ファイル: g4rootered.C プロジェクト: liybu36/Montecarlo
//--------------------------------------------
//                    main 
//--------------------------------------------
int main (int argc, char *argv[]) {

  if(argc == 1 || (argc > 1 && !string(argv[1]).find("help")) )  { 
  //if(file.find("help") < 10) {
    cout << "Usage: g4rooter [FILE] [OPTIONS] [OUTPUT]" <<endl ;
    cout <<endl ;
    cout << " Options: " << endl ;
    cout << " nevents=N:  max number (N) of events to process (default: 10000)" <<endl ;    
    cout << " skipN=N:    skip the first N events (default: 0)" <<endl ;    
    cout << " kB=xxx:     set the Birks parameter (default: 0.012 cm/MeV)" <<endl ;    
    cout << " LY=xxx:     set the light yield (default: 500 p.e./MeV)" <<endl ;    
    cout << endl;
    cout << " Output: " << endl ;
    cout << " filename.root (default: FILE.root)" << endl ;
    cout << endl;
    cout << "Version v783.r4" << endl ;
    cout << "..bye ;) DF  ([email protected])" << endl ;
    return 0 ;
  }

  string file = argv[1] ;

  if(file.find(".fil") == string::npos) {
    cout << "file " <<  file << " not found.... Bye!" << endl;
    cout << "ps: the input file needs the .fil extension"  << endl;
    return 0;
  }
  string rootfile = file;
  rootfile.replace(file.find(".fil"),4,".root");

  int nevents     = 100000000;
  int skipNevents = 0;
  float kB        = 0.012;
  float LY        = 500.;

  int loop=2;
  while(argv[loop]) {
    string argument = argv[loop];
    if(!argument.find("nevents=")) {  
      argument.erase(0,8);  
      nevents = (int)  atoi(argument.c_str()) ;
      cout << "   max number of events to process: " << nevents << endl;
    }    
    if(!argument.find("skipN=")) {  
      argument.erase(0,6);  
      skipNevents = atoi(argument.c_str()) ;
      cout << "   events to skip: " << skipNevents << endl;
    }    
    if(!argument.find("kB=")) {  
      argument.erase(0,3);  
      kB = atof(argument.c_str()) ;
      cout << "   kB: " << kB << " cm/MeV" << endl;
    }    
    if(!argument.find("LY=")) {  
      argument.erase(0,3);  
      kB = atof(argument.c_str()) ;
      cout << "   LY: " << LY << " p.e./MeV" << endl;
    }    
    size_t found;
    found=argument.rfind(".root");
    if(found!=string::npos) { 
      rootfile = argument;
      cout << "   output root file: " << argument << endl ;
    }
    
    loop++;
  }
  
  TFile *ff = new TFile(rootfile.c_str(),"recreate");
  
  // event extra variables
  float radius = 0;
  
  // daughter variables
  int    Did[MAXDAUGHTER], Dpdg[MAXDAUGHTER], Dtrackid[MAXDAUGHTER], Dparenttrackid[MAXDAUGHTER],   // Dpid[MAXDAUGHTER],
    Dprocess[MAXDAUGHTER], Dmat[MAXDAUGHTER];
  float  Dene[MAXDAUGHTER], Dx[MAXDAUGHTER], Dy[MAXDAUGHTER], Dz[MAXDAUGHTER], 
         Dr[MAXDAUGHTER], Dpx[MAXDAUGHTER], Dpy[MAXDAUGHTER], Dpz[MAXDAUGHTER];
  double Dtime[MAXDAUGHTER];
  
  // deposit variables


/*  int    dep_pdg[MAXDEPOSIT], dep_mat[MAXDEPOSIT], dep_track[MAXDEPOSIT];
  float  dep_ene[MAXDEPOSIT], dep_x[MAXDEPOSIT], dep_y[MAXDEPOSIT], 
         dep_z[MAXDEPOSIT], dep_r[MAXDEPOSIT], dep_step[MAXDEPOSIT]; 
  double dep_time[MAXDEPOSIT];
*/

  int    *dep_pdg   = new int[MAXDEPOSIT]; 
  int    *dep_mat   = new int[MAXDEPOSIT];
  int    *dep_track = new int[MAXDEPOSIT];
  int    *dep_parenttrack = new int[MAXDEPOSIT];
  float  *dep_totalene  = new float[MAXDEPOSIT];
  float  *dep_kineticene  = new float[MAXDEPOSIT];
  float  *dep_ene   = new float[MAXDEPOSIT]; 
  float  *dep_x     = new float[MAXDEPOSIT];
  float  *dep_y     = new float[MAXDEPOSIT]; 
  float  *dep_z     = new float[MAXDEPOSIT]; 
  float  *dep_r     = new float[MAXDEPOSIT]; 
  float  *dep_step  = new float[MAXDEPOSIT]; 
  double *dep_time  = new double[MAXDEPOSIT];
   

  // user variables
  int    INT1[MAXUSER], INT2[MAXUSER];  
  float  FLOAT1[MAXUSER], FLOAT2[MAXUSER];
  double DOUBLE[MAXUSER];
  
  // pe variables
  int    *pe_pmt = new int[MAXNPE];     
  double *pe_time= new double[MAXNPE]; 
  int NPES1, NPES2 ; 

  // veto pe variables
  int    *veto_pe_pmt  = new int[MAXNPE];    
  double *veto_pe_time = new double[MAXNPE]; 
  
  // mu pe variables
  int    *mu_pe_pmt  = new int[MAXNPE];     
  double *mu_pe_time = new double[MAXNPE];   

  // ph variables
  int    ph_volume[MAXNPH], ph_pid[MAXNPH];
  float  ph_wl[MAXNPH], ph_x[MAXNPH], ph_y[MAXNPH], ph_z[MAXNPH];  
  double ph_time[MAXNPH]; 

  float s1ene,s2ene, veto_visene, mu_visene, ene, qene, qnpe, tpcene, vetoene, muene ;
  TTree *dstree = new TTree("dstree","The G4DS Root Tree");

  //  dstree->SetMaxVirtualSize(100000);
  
  dstree->SetMaxVirtualSize(10000000000);
  dstree->Branch("ev",             &theEvent.EventID,         "ev/I");
  dstree->Branch("pdg",            &theEvent.PDG,             "pdg/I");
  dstree->Branch("ene0",           &theEvent.Energy,          "ene0/F");  
  dstree->Branch("s1ene",          &theEvent.S1Energy,        "s1ene/F");     
  dstree->Branch("s2ene",          &theEvent.S2Energy,        "s2ene/F");     
  dstree->Branch("veto_visene",    &theEvent.VetoVisEnergy,   "veto_visene/F");    
  dstree->Branch("mu_visene",      &theEvent.MuVisEnergy,     "mu_visene/F");      

  dstree->Branch("tpcene",          &theEvent.TPCDepEnergy,   "tpcene/F");     
  dstree->Branch("vetoene",          &theEvent.VetoDepEnergy, "vetoene/F");     
  dstree->Branch("muene",          &theEvent.MuDepEnergy,     "muene/F");     
  dstree->Branch("ene",            &ene,		      "ene/F");        
  dstree->Branch("x",              &theEvent.Position[0],     "x/F"); 	       
  dstree->Branch("y",              &theEvent.Position[1],     "y/F"); 	       
  dstree->Branch("z",              &theEvent.Position[2],     "z/F"); 	       
  dstree->Branch("r",              &radius,		      "radius/F");     
  dstree->Branch("px",             &theEvent.Direction[0],    "px/F");	       
  dstree->Branch("py",             &theEvent.Direction[1],    "py/F");	       
  dstree->Branch("pz",             &theEvent.Direction[2],    "pz/F");	       
  dstree->Branch("bx",             &theEvent.CenterOfMass[0], "bx/F");	       
  dstree->Branch("by",             &theEvent.CenterOfMass[1], "by/F");	       
  dstree->Branch("bz",             &theEvent.CenterOfMass[2], "bz/F");	       

  dstree->Branch("npeS1",          &NPES1 ,	              "npeS1/I");        
  dstree->Branch("npeS2",          &NPES2 ,        	      "npeS2/I");        
  dstree->Branch("npe",            &theEvent.NPE ,	      "npe/I");        
  dstree->Branch("munpe" ,         &theEvent.MuNPE ,	      "munpe/I");      
  dstree->Branch("vnpe",           &theEvent.VetoNPE ,        "vnpe/I");       
  dstree->Branch("nph",            &theEvent.NPH,	      "nph/I");        
  dstree->Branch("ndaughters",     &theEvent.NDaughters,      "ndaughters/I"); 
  dstree->Branch("ndeposits" ,     &theEvent.NDeposits,       "ndeposits/I");  
  dstree->Branch("nusers",         &theEvent.NUsers,	      "nusers/I");     
  dstree->Branch("dau_id",         Did,                  "Did[ndaughters]/I");
  dstree->Branch("dau_pdg",        Dpdg,                 "Dpdg[ndaughters]/I");
  //  dstree->Branch("dau_pid",        Dpid,                 "Dpid[ndaughters]/I");
  dstree->Branch("dau_trackid",    Dtrackid,              "Dtrackid[ndaughters]/I");
  dstree->Branch("dau_parenttrackid",    Dparenttrackid,      "Dparenttrackid[ndaughters]/I");
  dstree->Branch("dau_process",    Dprocess,             "Dprocess[ndaughters]/I");
  dstree->Branch("dau_time",       Dtime,                "Dtime[ndaughters]/D");
  dstree->Branch("dau_ene",        Dene,                 "Dene[ndaughters]/F");   
  dstree->Branch("dau_x",          Dx,                   "Dx[ndaughters]/F");	 
  dstree->Branch("dau_y",          Dy,                   "Dy[ndaughters]/F");	 
  dstree->Branch("dau_z",          Dz,                   "Dz[ndaughters]/F") ;   
  dstree->Branch("dau_r",          Dr,                   "Dr[ndaughters]/F");	 
  dstree->Branch("dau_px",         Dpx,                  "Dpx[ndaughters]/F");   
  dstree->Branch("dau_py",         Dpy,                  "Dpy[ndaughters]/F") ;  
  dstree->Branch("dau_pz",         Dpz,                  "Dpz[ndaughters]/F");   
  dstree->Branch("dep_pdg",     dep_pdg,                  "dep_pdg[ndeposits]/I");    
  dstree->Branch("dep_mat",     dep_mat,                  "dep_mat[ndeposits]/I");    

  dstree->Branch("dep_track",   dep_track,                "dep_track[ndeposits]/I");    
  dstree->Branch("dep_parenttrack",   dep_parenttrack,                "dep_parenttrack[ndeposits]/I");    


  dstree->Branch("dep_time",    dep_time,                 "dep_time[ndeposits]/D");   
  dstree->Branch("dep_totalene",     dep_totalene,                  "dep_totalene[ndeposits]/F")  ;  
  dstree->Branch("dep_kineticene",     dep_kineticene,                  "dep_kineticene[ndeposits]/F")  ;  
  dstree->Branch("dep_ene",     dep_ene,                  "dep_ene[ndeposits]/F")  ;  
  dstree->Branch("dep_step",    dep_step,                 "dep_step[ndeposits]/F")  ;  
  dstree->Branch("dep_x",       dep_x,                    "dep_x[ndeposits]/F");      
  dstree->Branch("dep_y",       dep_y,                    "dep_y[ndeposits]/F");      
  dstree->Branch("dep_z",       dep_z,                    "dep_z[ndeposits]/F") ;     
  dstree->Branch("dep_r",       dep_r,                    "dep_r[ndeposits]/F") ;     
 
  dstree->Branch("userint1",    INT1,                     "int1[nusers]/I");  
  dstree->Branch("userint2",    INT2,                     "int2[nusers]/I");
  dstree->Branch("userfloat1",  FLOAT1,                   "float1[nusers]/F")  ; 
  dstree->Branch("userfloat2",  FLOAT2,                   "float2[nusers]/F");   
  dstree->Branch("userdouble0", DOUBLE,                   "double0[nusers]/D");  
 
  dstree->Branch("pe_time",     pe_time,                  "pe_time[npe]/D");
  dstree->Branch("pe_pmt",      pe_pmt,                   "pe_pmt[npe]/I");   
  
  dstree->Branch("vpe_time",  veto_pe_time,               "veto_pe_time[vnpe]/D");   
  dstree->Branch("vpe_pmt",   veto_pe_pmt,                "veto_pe_pmt[vnpe]/I"); 
    
  dstree->Branch("mupe_time",   mu_pe_time,               "mu_pe_time[munpe]/D");   
  dstree->Branch("mupe_pmt",    mu_pe_pmt,                "mu_pe_pmt[munpe]/I");   
  
  dstree->Branch("ph_volume",    ph_volume,               "ph_volume[nph]/I");
  dstree->Branch("ph_pid",       ph_pid,                  "ph_pid[nph]/I");
  dstree->Branch("ph_wl",        ph_wl,                   "ph_wl[nph]/F");
  dstree->Branch("ph_x",         ph_x,                    "ph_x[nph]/F");
  dstree->Branch("ph_y",         ph_y,                    "ph_y[nph]/F");
  dstree->Branch("ph_z",         ph_z,                    "ph_z[nph]/F");
  dstree->Branch("ph_time",      ph_time,                 "ph_time[nph]/D");
  
  
  // Open the binary file
  ifstream *_bin_fstream;
  _bin_fstream = new ifstream (file.c_str(), std::ios::binary);
  cout << endl ;
  cout << "Binary File: " << file << endl;
  cout << endl ;
  if (!(*_bin_fstream)) {
    std::cerr << "Cannot open file. Exiting...\n";
    exit(1);
  }
  
  int counter = 0;
  
  // Read Header
  _readHeader(_bin_fstream); 
  
  
  // Loop over the events
  for(int _i=0; _i<nevents; _i++) {  
  
    // Skip Events
    if(_i < skipNevents) {
      _skipEvent(_bin_fstream);
      continue;
    }
    // Read Event
    if(!_readEvent(_bin_fstream)) break ; 

    // Close the binary file if the end is reached
    if(_bin_fstream->eof()) break ;
    
    // Print counter

    if (!(counter % 100000) && counter>0) std::cout << counter <<" processed events " << " (event id = " <<  theEvent.EventID << ")"<< std::endl;
    counter++ ;
    
    // sort photon and photoelectron vectors by time
    std::sort(theDeposits.begin(),theDeposits.end(), cmp_deposit());
    std::sort(thePhotons.begin(),thePhotons.end(), cmp_photon());
    std::sort(thePhotoElectrons.begin(),thePhotoElectrons.end(), cmp_photoelectron());
    std::sort(theVetoPhotoElectrons.begin(),theVetoPhotoElectrons.end(), cmp_photoelectron());
    std::sort(theMuPhotoElectrons.begin(),theMuPhotoElectrons.end(), cmp_photoelectron());
    
    // initialize variables 
    ene         = 0;
    
   
    // Fill daughter variables
    for(int i=0;i<theEvent.NDaughters;++i) {
      Did[i]      = theDaughters[i].Id ;    
      Dpdg[i]     = theDaughters[i].PDG ;   
      //     Dpid[i]     = theDaughters[i].PID ;   
      Dtrackid[i]     = theDaughters[i].TrackID ;   
      Dparenttrackid[i]     = theDaughters[i].ParentTrackID ;   
      Dprocess[i] = theDaughters[i].Process ;
      Dtime[i]    = theDaughters[i].Time ;  
      Dene[i]     = theDaughters[i].Energy ;   
      Dx[i]       = theDaughters[i].Position[0] ;     
      Dy[i]       = theDaughters[i].Position[1] ;     
      Dz[i]       = theDaughters[i].Position[2] ;     
      Dpx[i]      = theDaughters[i].Direction[0] ;    
      Dpy[i]      = theDaughters[i].Direction[1] ;
      Dpz[i]      = theDaughters[i].Direction[2] ;
      Dr[i]       = sqrt(pow(Dx[i],2)+pow(Dy[i],2)+pow(Dz[i],2));
    }
    // Fill deposits variables
    for(int i=0;i<theEvent.NDeposits;++i) {
      dep_pdg[i]  = theDeposits[i].PID;
      dep_mat[i]  = theDeposits[i].Volume;
      dep_track[i]= theDeposits[i].Track;
      dep_parenttrack[i]= theDeposits[i].ParentTrack;
      dep_totalene[i]  = theDeposits[i].TotalEnergy;
      dep_kineticene[i]  = theDeposits[i].KineticEnergy;
      dep_ene[i]  = theDeposits[i].Energy;
      dep_step[i] = theDeposits[i].Step;
      dep_x[i]    = theDeposits[i].Position[0];
      dep_y[i]    = theDeposits[i].Position[1];
      dep_z[i]    = theDeposits[i].Position[2];
      dep_time[i] = theDeposits[i].Time;
      dep_r[i]    = sqrt(pow(dep_x[i],2)+pow(dep_y[i],2)+pow(dep_z[i],2));
      if(dep_mat[i] == 6) ene         += dep_ene[i];
    }    
    // Fill user variables
    for(int i=0;i<theEvent.NUsers;++i) {
      INT1[i]    = theUsers[i].UserInt1;
      INT2[i]    = theUsers[i].UserInt2;
      FLOAT1[i]  = theUsers[i].UserFloat1;
      FLOAT2[i]  = theUsers[i].UserFloat2; 
      DOUBLE[i]  = theUsers[i].UserDouble; 
    }
    NPES1 = 0 ; 
    NPES2 = 0 ; 
    // Fill pe variables
    for(int i=0;i<theEvent.NPE;++i) {
      pe_pmt[i]  = thePhotoElectrons[i].PMT;    
      pe_time[i] = thePhotoElectrons[i].Time;    
      if (pe_time[i] < 10000) NPES1++ ; 
      else NPES2++ ; 
    }
    // Fill veto pe variables
    for(int i=0;i<theEvent.VetoNPE;++i) {
      veto_pe_pmt[i]  = theVetoPhotoElectrons[i].PMT;    
      veto_pe_time[i] = theVetoPhotoElectrons[i].Time;    
    }    
     // Fill mu pe variables
    for(int i=0;i<theEvent.MuNPE;++i) {
      mu_pe_pmt[i]  = theMuPhotoElectrons[i].PMT;    
      mu_pe_time[i] = theMuPhotoElectrons[i].Time;    
    }
    // Fill photon variables
    for(int i=0;i<theEvent.NPH;++i) {
      ph_volume[i] = thePhotons[i].VolumeID; 
      ph_pid[i]    = thePhotons[i].PID; 
      ph_wl[i]     = thePhotons[i].Wavelength;  
      ph_x[i]      = thePhotons[i].Position[0];   
      ph_y[i]      = thePhotons[i].Position[1];   
      ph_z[i]      = thePhotons[i].Position[2];   
      ph_time[i]   = thePhotons[i].Time;    
    }
        
    // Fill the event
    dstree->Fill();
    
  }
  // Write the tree
  dstree->Write();
  
  // Close the root file
  ff->Close();

  // Close the binary file
  _bin_fstream->close();
  cout << "Rootfile " << rootfile.c_str() << " created! " << endl ;
  cout << "Bye...!" << endl ;
  return 0 ;
}