root-project / root

The official repository for ROOT: analyzing, storing and visualizing big data, scientifically
https://root.cern
Other
2.71k stars 1.29k forks source link

Hadd does not add correctly histograms in nested folders #16474

Closed pviscone closed 1 month ago

pviscone commented 2 months ago

Check duplicate issues.

Description

Hi everyone, I am having a problem with hadd trying to merge some TH1. I have attached the root file that I am using.

Command hadd hadd.root ./EEstudy_DoubleElectronsPU0_131Xv3Tc_0.root ./EEstudy_DoubleElectronsPU0_131Xv3Tc_1.root

EEstudy_DoubleElectronsPU0_131Xv3Tc_0.root EEstudy_DoubleElectronsPU0_131Xv3Tc_1.root hadd.root

The files contain 2 TH1:

Trying to sum the histograms in the two files I obtain something weird:

I already had a first discussion on the ROOT forum: https://root-forum.cern.ch/t/hadd-does-not-add-correctly-histograms-in-nested-folders/61695

Reproducer

EEstudy_DoubleElectronsPU0_131Xv3Tc_0.root EEstudy_DoubleElectronsPU0_131Xv3Tc_1.root hadd.root

root check.cpp

//check.cpp
void check(){
    TFile file0 ("EEstudy_DoubleElectronsPU0_131Xv3Tc_0.root");
    TFile file1 ("EEstudy_DoubleElectronsPU0_131Xv3Tc_1.root");
    TFile hadd ("hadd.root");

    auto h0 = (TH1D*) file0.Get("HGCalCluTkGenMatchSelected/HGCalCluGenMatch/GenEle/pt");
    auto h1 = (TH1D*) file1.Get("HGCalCluTkGenMatchSelected/HGCalCluGenMatch/GenEle/pt");
    auto wrong_h1 = (TH1D*) file1.Get("GenEle/pt");
    auto hout=(TH1D*) hadd.Get("HGCalCluTkGenMatchSelected/HGCalCluGenMatch/GenEle/pt");

    for(int i=0; i<100; i++){
        cout << "Bin: " << i  << " Expected: " << h0->GetBinContent(i)+h1->GetBinContent(i) << " Wrong Sum: "  << h0->GetBinContent(i)+wrong_h1->GetBinContent(i)  << " FileOut: " << hout->GetBinContent(i) << endl;
    };
}

For each bin:

FileOut is always equal to Wrong Sum instead of Expected

Output:

Bin: 0 Expected: 0 Wrong Sum: 0 FileOut: 0
Bin: 1 Expected: 0 Wrong Sum: 0 FileOut: 0
Bin: 2 Expected: 0 Wrong Sum: 0 FileOut: 0
Bin: 3 Expected: 15 Wrong Sum: 250 FileOut: 250
Bin: 4 Expected: 95 Wrong Sum: 261 FileOut: 261
Bin: 5 Expected: 155 Wrong Sum: 329 FileOut: 329
Bin: 6 Expected: 210 Wrong Sum: 345 FileOut: 345
Bin: 7 Expected: 279 Wrong Sum: 401 FileOut: 401
Bin: 8 Expected: 293 Wrong Sum: 381 FileOut: 381
Bin: 9 Expected: 291 Wrong Sum: 360 FileOut: 360
Bin: 10 Expected: 287 Wrong Sum: 352 FileOut: 352
Bin: 11 Expected: 372 Wrong Sum: 460 FileOut: 460
Bin: 12 Expected: 330 Wrong Sum: 384 FileOut: 384
Bin: 13 Expected: 378 Wrong Sum: 434 FileOut: 434
Bin: 14 Expected: 351 Wrong Sum: 414 FileOut: 414
Bin: 15 Expected: 395 Wrong Sum: 444 FileOut: 444
Bin: 16 Expected: 410 Wrong Sum: 456 FileOut: 456
Bin: 17 Expected: 335 Wrong Sum: 382 FileOut: 382
Bin: 18 Expected: 411 Wrong Sum: 455 FileOut: 455
Bin: 19 Expected: 389 Wrong Sum: 435 FileOut: 435
Bin: 20 Expected: 357 Wrong Sum: 408 FileOut: 408
Bin: 21 Expected: 388 Wrong Sum: 430 FileOut: 430
Bin: 22 Expected: 330 Wrong Sum: 364 FileOut: 364
Bin: 23 Expected: 380 Wrong Sum: 415 FileOut: 415
Bin: 24 Expected: 348 Wrong Sum: 375 FileOut: 375
Bin: 25 Expected: 357 Wrong Sum: 391 FileOut: 391
Bin: 26 Expected: 413 Wrong Sum: 443 FileOut: 443
Bin: 27 Expected: 402 Wrong Sum: 447 FileOut: 447
Bin: 28 Expected: 398 Wrong Sum: 431 FileOut: 431
Bin: 29 Expected: 408 Wrong Sum: 445 FileOut: 445
Bin: 30 Expected: 419 Wrong Sum: 459 FileOut: 459
Bin: 31 Expected: 373 Wrong Sum: 400 FileOut: 400
Bin: 32 Expected: 401 Wrong Sum: 427 FileOut: 427
Bin: 33 Expected: 388 Wrong Sum: 416 FileOut: 416
Bin: 34 Expected: 471 Wrong Sum: 500 FileOut: 500
Bin: 35 Expected: 403 Wrong Sum: 432 FileOut: 432
Bin: 36 Expected: 435 Wrong Sum: 455 FileOut: 455
Bin: 37 Expected: 369 Wrong Sum: 390 FileOut: 390
Bin: 38 Expected: 379 Wrong Sum: 412 FileOut: 412
Bin: 39 Expected: 363 Wrong Sum: 389 FileOut: 389
Bin: 40 Expected: 400 Wrong Sum: 425 FileOut: 425
Bin: 41 Expected: 426 Wrong Sum: 467 FileOut: 467
Bin: 42 Expected: 396 Wrong Sum: 428 FileOut: 428
Bin: 43 Expected: 416 Wrong Sum: 446 FileOut: 446
Bin: 44 Expected: 472 Wrong Sum: 498 FileOut: 498
Bin: 45 Expected: 450 Wrong Sum: 479 FileOut: 479
Bin: 46 Expected: 414 Wrong Sum: 441 FileOut: 441
Bin: 47 Expected: 411 Wrong Sum: 432 FileOut: 432
Bin: 48 Expected: 426 Wrong Sum: 467 FileOut: 467
Bin: 49 Expected: 358 Wrong Sum: 385 FileOut: 385
Bin: 50 Expected: 381 Wrong Sum: 395 FileOut: 395
Bin: 51 Expected: 406 Wrong Sum: 435 FileOut: 435
Bin: 52 Expected: 442 Wrong Sum: 468 FileOut: 468
Bin: 53 Expected: 383 Wrong Sum: 411 FileOut: 411
Bin: 54 Expected: 437 Wrong Sum: 468 FileOut: 468
Bin: 55 Expected: 437 Wrong Sum: 459 FileOut: 459
Bin: 56 Expected: 468 Wrong Sum: 483 FileOut: 483
Bin: 57 Expected: 387 Wrong Sum: 408 FileOut: 408
Bin: 58 Expected: 423 Wrong Sum: 452 FileOut: 452
Bin: 59 Expected: 417 Wrong Sum: 443 FileOut: 443
Bin: 60 Expected: 435 Wrong Sum: 454 FileOut: 454
Bin: 61 Expected: 436 Wrong Sum: 462 FileOut: 462
Bin: 62 Expected: 397 Wrong Sum: 415 FileOut: 415
Bin: 63 Expected: 439 Wrong Sum: 467 FileOut: 467
Bin: 64 Expected: 424 Wrong Sum: 443 FileOut: 443
Bin: 65 Expected: 387 Wrong Sum: 413 FileOut: 413
Bin: 66 Expected: 425 Wrong Sum: 457 FileOut: 457
Bin: 67 Expected: 453 Wrong Sum: 467 FileOut: 467
Bin: 68 Expected: 421 Wrong Sum: 451 FileOut: 451
Bin: 69 Expected: 447 Wrong Sum: 467 FileOut: 467
Bin: 70 Expected: 444 Wrong Sum: 460 FileOut: 460
Bin: 71 Expected: 394 Wrong Sum: 413 FileOut: 413
Bin: 72 Expected: 458 Wrong Sum: 479 FileOut: 479
Bin: 73 Expected: 426 Wrong Sum: 454 FileOut: 454
Bin: 74 Expected: 421 Wrong Sum: 440 FileOut: 440
Bin: 75 Expected: 436 Wrong Sum: 457 FileOut: 457
Bin: 76 Expected: 452 Wrong Sum: 482 FileOut: 482
Bin: 77 Expected: 452 Wrong Sum: 468 FileOut: 468
Bin: 78 Expected: 427 Wrong Sum: 444 FileOut: 444
Bin: 79 Expected: 370 Wrong Sum: 388 FileOut: 388
Bin: 80 Expected: 448 Wrong Sum: 464 FileOut: 464
Bin: 81 Expected: 395 Wrong Sum: 416 FileOut: 416
Bin: 82 Expected: 429 Wrong Sum: 447 FileOut: 447
Bin: 83 Expected: 414 Wrong Sum: 428 FileOut: 428
Bin: 84 Expected: 430 Wrong Sum: 448 FileOut: 448
Bin: 85 Expected: 416 Wrong Sum: 424 FileOut: 424
Bin: 86 Expected: 409 Wrong Sum: 426 FileOut: 426
Bin: 87 Expected: 407 Wrong Sum: 425 FileOut: 425
Bin: 88 Expected: 424 Wrong Sum: 439 FileOut: 439
Bin: 89 Expected: 388 Wrong Sum: 415 FileOut: 415
Bin: 90 Expected: 415 Wrong Sum: 428 FileOut: 428
Bin: 91 Expected: 466 Wrong Sum: 484 FileOut: 484
Bin: 92 Expected: 391 Wrong Sum: 407 FileOut: 407
Bin: 93 Expected: 453 Wrong Sum: 470 FileOut: 470
Bin: 94 Expected: 410 Wrong Sum: 427 FileOut: 427
Bin: 95 Expected: 380 Wrong Sum: 402 FileOut: 402
Bin: 96 Expected: 460 Wrong Sum: 478 FileOut: 478
Bin: 97 Expected: 464 Wrong Sum: 490 FileOut: 490
Bin: 98 Expected: 435 Wrong Sum: 448 FileOut: 448
Bin: 99 Expected: 478 Wrong Sum: 494 FileOut: 494

ROOT version

   ------------------------------------------------------------------
  | Welcome to ROOT 6.32.04                        https://root.cern |
  | (c) 1995-2024, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for linuxx8664gcc on Aug 30 2024, 19:16:07                 |
  | From heads/master@tags/v6-32-04                                  |
  | With c++ (GCC) 14.2.1 20240805                                   |
  | Try '.help'/'.?', '.demo', '.license', '.credits', '.quit'/'.q'  |
   ------------------------------------------------------------------

Installation method

pre-built binary

Operating system

Linux

Additional context

No response

silverweed commented 2 months ago

Looks like a duplicate of #16190

silverweed commented 1 month ago

Fixed by https://github.com/root-project/root/pull/16518