Plotting array size


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Hello everyone,
I have an array and I want to calculate its size for each event, I wrote this code but I couldn’t run it because I got an error clear in the attached pic.
here is the code I wrote, and my input file consists of 4 columns and about 500k raws.
any help will be highly appreciated, thanks a lot in advance.


cluster_size.cc (1.9 KB)

    TH1F *h_cluster_size = new TH1F("cluster_size", "cluster_size", 100, 0., 727.);
    for(int i = 0; i < 4100; i++){ //looping over number of events
	  // for every event, you need to get the "posx" here and then:
	  h_cluster_size->Fill(posx.size());
	}

thanks a lot for your response, I edited the code as you advised but I still got the same error message

The error message you’re seeing is caused by the fact that the variable cluster_size is being redefined inside the for loop. Because it is already defined outside the loop, you can’t define it again within the loop. The fix @Wile_E_Coyote provided is the right one.

Here is the full corrected code:

int cluster_size(const char * input = "out1.txt", const char * output = "pixels.root"){
    ifstream fint;   //input file
    fint.open(input);
    if(!fint.is_open()){
      cout << "Input File " << input << " had issues while opening, closing the script\n";
        return -1;
    }

    TFile * fout = new TFile(output, "RECREATE");
    int pixarrX = 727, pixarrY = 727;       //number of pixels in 2D
    vector <int> posx;
    vector <int> posy;
    vector <float> thick;
    vector <double> dose;
    string spx, spy, sth, sd;
    string line;
    stringstream ssth, ssd;
    double d = 0, t = 0;
    int c_dec = 1;
    int prev = 0;
    while (getline(fint,line)){
        if(line.length() == 1 || line.empty()){
            continue;
        }
        else if(isdigit(line.at(0))){
            stringstream s_data(line);

            getline(s_data,spx,',');
            getline(s_data,spy,',');
            getline(s_data,sth,',');
            getline(s_data,sd,',');

            stringstream ssth(sth), ssd(sd);

            ssth >> t;      //common procedure for the conversion
            ssd >> d;

            posx.push_back(stoi(spx));  //for int (not in scientific notation) the stoi function is used
            posy.push_back(stoi(spy));
            thick.push_back(t);
            dose.push_back(d);
    }

    TH1F *h_cluster_size = new TH1F("cluster_size","cluster_size",100, 0.,727.);
    int cluster_size = 0;
    for(int i = 0; i < 4100; i++){ //looping over number of events
         cluster_size  = posx.size();
         h_cluster_size->Fill(cluster_size);
    }
    h_cluster_size->Write();
    fout->Close();
    return 0;
}

Can you try ?

Thanks a lot, for your detailed answer I have tried to run it and it is working perfectly.

Hello again every one
when I used the previous code couple of times it was giving me the same result, so I am guessing it is only plot one event not all events as I want,
I tried to include anothe column for the event ID to loop on them, and shifted the filling to the main loop, in order to make the loop on each event ID and plot all of them
but it gives me the errore in the attached picture,
any advice or help will be highly appreciated
thanks a lot

cluster22.cc (1.7 KB)

int cluster22(const char * input = "Chargeff.csv", const char * output = "pixels.root"){
   ifstream fint;   //input file
   fint.open(input);
   if(!fint.is_open()){
      cout << "Input File " << input << " had issues while opening, closing the script\n";
      return -1;
   }

   TFile * fout = new TFile(output, "RECREATE");
   int pixarrX = 727, pixarrY = 727;       //number of pixels in 2D
   vector <int> posx;
   vector <int> posy;
   vector <float> thick;
   vector <double> dose;
   vector <int> eventID;
   string spx, spy, sth, sd, spn;
   string line;
   stringstream ssth, ssd;
   double d = 0, t = 0;

   while (getline(fint,line)){
      if(line.length() == 1 || line.empty()){
         continue;
      } else if(isdigit(line.at(0))) {
         stringstream s_data(line);

         getline(s_data,spx,',');
         getline(s_data,spy,',');
         getline(s_data,sth,',');
         getline(s_data,sd,',');
         getline(s_data,spn,',');

         stringstream ssth(sth), ssd(sd);

         ssth >> t;      //common procedure for the conversion
         ssd >> d;

         posx.push_back(stoi(spx));  //for int (not in scientific notation) the stoi function is used
         posy.push_back(stoi(spy));
         thick.push_back(t);
         dose.push_back(d);
         eventID.push_back(stoi(spn));
      }

      TH1F *h_cluster_size = new TH1F("cluster_size","cluster_size",100, 0.,6.);
      int cluster_size = 0;
      for(int i = 0; i < eventID.size(); i++){ //looping over number of events
         cluster_size  = posx.size();
      }
      h_cluster_size->Fill(cluster_size);
      h_cluster_size->Write();
      fout->Close();
   }
   return 0;
}

thanks a lot for your help,
I got the previous error again

How do you run your macro ? This screen dumps does not show it.

root mymacrofile.cc

You call “fout->Close();” right for the first “getline(fint,line)”, so then the next “input lines” do not have any opened output file (and you recreate the “h_cluster_size” histogram for every “input line”).

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.