Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 08:17:26

0001 #include "TreeBuilder.hxx"
0002 #include "Particle.hxx"
0003 #include "Constants.hxx"
0004 #include "DEMPEvent.hxx"
0005 
0006 #include <iostream>
0007 #include <vector>
0008 #include <stdio.h>
0009 #include <string.h>
0010 #include "json/json.h"
0011 
0012 using namespace std;
0013 
0014 TreeBuilder::TreeBuilder(const char * file_name, const char * name) {
0015 
0016   extern Json::Value obj;
0017 
0018   tree_name = name;
0019 
0020 //  string str_copy = "RootFiles/" + obj["file_name"].asString() + ".root";
0021 //  file_name = str_copy.c_str();
0022 //
0023 //  cout << obj["output_file"].asString() << endl;
0024 //  cout << file_name << endl;
0025 //  cout << file_name <<endl;
0026 
0027 
0028   File_Out = new TFile(file_name, "RECREATE");
0029   Tree_Out = new TTree(tree_name, tree_name);
0030 
0031   Tree_Out->SetAutoSave();
0032 
0033   nParticles = 0;
0034 
0035   nVars = 8;
0036 }
0037 
0038 void TreeBuilder::AddParticle(Particle * p)
0039 {
0040   ParticleList.push_back(p);
0041   vector< double* >* TempVec = new vector<double*>(nVars);
0042   // Vector elements must be initialized to non-null
0043   // pointers
0044   for (int i = 0; i<nVars; i++)
0045     TempVec->at(i) = new double();
0046 
0047   PropList.push_back(TempVec);
0048 
0049   char * p_name = p->GetName();
0050   char b_name[100]; // Branch name
0051   char l_list[100]; // Leaf list
0052 
0053   strcpy(b_name, p_name);
0054   strcat(b_name, "_pid");
0055   strcpy(l_list, b_name);
0056   strcat(l_list, "/D");
0057   Tree_Out->Branch(b_name,TempVec->at(0),l_list);
0058 
0059   strcpy(b_name, p_name);
0060   strcat(b_name, "_Px");
0061   strcpy(l_list, b_name);
0062   strcat(l_list, "/D");
0063   Tree_Out->Branch(b_name,TempVec->at(1),l_list);
0064 
0065   strcpy(b_name, p_name);
0066   strcat(b_name, "_Py");
0067   strcpy(l_list, b_name);
0068   strcat(l_list, "/D");
0069   Tree_Out->Branch(b_name,TempVec->at(2),l_list);
0070 
0071   strcpy(b_name, p_name);
0072   strcat(b_name, "_Pz");
0073   strcpy(l_list, b_name);
0074   strcat(l_list, "/D");
0075   Tree_Out->Branch(b_name,TempVec->at(3),l_list);
0076 
0077 
0078   strcpy(b_name, p_name);
0079   strcat(b_name, "_E");
0080   strcpy(l_list, b_name);
0081   strcat(l_list, "/D");
0082   Tree_Out->Branch(b_name,TempVec->at(4),l_list);
0083 
0084   strcpy(b_name, p_name);
0085   strcat(b_name, "_theta");
0086   strcpy(l_list, b_name);
0087   strcat(l_list, "/D");
0088   Tree_Out->Branch(b_name,TempVec->at(5),l_list);
0089 
0090   strcpy(b_name, p_name);
0091   strcat(b_name, "_phi");
0092   strcpy(l_list, b_name);
0093   strcat(l_list, "/D");
0094   Tree_Out->Branch(b_name,TempVec->at(6),l_list);
0095 
0096   strcpy(b_name, p_name);
0097   strcat(b_name, "_P");
0098   strcpy(l_list, b_name);
0099   strcat(l_list, "/D");
0100   Tree_Out->Branch(b_name,TempVec->at(7),l_list);
0101 
0102   nParticles++;
0103 
0104   //cout << ParticleList[0] << endl;
0105   //cout << TempVec->at(1) << endl;
0106   //cout << PropList.at(0)->at(1) << endl;
0107 }
0108 
0109 void TreeBuilder::Fill()
0110 {
0111   Retrieve();
0112   Tree_Out->Fill();
0113 }
0114 
0115 // Particles have private data members that must be retrieved
0116 // to be copied to an accessible address to be added to the
0117 // TTree.
0118 void TreeBuilder::Retrieve()
0119 {
0120   for (int i = 0; i < nParticles; i++){
0121     //cout << PropList.at(i)->at(1) <<endl;
0122     *PropList.at(i)->at(0) = (double)ParticleList.at(i)->GetPid();
0123     *PropList.at(i)->at(1) = ParticleList.at(i)->Px()/1000;
0124     *PropList.at(i)->at(2) = ParticleList.at(i)->Py()/1000;
0125     *PropList.at(i)->at(3) = ParticleList.at(i)->Pz()/1000;
0126     *PropList.at(i)->at(4) = ParticleList.at(i)->E()/1000;
0127     *PropList.at(i)->at(5) = ParticleList.at(i)->Theta()*constants::DEG;
0128     *PropList.at(i)->at(6) = ParticleList.at(i)->Phi()*constants::DEG;
0129     *PropList.at(i)->at(7) = ParticleList.at(i)->P()/1000;
0130     //cout <<  &PropList.at(i)->at(1) << endl;
0131   }
0132 }
0133 
0134 void TreeBuilder::Save()
0135 {
0136   Tree_Out->AutoSave();
0137   //File_Out->Write();
0138 }
0139 
0140 void TreeBuilder::AddDouble(double* x, const char* name)
0141 {
0142   char b_name[100];
0143   char l_list[100];
0144 
0145   strcpy(b_name, name);
0146   strcpy(l_list, name);
0147   strcat(l_list,"/D");
0148 
0149   Tree_Out->Branch(b_name, x, l_list);
0150 
0151 }
0152 
0153 void TreeBuilder::AddEvent(DEMPEvent * event)
0154 {
0155   this->AddParticle(event->BeamElec);
0156   this->AddParticle(event->ProdPion);
0157   this->AddParticle(event->ProdProt);
0158   this->AddParticle(event->ScatElec);
0159   this->AddParticle(event->TargNeut);
0160   //this->AddParticle(event->VirtPhot);
0161 
0162   /*
0163   this->AddDouble(event->qsq_GeV,"qsq_GeV");
0164   this->AddDouble(event->w_GeV,"w_GeV");
0165   this->AddDouble(event->t_GeV,"t_GeV");
0166   this->AddDouble(event->t_prime_GeV, "t_prime_GeV");
0167   this->AddDouble(event->x_B, "x_B");
0168   this->AddDouble(event->negt, "negt");
0169   
0170   this->AddDouble(event->Vertex_x,"Vertex_x");
0171   this->AddDouble(event->Vertex_y,"Vertex_y");
0172   this->AddDouble(event->Vertex_z,"Vertex_z");
0173   */
0174 }